在我的基于Java的web-app中,我希望在将一些数据写入数据库之前对其进行加密,并在将数据加载回内存后对其进行解密。为此,我使用了bouncycastle API并创建了一个如下所示的类:
public class BlowfishEnrypter implements IEncrypter {
/*--- Members ---*/
private BufferedBlockCipher cipher;
private KeyParameter key;
/*--- Constructors ---*/
/**
* Initialize the cryptographic engine. The key array should be at least 8
* bytes long.
*
* @param key
*/
public BlowfishEnrypter(byte[] key) {
cipher = new BufferedBlockCipher(new CBCBlockCipher(new BlowfishEngine()));
this.key = new KeyParameter(key);
}
/**
* Initialize the cryptographic engine. The key array should be at least 8
* bytes long.
*
* @param key
*/
public BlowfishEnrypter(String key) {
this(key.getBytes());
}
/*--- Public ---*/
/**
* {@inheritDoc}
*/
public String encrypt(String input) throws EncryptionException {
if (StringUtils.hasText(input)) {
byte[] bytes = Hex.decode(input);
try {
return new String(encrypt(bytes));
} catch (CryptoException e) {
throw new EncryptionException("Error occured while trying to encrypt", e);
}
} else {
throw new EncryptionException("Illegal argument for encryption: " + input);
}
}
/**
* {@inheritDoc}
*/
public String decrypt(String input) throws EncryptionException {
if (StringUtils.hasText(input)) {
byte[] bytes = Hex.decode(input);
try {
return new String(decrypt(bytes));
} catch (CryptoException e) {
throw new EncryptionException("Error occured while trying to decrypt", e);
}
} else {
throw new EncryptionException("Illegal argument for decryption: " + input);
}
}
/*--- Private ---*/
/**
* Encrypt arbitrary byte array, returning the encrypted data in a different
* byte array.
*
* @param data
* @return Encrypted byte array
* @throws CryptoException
*/
private synchronized byte[] encrypt(byte[] data) throws CryptoException {
if (data == null || data.length == 0) {
return new byte[0];
}
cipher.init(true, key);
return callCipher(data);
}
/**
* Decrypts arbitrary data
*
* @param data
* To decrypts
* @return Decrypted byte array
* @throws CryptoException
*/
private synchronized byte[] decrypt(byte[] data) throws CryptoException {
if (data == null || data.length == 0) {
return new byte[0];
}
cipher.init(false, key);
return callCipher(data);
}
/**
* Private routine that does the gritty work.
*
* @param data
* Data to operate on
* @return Processed byte array
* @throws CryptoException
*/
private byte[] callCipher(byte[] data) throws CryptoException {
int size = cipher.getOutputSize(data.length);
byte[] result = new byte[size];
int olen = cipher.processBytes(data, 0, data.length, result, 0);
olen += cipher.doFinal(result, olen);
if (olen < size) {
byte[] tmp = new byte[olen];
System.arraycopy(result, 0, tmp, 0, olen);
result = tmp;
}
return result;
}
}
到目前为止一直很好(我想是的,如果你对这个课程有任何意见,请继续)。要初始化这个类,我应该提供一个密钥。我的问题是 - 我该如何管理这个密钥?
更具体一点:
答案 0 :(得分:1)
取决于许多事情:
不确定为什么要加密实际的密钥,因为您将把密钥放在哪里以保护原始密钥?
更好的做法是查看Java SE security以查看Java Keystore可以为您做些什么。此外,您还可以阅读bouncy castle's网站上可能有用的资源。
答案 1 :(得分:1)
我们对许多网络应用使用了类似的加密技术。通常,密钥在源代码中的属性文件中保存为字符串。密钥未加密,其字符串包含特殊字符和其他字符串组合,以使其更强(数字,大写等)。一旦生活,密钥通常会被业务用户在6个月内改变一次..