We have code for AES-128 encryption in Java and we want some equivalent code in WP7.
However, we run into a problem: The two implementations yield different encrypted texts
Here is the code we're using:
Java Code
package com.emap.services; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESEcrypt1 { static byte[] ibv = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}; public String encryptData() { String message = "Testing AES encryption-decryption amlgorithm for WP7."; String encryptedStr = ""; try { SecretKeySpec skeySpec = new SecretKeySpec("Passkey".getBytes(), "AES"); IvParameterSpec iv = new IvParameterSpec(ibv); // Instantiate the cipher Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(message.getBytes()); encryptedStr = Base64.encode(encrypted); } catch (BadPaddingException ex) { System.out.println("Error: " + ex.getMessage()); encryptedStr = "error"; } catch (IllegalBlockSizeException ex) { System.out.println("Error: " + ex.getMessage()); encryptedStr = "error"; } catch (InvalidAlgorithmParameterException ex) { System.out.println("Error: " + ex.getMessage()); encryptedStr = "error"; } catch (InvalidKeyException ex) { System.out.println("Error: " + ex.getMessage()); encryptedStr = "error"; } catch (NoSuchAlgorithmException ex) { System.out.println("Error: " + ex.getMessage()); encryptedStr = "error"; } catch (NoSuchPaddingException ex) { System.out.println("Error: " + ex.getMessage()); encryptedStr = "error"; } catch (Exception ex) { System.out.println("Error: " + ex.getMessage()); encryptedStr = "error"; } System.out.println("Encrypted: " + encryptedStr); return encryptedStr; } } WP7 Code
static byte[] ibv = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}; public string Encrypt(string dataToEncrypt, string password) //public string Encrypt(string dataToEncrypt) { AesManaged aes = null; MemoryStream memStream = null; CryptoStream crStream = null; try { //Generate a Key based on a Password and Salt //Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt)); Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, ibv); aes = new AesManaged(); aes.Key = rfc2898.GetBytes(aes.KeySize / 8); aes.IV = rfc2898.GetBytes(aes.BlockSize / 8); memStream = new MemoryStream(); crStream = new CryptoStream(memStream, aes.CreateEncryptor(), CryptoStreamMode.Write); byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt); crStream.Write(data, 0, data.Length); crStream.FlushFinalBlock(); //Return Base 64 String return Convert.ToBase64String(memStream.ToArray()); } finally { //cleanup if (crStream != null) crStream.Close(); if (memStream != null) memStream.Close(); if (aes != null) aes.Clear(); } } Any help would be greatly appreciated.
message.getBytes()- that will use the platform default encoding in Java. That's probably not the problem, but it's a bad idea anyway.