Java 从证书文件中获取密钥,用于AES加密和解密算法
我正在进行AES加密,在加密过程中,我将使用证书文件中的密钥初始化密码,如下所示 encryptModeCipher=Cipher.getInstance(“AES”); encryptModeCipher.init(Cipher.ENCRYPT_模式,aesSecretKey) 但我在这里看到的问题是,对于我使用的所有证书,我的secretKey()保持不变。有什么建议吗?为什么?并提出这样做的好主意 字节[]encryptionKey=Arrays.copyOf(编码,32); secretKey=新的SecretKeySpec(encryptionKey,算法) 公共类EncryptionServiceHelper{Java 从证书文件中获取密钥,用于AES加密和解密算法,java,aes,Java,Aes,我正在进行AES加密,在加密过程中,我将使用证书文件中的密钥初始化密码,如下所示 encryptModeCipher=Cipher.getInstance(“AES”); encryptModeCipher.init(Cipher.ENCRYPT_模式,aesSecretKey) 但我在这里看到的问题是,对于我使用的所有证书,我的secretKey()保持不变。有什么建议吗?为什么?并提出这样做的好主意 字节[]encryptionKey=Arrays.copyOf(编码,32); secret
private String algorithm = "AES";
private String certPass;
private SecretKey secretKey;
public SecretKey setKey() {
try {
certPass="****";
char[] pass = certPass.toCharArray();
KeyStore keyStore = KeyStore.getInstance("jceks");
File file = new File("D:/aws-kms-dps/***.jks");
InputStream inputStream = new FileInputStream(file);
keyStore.load(inputStream, pass);
Certificate cert = keyStore.getCertificate("****");
Key key = cert.getPublicKey();
secretKey = new SecretKeySpec(key.getEncoded(), algorithm);
byte[] encoded = secretKey.getEncoded();
byte[] encryptionKey = Arrays.copyOf(encoded, 32);
secretKey = new SecretKeySpec(encryptionKey, algorithm);
} catch (IOException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
return secretKey;
}
public static void main(String args[]){
AESEncryptionServiceHelper aesEncryptionServiceHelper=new AESEncryptionServiceHelper();
aesEncryptionServiceHelper.setKey();
}
}您似乎正在使用(部分)公钥作为AES密钥。这是一个非常糟糕的主意
- 公钥是。。好。。公共和静态
- 它的熵相对较低(因为在ASN.1格式中定义了多个字节)
// initialization vector
SecureRandom rnd = new SecureRandom();
byte[] iv = new byte[SYMMETRIC_BLOCK_SIZE / 8];
rnd.nextBytes(iv);
encryptionParams.setIv(iv);
IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
SecretKey symmetricKey = new SecretKeySpec(encryptionParams.getKey(), SYMMETRIC_KEY_ALG);
Cipher cipher = Cipher.getInstance(SYMMETRIC_CIPHER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, symmetricKey, ivParamSpec);
// for HMAC we should be able to use the same key as for encryption
// for CBC-MAC it may not be the case
// https://en.wikipedia.org/wiki/CBC-MAC#Using_the_same_key_for_encryption_and_authentication
Mac mac = Mac.getInstance(EncryptionTest.HASH_ALGORITHM_NAME);
mac.init(symmetricKey);
byte[] encrypted = cipher.doFinal(encryptionParams.getPlaintext());
encryptionParams.setCiphertext(encrypted);
byte[] authTag = mac.doFinal(encrypted);
encryptionParams.setMac(authTag);
您似乎正在使用(部分)公钥作为AES密钥。这是一个非常糟糕的主意
- 公钥是。。好。。公共和静态
- 它的熵相对较低(因为在ASN.1格式中定义了多个字节)
// initialization vector
SecureRandom rnd = new SecureRandom();
byte[] iv = new byte[SYMMETRIC_BLOCK_SIZE / 8];
rnd.nextBytes(iv);
encryptionParams.setIv(iv);
IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
SecretKey symmetricKey = new SecretKeySpec(encryptionParams.getKey(), SYMMETRIC_KEY_ALG);
Cipher cipher = Cipher.getInstance(SYMMETRIC_CIPHER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, symmetricKey, ivParamSpec);
// for HMAC we should be able to use the same key as for encryption
// for CBC-MAC it may not be the case
// https://en.wikipedia.org/wiki/CBC-MAC#Using_the_same_key_for_encryption_and_authentication
Mac mac = Mac.getInstance(EncryptionTest.HASH_ALGORITHM_NAME);
mac.init(symmetricKey);
byte[] encrypted = cipher.doFinal(encryptionParams.getPlaintext());
encryptionParams.setCiphertext(encrypted);
byte[] authTag = mac.doFinal(encrypted);
encryptionParams.setMac(authTag);
证书文件中没有密钥。根据定义,其中的所有内容都是公开的,包括用于构造所谓“密钥”的公钥。如果您使用公钥作为密钥,那么您已经处于完全的不安全状态。不清楚你在问什么,或者你在说什么。是的。我不太清楚这件事。。。我只能从证书中获取公钥。。。但不知怎的,我需要从中生成一个密钥。。给我一个好的建议不,你不需要这样做,因为这是不安全的。你不需要做错事。你可能误解了你的要求。将其发送回以进行澄清。证书文件中没有密钥。根据定义,其中的所有内容都是公开的,包括用于构造所谓“密钥”的公钥。如果您使用公钥作为密钥,那么您已经处于完全的不安全状态。不清楚你在问什么,或者你在说什么。是的。我不太清楚这件事。。。我只能从证书中获取公钥。。。但不知怎的,我需要从中生成一个密钥。。给我一个好的建议不,你不需要这样做,因为这是不安全的。你不需要做错事。你可能误解了你的要求。发回澄清。