java - 将 key 保存到 derby 数据库

标签 java database derby blowfish secret-key

我正在使用河豚算法来加密图像和解密图像。我的程序运行没有任何问题,但我正在尝试对其进行微小的修改。目前生成的 key 仅临时保存在 key 变量中,但我想将其永久保存到 derby 数据库中,以便将来使用它。

我的问题是 derby 中保存 key 的列应采用什么类型的数据?(即:大整数、varchar 等) 我可以直接将其保存到数据库吗?

谢谢。

下面是我生成 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);  
       }  
}

最佳答案

您可以将 key 编码为字符串,并使用适当的数据类型(例如 Varchar)存储该字符串

String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());

key 可以按如下方式重建:

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

关于java - 将 key 保存到 derby 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28700464/

相关文章:

hadoop - 由 : ERROR XSDB6: Another instance of Derby may have already booted the database 引起

java - 有没有办法在不到 O(n) 的时间内连接 Java 字符串?

java - Elasticsearch : Boost Results with "Exact match" in "query_string" without specifying the field

database - JAXB、BigDecimal 还是 double?

java - ResultSet.get 抛出 java.sql.SQLException : Invalid operation at current cursor position

sql - SQL插入触发器

JavaFX 对话框

java - 我得到 "file not found: example.java"并将我的文件放在 jre/bin 中

database - 保持多个环境中的开发数据库同步

一个字段中有多个值的MySQL表