• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting C++ Windows Crypto API to Java

 
Franck McGeough
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm converting a project written in C++ that uses the Windows Crypto API to Java. I downloaded the crypto jar from bouncycastle.org and it works great within Java but I'm having problems when unit testing the decryption within Java of a C++ encrypted string. Does anyone have any pointers at web sites / books that cover this type of conversion effort?

The C++ code uses RC4. The encrypted data is actual hex-encoded (that's what the hexStringToByteArray call is all about). The Java code to decrypt looks like this :

<code>
private static String baseDecrypt(String dataEncrypted, String key)
{
String decryptedPwd = new String();
try
{
MessageDigest md = MessageDigest.getInstance("MD5", "BC");
byte buf[] = key.getBytes();
byte bufEncrypted[] = DBUtil.hexStringToByteArray(dataEncrypted);
md.update(buf);
byte hash[] = md.digest();
KeyParameter keyParambc = new KeyParameter(hash);
RC4Engine rc4Engine = new RC4Engine();
rc4Engine.init(false, keyParambc);
byte bufDecrypted[] = new byte[bufEncrypted.length];
rc4Engine.processBytes(bufEncrypted, 0, bufEncrypted.length, bufDecrypted, 0);
rc4Engine.reset();
decryptedPwd = new String(bufDecrypted);
}
catch(Exception e)
{
}
return decryptedPwd;
}
</code>
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I'm having problems

What kind of problems? Are you getting exceptions?
 
Franck McGeough
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the C++ code generates an encrypted string that is different from the bouncycastle given the same input string and key. The MD5 hash between the two code bases are identical. I'm not sure whether the cipher text (session key) produced by :

<code>
KeyParameter keyParambc = new KeyParameter(hash);
</code>

in Java or :

<code>
CryptDeriveKey(hProv, CALG_RC4, hHash, 0, &hKey);
</code>

in C++ are actually producing something that is the same length. I think that may be the problem. In the Java code I can do :

<code>
keyParambc.getKey().length;
</code>

to determine the length of the cipher text but I can't figure out how to do the same thing via Microsoft's Crypto API. I'm assuming that this is the issue but, in any case, the C++ code generates an encrypted string that is different from the bouncycastle given the same input string and key.
 
Franck McGeough
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the solution was certainly odd. I'm not sure why but the Microsoft CryptoAPI discards a number of the bytes in the hash. If I do the following in the Java code :



That is, just use the first 5 bytes of the generated hash and zero out the remainder then I get the same encrypted string in Java that I get in the C++ code. I need to test some more to verify but that appears to work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic