0

I can't find the method to decrypt an encrypted String with Cipher. I have a key file and I don't know when I have to introduce or how can I inject the key for decryption!!

A example of the code is this:

public static void main(String args[]) throws Exception {
    KeyGenerator kg = KeyGenerator.getInstance("DESede");
    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    PrivateKey key = getPrivateKey("path/DESedeRACE.key");

    c.init(Cipher.ENCRYPT_MODE, key);
    byte input[] = "Gufz74gXCm2UV3ux+D4/Bnp7Jop90EvfK3QCkt6ZhRmtKeRf9OxzNilY8Xcai1UUQ==".getBytes();
    byte encrypted[] = c.doFinal(input);
    byte iv[] = c.getIV();
    System.out.println(new String(iv));

    IvParameterSpec dps = new IvParameterSpec(iv);
    c.init(Cipher.DECRYPT_MODE, key, dps);
    byte output[] = c.doFinal(encrypted);
    System.out.println(new String(output));
} 
halil
  • 800
  • 16
  • 33
J Lopez
  • 1
  • 1
  • What is the format of your key file? – Beethoven Sep 08 '16 at 13:41
  • Don't know... the customer only sent me a file.key for descrypt and i know that is a DESede/CBC/PKCS5Padding – J Lopez Sep 08 '16 at 13:51
  • Hm unfortunate... What happens if you just read the contents of the file into the `input` array? – Beethoven Sep 08 '16 at 13:53
  • I Can't ! :D don't know why! – J Lopez Sep 08 '16 at 14:02
  • Like this `byte[] input = new byte[48]; try(DataInputStream in = new DataInputStream(new FileInputStream("path/DESedeRACE.key"))) { in.readFully(input); }`? – Beethoven Sep 08 '16 at 14:04
  • Another idea: You seem to be using a Base64-encoded key, converting it directly to bytes. Probably you will have to decode it first: `byte[] input = Base64.getDecoder().decode(string)` – Beethoven Sep 08 '16 at 14:11
  • Exception in thread "main" java.io.EOFException at java.io.DataInputStream.readFully(DataInputStream.java:197) at java.io.DataInputStream.readFully(DataInputStream.java:169) at Roger.main(Roger.java:19) – J Lopez Sep 08 '16 at 14:32
  • This exception means, that the file is shorter than the number of bytes we tried to read. How large is your file, and what is your required key size? – Beethoven Sep 08 '16 at 14:33
  • I can't join in the .key file... and i wanna read the response of a webservice... for example: ifekNvQYzyYKkR7pr68ayFIMwbdPajrrC1aphgzIK1zl1voSiXis8o90E0LQF0MIPWWVboYBrm7PaORpvwwgGuz74gXCm2UV3ux+D4/Bnp7Jop90EvfK3QCkt6ZhRmtKeRf9OxzNilY8Xcai1UUQ== – J Lopez Sep 08 '16 at 14:43
  • @JLopez I don't quite get what you're asking. Do you have trouble writing the decryption method in Java? It's basically the same thing as the encryption, but you have to produce an IV and ciphertext that are actually usable, because you will lose some bytes when trying to print arbitrary bytes (`new String(iv)` and `new String(output)`) by assuming a valid character encoding. You need to use something like Hex or Base64 to produce a printable representation of the IV and ciphertext. – Artjom B. Sep 08 '16 at 18:00
  • Possible duplicate of [IllegalBlockSizeException when trying to encrypt and decrypt a string with AES](http://stackoverflow.com/questions/30383736/illegalblocksizeexception-when-trying-to-encrypt-and-decrypt-a-string-with-aes) – Artjom B. Sep 08 '16 at 18:00

0 Answers0