For legacy compatibility I am being tasked with taking a 40 character value and adding 00000000 to the end and encrypting using RC2 in CBC Mode. I've been provided a 20 character key to use for encryption and a stand alone tool, written in Java, already being used to encrypt one 48 character string at a time. I am writing a script that will iterate through the list of 48 character values, encrypt each one, and then write it back to a table. I've tried to follow the Java code to replicate the process but am not getting the same results. Any help would be appreciated. Currently there is just one 48 character value in the list of values for testing. Later it will be pointed at an Oracle table for input and output:
value: e134db7b54ac00cb4236bb1be093e555613a54a600000000
key: 4757A2501EF662FD4C62
Python Result: EE2FCB7440EF47E55D4C01E8FCFF0069FB31438C4D69CA54
Java Result: F05CD7CD8906548C9B9FA2489D0B80A090BCF1D24FCE425B
Python:
from Cryptodome.Cipher import ARC2
values = ['e134db7b54ac00cb4236bb1be093e555613a54a600000000']
for value in values:
value = bytearray(value, 'ascii').decode('hex')
key = bytearray('4757A2501EF662FD4C62', 'ascii').decode('hex')
iv = '\x00\x00\x00\x00\x00\x00\x00\x00'
ef_keylen = 80
cipher = ARC2.new(key, ARC2.MODE_CBC, iv=iv, effective_keylen=ef_keylen)
encryptedvalue = cipher.encrypt(value)
encryptedvalue = encryptedvalue.encode('hex')
Java:
public static byte[] encrypt(String value, String rc2Key) throws Exception {
byte[] valueBytes = Hex.decodeHex(value.toCharArray());
byte[] rc2KeyBytes = Hex.decodeHex(rc2Key.toCharArray());
Key k = new SecretKeySpec(rc2KeyBytes, "RC2");
byte[] iv = {0,0,0,0,0,0,0,0};
RC2ParameterSpec spec = new RC2ParameterSpec(rc2Key.length() * 4, iv);
Cipher cipher = Cipher.getInstance("RC2/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, k, spec);
byte[] encrypted = cipher.doFinal(valueBytes);
return encrypted;
}
Why am I getting different encrypted values? What step am I missing. I'm fluent in Python or c# but am a novice with Java so am not sure where I am going wrong...