Guys i m doing a project in windows phone 8.i m trying to do aes encryption of a byte array into byte array using key and iv.Also passing the same key and iv in android and wp8.but not getting the same outputs.Whats wrong with my code??? please help me regarding this issue.
My C# aes encryption code is:
public static byte[] Encrypt (byte[] data, byte[] key)
{
byte[] encrypted;
using (AesManaged AESM = new AesManaged())
{
byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
AESM.KeySize = 128;
AESM.BlockSize = AESM.LegalBlockSizes[0].MaxSize;
AESM.Key = key;
AESM.IV = iv;
ICryptoTransform encryptor = AESM.CreateEncryptor();
encrypted = encryptor.TransformFinalBlock(data, 0, data.Length);
}
return encrypted;
}
My android aes encryption code is:
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 }
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);
return output;
}