2015-02-24 55 views
0

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

我的问题是 什么是数据的列应该是在德比保存密钥类型?(即: - 大整数,VARCHAR等) 我可以直接将它保存到数据库?

谢谢。

下面是我生成我的密钥的代码。

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

回答

1

你可以编码密钥为一个字符串,并使用适当的数据类型字段类似于VARCHAR

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

的关键存储字符串可以被重建如下:

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