I have a following code in C#. It does encoding an array of bytes with an AES symmetric algorithm. I need to write Java equivalent of this code.
class Program
{
static void Main(string[] args)
{
string a = "ABCDEFGHIJKLMNOP";
byte[] bytes = Encoding.ASCII.GetBytes(a);
byte[] cipher = encode(bytes, "1111111122222222111111112222222211111111222222221111111122222222", "66666666555555556666666655555555");
}
private static byte[] encode(byte[] toEncrypt, string sKey, string sIV)
{
byte[] IV = new byte[16];
byte[] key = new byte[32];
byte[] array = new byte[toEncrypt.Length];
string s;
for (int i = 0; i < IV.Length; ++i)
{
s = sIV.Substring(i * 2, 2);
IV[i] = Convert.ToByte(s, 16);
}
for (int i = 0; i < key.Length; ++i)
{
s = sKey.Substring(i * 2, 2);
key[i] = Convert.ToByte(s, 16);
}
MemoryStream filecrypt = new MemoryStream(array);
AesManaged encrypt = new AesManaged();
encrypt.Mode = CipherMode.CBC;
encrypt.Padding = PaddingMode.None;
encrypt.BlockSize = 128;
encrypt.KeySize = 256;
CryptoStream cs = new CryptoStream(filecrypt, encrypt.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(toEncrypt, 0, toEncrypt.Length);
cs.Close();
return array;
}
}
This is my attempt of writing this in Java. The code looks fine, but the output is different, something must be wrong.
public class Main {
public static void main(String [] args) {
byte [] code = encode("ABCDEFGHIJKLMNOP".getBytes(), "1111111122222222111111112222222211111111222222221111111122222222", "66666666555555556666666655555555");
}
private static byte[] toByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
int a;
int b;
for (int i = 0; i < len; i += 2) {
a = (Character.digit(s.charAt(i), 16) << 4);
b = Character.digit(s.charAt(i+1), 16);
int n = (Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16);
data[i / 2] = (byte) (n);
}
return data;
}
private static byte[] encode(byte[] toEncrypt, String skey, String siv)
{
byte[] key = toByteArray(skey);
byte[] iv = toByteArray(siv);
byte[] array = new byte[toEncrypt.length];
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
array = cipher.doFinal(array);
} catch (Exception ex) {
ex.printStackTrace();
}
return array;
}
}
Any clues and ideas will be very appreciated.