简体   繁体   中英

Saving Secret key to derby datase

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()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM