Saving SecretKey to database
843810Mar 8 2002 — edited Sep 23 2002 I need to encrypt user password with a key and store that key in the database along with the encrypted password. Every once in a while I need to generate a new encryption key. So far it works fine with the encryption/decryption routine for 3-4 tests, but every once in a while I get the BadPadding exception and when i look at the key stored in the database I see that the value is a bit different from what was generated. Can anyone please tell me why this might be happening?
I can post my code for new key generation and storing and retrieving it from the database.
Here is my method to get the new key that I will use for encryption and then store this key in the database column of varchar2(100) . What should I do different?
public String genEncryptionKey() {
try {
// Step 1.
// Get an instance of a key generator for DES
KeyGenerator kgen = KeyGenerator.getInstance("DES" );
kgen.init(56, new SecureRandom() );
// Step 2.
// Generate the key
SecretKey sKey = kgen.generateKey();
return new String( sKey.getEncoded());
}
catch( Exception e ) {
e.printStackTrace();
}
return null;
}
I am then storing this key in the database
when i do a printlln after this method to see the value of the key and compare it with what i get from the database they are slighlty different
Thanks