SecretKeySpec clientKey = new SecretKeySpec(kbytes, "Blowfish");
System.out.println("clientKey");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, clientKey);
SealedObject msg = new SealedObject("This is a secret", cipher);
// Now use the server key to decrypt the msg
byte[] skbytes = server.session.getSessionKey();
SecretKeySpec serverKey = new SecretKeySpec(skbytes, "Blowfish");
Cipher scipher = Cipher.getInstance("Blowfish");
scipher.init(Cipher.DECRYPT_MODE, serverKey);
String theMsg = (String) msg.getObject(scipher);
System.out.println("Decrypted: "+theMsg);
// Try a key that should fail
KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
kgen.init(320);
SecretKey key = kgen.generateKey();
cipher.init(Cipher.DECRYPT_MODE, key);
try
{
String tmp = (String) msg.getObject(cipher);
throw new IllegalArgumentException("Should have failed to decrypt the msg");
}
catch(Exception e)
{
System.out.println("Arbitrary key failed as expected");