gpt4 book ai didi

java - Java Encrypter 的 key 处理

转载 作者:行者123 更新时间:2023-11-30 09:32:50 24 4
gpt4 key购买 nike

在我基于 Java 的网络应用程序中,我想在将一些数据写入数据库之前对其进行加密,并在将其加载回内存后对其进行解密。为此,我使用了 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;
}
}

到目前为止一切顺利(我认为是这样,如果您对本类(class)有任何意见,请继续)。要初始化此类,我应该提供一个 key 。我的问题是 - 我应该如何管理这个 key ?

更具体地说:

  • 我应该使用特定技术来创建它吗?
  • 我应该把它存放在哪里?在属性文件中?在数据库中?在我的代码中的某处?
  • 这个 key (我们在这里讨论的是字符串,对吗?)是否应该加密,然后在加载或使用后解密?如果是这样,我应该怎么做?

最佳答案

取决于很多事情:

  1. 您的代码是否存储在其他人无法查看/访问的安全位置?如果是这样,我会把它放在代码中。
  2. 您是否打算不时更改 key (这样更安全)?如果是这样并且第 1 点仍然成立,则将其粘贴到属性文件中。
  3. 否则将其放入数据库,并对数据库的用户名/密码保密!

但不确定您为什么要加密实际 key ,因为您会将 key 放在哪里以保护原始 key ?

但更好的做法是查看 Java SE security查看 Java Keystore 等可以为您做什么。此外,您还可以阅读有关 bouncy castle's 的资源。可能有帮助的网站。

关于java - Java Encrypter 的 key 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com