0

Guys I am working on WP8 C# project and I'm having a problem with AES encryption in C#.

I have written code that encrypts a byte array with a key and an IV and returns a byte array;

AES Encryption in C#

public byte[] encryptAESWithIV(byte[] key, byte[] Text)
   {
     byte[] encrypted = new byte[48];
     byte[] iv = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

     encrypted = EncryptStringToBytes_Aes(toHex(Text), key, iv);

     byte[] output = new byte[encrypted.Length + iv.Length];
     Array.Copy(iv, 0, output, 0, iv.Length);
     Array.Copy(encrypted, 0, output, iv.Length, encrypted.Length);

     return output;
   }

AES Encryption in Java (Android code)

public static byte[] encryptAESWithIV(byte[] key, byte[] clearText)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 
           InvalidParameterSpecException, IllegalBlockSizeException, 
           BadPaddingException, UnsupportedEncodingException {

    SecretKeySpec skeySpec = new SecretKeySpec(key, CIPHER_AES);
    Cipher cipher = Cipher.getInstance(CIPHER_AES_MODE);

    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    //random.nextBytes(iv);

    try {
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv)); 
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }

    byte[] encrypted = cipher.doFinal(clearText);
    byte[] output = new byte[encrypted.length + iv.length];
    System.arraycopy(iv, 0, output, 0, iv.length);
    System.arraycopy(encrypted, 0, output, iv.length, encrypted.length);
    Log.v("encrypt-length",""+encrypted.length);
    Log.v("iv-length",""+iv.length);
    //Log.d("output_bytes", Arrays.toString(output));
    return output;  
}

In both cases I have given the same IV, Text and key.

In C# I did not get the same result as with the Java code. The Java code is correct and I want to get the same result in C#.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Arsal
  • 343
  • 2
  • 8
  • 21
  • I can't see working with Cipher in C# code. And the cipher should be there, same as in Java – Olter Mar 25 '14 at 11:39
  • is there not possible without cipher..??If no then any possible solution? – Arsal Mar 25 '14 at 11:44
  • yup ..I just wanna same thing in C#. But could not found any proper solution.Well thanks – Arsal Mar 25 '14 at 11:51
  • Are you using `EncryptStringToBytes_Aes` as defined on this page: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aes%28v=vs.110%29.aspx? – Duncan Jones Mar 25 '14 at 12:02
  • Seems, that's a code for .Net Framework, not Silverlight for windows-phone. Perhaps this [link](http://stackoverflow.com/questions/19699977/aes-encryption-java-to-c-sharp) would help? – Olter Mar 25 '14 at 12:05
  • @Duncan I have used your link before but could not found require solution. Any running or Comparative solution to Java code..?? – Arsal Mar 25 '14 at 13:50
  • @Olter I have just data in bytes not in strings. byte[] encrypted = cipher.doFinal(clearText); return byte array. I wanna same in C#. Any comparatively solution with code..? – Arsal Mar 25 '14 at 13:55
  • http://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in-C I have followed this link to encrypts a byte array with a key and an IV and returns a byte array; But Rijndael alg = Rijndael.Create(); shows an error. Even I have installed Rijndael reference. May be I could get any solution.? – Arsal Mar 25 '14 at 13:58

0 Answers0