3DES, Encryption in C (Unsigned), Decryption in Java
Hi Guys,
I face 3DES decryption problem in Java where as encryption was done in C by using OpenSSL. Java 3DES method input is in byte[]. As we known, Java didn抰 support for unsigned data type. That抯 why my decryption result is different with the original value.
Below is my Java code:
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
DESedeKeySpeckeySpec =newDESedeKeySpec(getTripleDesMD5Value("abcde"));
SecretKey key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] finalBytes=newbyte[16];
byte[] bytesToEncrypt ="1234".getBytes();
if (bytesToEncrypt.length<16){
int i=0;
for (i=0;i<16;i++){
if (i<bytesToEncrypt.length){
finalBytes[i]=bytesToEncrypt[i];
}else{
finalBytes[i]='\0';
}
}
}
String encryptedBytesAsString="";
byte[] oBytes = finalBytes;
byte[] otBytes =newbyte[8];
for (int i=0;i<8;i++){
otBytes[i]=oBytes[i];
}
byte[] encryptedBytes = cipher.doFinal(otBytes);
getTripleDesMD5Value("abcde") = in signed byte.
In other words, encryption/decryption in Java will use signed byte object as an input.
Anyone face this problem before and get solution?
Thank you very much.
Regards,
Sapiduta>

