Decrypt AES encrypted file in C # using java

I have the following problem. I use this code to encrypt sample text in C # and want to decrypt it in java. I am using the following java code.

byte[] IV = { 65, 1, 2, 23, 4, 5, 6, 7, 32, 21, 10, 11, 12, 13, 84, 45 };
byte[] KEY = { 0, 42, 2, 54, 4, 45, 6, 7, 65, 9, 54, 11, 12, 13, 60, 15 };
byte baData[] = new byte[1024];
int iRead = 0;

SecretKeySpec key = new SecretKeySpec(KEY, "AES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));

File file = new File("/sdcard", "SAMPLE.txt");

FileInputStream in = new FileInputStream(file);
iRead = in.read(baData, 0, baData.length);

String strResult = new String(cipher.doFinal(baData, 0, baData.length));

I obviously use the same IV and KEY values ​​above as in the C # example. The above java code runs on an Android device, and the encrypted binary is SAMPLE.txt on sdcard. The problem is that I do not get the correct data after decrypting it. Can someone tell me what I am doing wrong?

Thank.

+3
source share
1 answer

There are several issues here:

  • ? - .
  • , read() - iRead
  • , 1024-

, .

CipherInputStream FileInputStream, InputStreamReader , . - Guava CharStreams.toString(), InputStreamReader .

EDIT: , , , Cipher , .

+6

All Articles