|
-
December 12th, 2010, 02:21 PM
#1
DHKeyExchange/Blowfish - unexpected decryption outcome
I am writing a proxy that analyzes packets passed between a client and server. For simplicity, i have limited the scope of this question to the interaction between a client and server. My program can be seen as the server in this context.
Task description: a test server that handles these operations
1. on client connection:
-generate P,G, server DHPrivateKey, DHPublicKey
-generate blowfish IV
-send to client P,G, serverDHPublicKey, IV
2. on client first reply
-extract out client DH public key
-complete DH key agreement -> obtains client/server shared key
-set shared key as the blowfish key
3. on client 2nd reply
-decrypt client's msg using blowfish cipher
Task complete.
Initially i have had a problem initializing my blowfish cipher using the shared key that i have obtained. An runtime exception was thrown; InvalidKeyException - Illegal Key Size. I managed to get past that by switching to unlimited policy files.
Now that i can initialize the cipher, i tried to decrypt client's 2nd reply. The decryption carried out without any exception, but the outcome was not what i expected - in fact nonsensical.
Immediately it became clear that this "Shared key" that i have obtained isn't really shared between client and server.
Knowing that the blowfish cipher actually worked, I am left with the hypothesis that DH key exchange was not carried out properly; or the shared key might not have been obtained properly.
These are the modified(only some variable names) codes that will fit in this context.
DH Exchange
Code:
public void DHKeyExchange(int mode) throws DHKeyExchangeException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
BigInteger p = new BigInteger(datapool.P, 16);
BigInteger g = new BigInteger(datapool.G, 16);
BigInteger PublicKey = new BigInteger(datapool.ClientPublicKey, 16);
BigInteger ServerPrivateKey = new BigInteger(datapool.ServerPrivateKey, 16);
DHPublicKeySpec pub = new DHPublicKeySpec(PublicKey, p, g);
DHPrivateKeySpec pri = new DHPrivateKeySpec(ServerPrivateKey, p, g);
/***************************************************************
* Using public key to generate PublicKey object
* and ServerPrivateKey to generate PrivateKey object
***************************************************************/
KeyFactory kfac = KeyFactory.getInstance("DiffieHellman");
PublicKey PubKey = kfac.generatePublic(pub);
PrivateKey PrivKey = kfac.generatePrivate(pri);
/******************************
* initialize agreement object
*******************************/
KeyAgreement agreement = KeyAgreement.getInstance("DiffieHellman");
//initialized agreement object with private key.
agreement.init(PrivKey);
/*******************************
* Complete the DH key exchange
******************************/
agreement.doPhase(PubKey, true);
datapool.SharedKey = agreement.generateSecret("Blowfish"); // a SecretKey object was generated
//i am not sure if i should have simply obtained the secret key as bytes, but by doing that i need to go through the hassle of converting it back to a key.
}
Blowfish Cipher
Code:
blowfish = Cipher.getInstance("Blowfish/CFB64/NoPadding");
blowfish.init(Cipher.DECRYPT_MODE, datapool.SharedKey, IV);
byte[] decryptedPacket = blowfish.doFinal(encryptedPacket);
What did i do wrong to result in this error?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|