File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes Converting C++ Windows Crypto API to Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Converting C++ Windows Crypto API to Java" Watch "Converting C++ Windows Crypto API to Java" New topic
Author

Converting C++ Windows Crypto API to Java

Franck McGeough
Greenhorn

Joined: Oct 27, 2005
Posts: 26
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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
but I'm having problems

What kind of problems? Are you getting exceptions?


Ping & DNS - updated with new look and Ping home screen widget
Franck McGeough
Greenhorn

Joined: Oct 27, 2005
Posts: 26
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

Joined: Oct 27, 2005
Posts: 26
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting C++ Windows Crypto API to Java
 
Similar Threads
reading binary file in java(with code also)
creating a link
generate Unique key
Encrypt in Java, Decrypt in IBM DataPower
AES decryption - InvalidKeyException: Parameters missing