gpt4 book ai didi

Java加密: Loading a symmetric key from file

转载 作者:行者123 更新时间:2023-12-01 10:41:10 25 4
gpt4 key购买 nike

最近我开始从事加密工作。我有一个功能正常的非对称加密类,但我还需要一个对称 key 类。尽管对称 key 类的大多数方面都可以正常工作,但从其编码字节加载 key 却无法正常工作。以下是对称 key 类。我已经标记了两个不起作用的构造函数。

public class PlasmaSymmetricEncrypter {
public static String DESEDE_ALGORITHM = "DESede";
public static String AES_ALGORITHM = "AES";

private Key key;
private String algorithm;


public PlasmaSymmetricEncrypter(Key key) {
this.key = key;
this.algorithm = key.getAlgorithm();
}

public PlasmaSymmetricEncrypter(File keyFile, String algorithm) throws IOException, NoSuchAlgorithmException { //This constructor is not working
this.algorithm = algorithm;
if(!keyFile.exists()) {
this.genKey(keyFile);
}

Key key = new SecretKeySpec(Files.readAllBytes(keyFile.toPath()), algorithm);
this.key = key;
}

public PlasmaSymmetricEncrypter(byte[] key, String algorithm) { //This constructor is not working
this(new SecretKeySpec(key, algorithm));
}

private void genKey(File file) throws NoSuchAlgorithmException, IOException {
KeyGenerator generator = KeyGenerator.getInstance(this.algorithm);
this.key = generator.generateKey();
file.delete();
if(file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
file.createNewFile();
FileOutputStream stream = new FileOutputStream(file);
stream.write(this.getEncodedKey());
stream.close();
}

public byte[] getEncodedKey() {
return this.key.getEncoded();
}

public byte[] encrypt(byte[] bytes) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
Cipher cipher = Cipher.getInstance(this.algorithm);
cipher.init(Cipher.ENCRYPT_MODE, this.key);
return cipher.doFinal(bytes);
}

public byte[] encrypt(String text) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
return this.encrypt(text.getBytes(StandardCharsets.UTF_8));
}

public String decrypt(byte[] bytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(this.algorithm);
cipher.init(Cipher.DECRYPT_MODE, this.key);
return new String(cipher.doFinal(bytes), StandardCharsets.UTF_8);
}

public String decrypt(String bytes) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
return this.decrypt(bytes.getBytes(StandardCharsets.ISO_8859_1));
}

public String encryptToString(String text) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
return new String(this.encrypt(text), StandardCharsets.ISO_8859_1);
}

}

使用以下测试代码运行会产生 java.security.InvalidKeyException:

    PlasmaAsymmetricEncrypter asymmetricEncrypter = new PlasmaAsymmetricEncrypter(new File("private.key"), new File("public.key"), PlasmaAsymmetricEncrypter.RSA_ALGORITHM);
PlasmaSymmetricEncrypter symmetricEncrypter = new PlasmaSymmetricEncrypter(new File("secret.key"), PlasmaSymmetricEncrypter.AES_ALGORITHM);
String encrypt = "hello world";
byte[] encryptedKey = asymmetricEncrypter.encryptPrivate(symmetricEncrypter.getEncodedKey());
PlasmaSymmetricEncrypter encrypter = new PlasmaSymmetricEncrypter(asymmetricEncrypter.decryptPublic(encryptedKey), PlasmaAsymmetricEncrypter.RSA_ALGORITHM);
System.out.println(encrypter.decrypt(symmetricEncrypter.encrypt(encrypt)));

异常(exception):

Exception in thread "main" java.security.InvalidKeyException: No installed provider supports this key: javax.crypto.spec.SecretKeySpec
at javax.crypto.Cipher.chooseProvider(Cipher.java:893)
at javax.crypto.Cipher.init(Cipher.java:1249)
at javax.crypto.Cipher.init(Cipher.java:1186)
at com.gmail.socraticphoenix.plasma.file.encryption.PlasmaSymmetricEncrypter.decrypt(PlasmaSymmetricEncrypter.java:96)
at com.gmail.socraticphoenix.plasma.Test.main(Test.java:38)

测试 38 是 System.out.println 调用,PlasmaSymmetricEncrypter 96 是 here

我所有的代码都在 github

为了清晰起见,进行了编辑:1. 请忽略加密字符串方法,一旦我解决了这个问题,它们将被替换为base64编码。2. 该错误不会发生在构造函数本身中,仅当我尝试获取重构 key 的密码时才会抛出该错误。

最佳答案

原来我是傻子。

在我的测试代码中,我构建了 SymmetricEncrypter,如下所示:

PlasmaSymmetricEncrypter encrypter = new PlasmaSymmetricEncrypter(asymmetricEncrypter.decryptPublic(encryptedKey), PlasmaAsymmetricEncrypter.RSA_ALGORITHM);

使用 RSA 算法,当然该算法不适用于对称 key 。

感谢那些试图提供帮助的人,但最终失败的是我的测试代码,而不是类(class)。

关于Java加密: Loading a symmetric key from file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34407131/

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