I am using blowfish algorithm to encrypt images and decrypt images. My programs works without any problem but i am trying to do a minor modification to it. Currently the secret key which is generated is only temporarily saved in the secret key variable but i want to save it permanently to derby data base so that i can use it in the future.
My questions are What is the type of data the column should be in derby to save the secret key ?(ie:- big integer, varchar ,etc) Can i directly save it to the database ?
Thank you.
Below is the code where i generate my secret key.
public FunctionClass() {
try {
keyGenerator = KeyGenerator.getInstance("Blowfish");
secretKey = keyGenerator.generateKey();
cipher = Cipher.getInstance("Blowfish");
} catch (NoSuchPaddingException ex) {
System.out.println(ex);
} catch (NoSuchAlgorithmException ex) {
System.out.println(ex);
}
}
You could encode the secret key into a String and store the string using an appropriate data type like Varchar
String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());
The key can be reconstructed as follows:
// decode the base64 encoded string
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
// rebuild key using SecretKeySpec
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, keyGenerator.getAlgorithm());