0

Possible Duplicate:
AES Encryption in Java and Decryption in C#

I have a file that is encrypted in C# alg has my IV and Key, the IV is 16 bytes and the Key is 32. The encryption properties are AES/CBC/PKCS7Padding. After doing the encryption I store off the keys since this is a 256byte encryption the bytes[] contains numbers ranging from 0 - 255. I have proven that if I encrypt the file using all number less than 128 my Java code will decrypt the file properly. However when i try to decrypt any files where the byte[] contains something that is larger than 127 it fails to decrypt because the keys are not correct. When I look at the bytes in Java they are auto converted to signed meaning a 241 is translated to a -14. I was thinking the bits should be the same and it should work but it does not. In the Java code I have placed the bytes in a string to verify they are exactly what is used in C# to Encrypt and that work to Decrypt on C#. They are but when placed into the byte[] that is passed they are translated.

Help please

bool Encrypt(string fileIn,string fileOut, Guid archiveKey, SymmetricAlgorithm alg) { // First we are going to open the file streams var fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read, FileShare.None); var fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

        // Now create a crypto stream through which we are going
        // to be pumping data. 
        // Our fileOut is going to be receiving the encrypted bytes. 
        var x = alg.CreateEncryptor();
        var cs = new CryptoStream(fsOut,
            x, CryptoStreamMode.Write);

        // Now will will initialize a buffer and will be processing
        // the input file in chunks. 
        // This is done to avoid reading the whole file (which can
        // be huge) into memory. 
        bufferLen = Shared.GetFileAttributes.ProcessingBufferSize;
        buffer = new byte[bufferLen];

        do
        {
            // read a chunk of data from the input file 
            bytesRead = fsIn.Read(buffer, 0, bufferLen);

            // encrypt it 
            cs.Write(buffer, 0, bytesRead);
            var inc = bytesRead == Shared.GetFileAttributes.ProcessingBufferSize ? 1 : bytesRead / Shared.GetFileAttributes.ProcessingBufferSize;
        } while (bytesRead != 0);

        // close everything 

        // this will also close the unrelying fsOut stream
        cs.Close();
        fsIn.Close();
        return true;
    }

Java Code public void DecryptTheFile(String fileName, String fileOut, String keys) throws Exception

 {
  setupKeys(IV, KEY,keys);

  // setup the cipher with the keys
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
  SecretKeySpec key = new SecretKeySpec(KEY, "AES/CBC/PKCS7Padding");
  IvParameterSpec ips = new IvParameterSpec(IV);
  cipher.init(Cipher.DECRYPT_MODE, key,ips);

  // decrypt a file
  byte[] buffer = new byte[1024];
  InputStream iFile = new FileInputStream(fileName);
  OutputStream oFile = new FileOutputStream(fileOut);

  iFile = new CipherInputStream(iFile, cipher);

  int r = 0;
  while ((r = iFile.read(buffer, 0, 1024)) > 0) {
   oFile.write(buffer, 0, r);
  }
  oFile.close();
  iFile.close();
 }
 private void setupKeys(byte[] IV,byte[] KEY,String keys)
 {
     String[] keyparts = keys.split(",");
     for (int i = 0; i < 16; i++)
     {
         Long l = Long.parseLong(keyparts[i]);
         IV[i] = (byte) (l.byteValue() );
         if (siv == null) siv = l.toString();
         else    siv = siv + l.toString();
     }
     for (int i = 16; i < 47; i++)
     {
         Long l = Long.parseLong(keyparts[i]);
         KEY[i - 16] = (byte) (l.byteValue() & 0xff);
         if (skey == null ) skey = l.toString();
         else            skey = skey + l.toString();
     }

 }
Community
  • 1
  • 1
David
  • 11
  • 1
  • 1
    There are a WHOLE bunch of "I used AES in C# and want to decrypt in Java" and vice-versa questions on Stack Overflow. Please look at the related questions to the right of this one. If your question is distinctly different, please point out the specific difference you think you see. – Eric J. Aug 23 '12 at 22:54
  • for (int i = 16; i < 47; i++) looks like you are missing one byte. – Jack Aug 24 '12 at 11:14
  • I also kind of suspect that you are reusing the same IV, which would be wrong. (Though this is difficult to tell from the text above) – Jack Aug 24 '12 at 11:17
  • Thanks for the catch on the 47 I was dropping the last byte - it did not resolve the issue though. – David Aug 24 '12 at 16:06
  • In response to the statements above. What makes this unique is the code works so long and the bytes[] for the key and iv are all smaller than 127. Both the Key and IV are random generated on the C# side and are between 0 and 255. There were some good reads in the existing list one that mentioned that android does not offer good support of 256 byte encryption. This is ultimatly going to be an android application. I also notice that none of the examples displayed or resolved contain a key or iv with byte values above 127. – David Aug 24 '12 at 17:11

0 Answers0