gpt4 book ai didi

java - 产生不同输出的字符串加密/解密类

转载 作者:行者123 更新时间:2023-11-30 09:40:21 25 4
gpt4 key购买 nike

我正在运行从互联网上获得的 Java 字符串加密/解密类!这是稍作修改的类(class):

公共(public)类 EncrypterDecrypter { 密码密码; 密码破译;

EncrypterDecrypter(SecretKey key)
{
try {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);

} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}
}

公共(public)类 EncryptionTester {

public static void main(String[] args)
{
try
{
//Generate a temporary key.
SecretKey key = KeyGenerator.getInstance("DES").generateKey();

//Create Encrypter/Decrypter class
EncrypterDecrypter crypto = new EncrypterDecrypter(key);

//More lines of code to use crypto object
}
catch (Exception e)
{
}
}

我的问题是,每次我创建 EncrypterDecrypter 类的新实例时,我都会得到不同的加密字符串,但要加密的字符串仍然相同!我的想法告诉我,问题是每次创建新实例时 SecretKey 对象都会不断变化,我想知道如何使加密器/解密器类的所有实例的 SecretKey 对象都相同,如果那是问题的原因!

最佳答案

如果您使用了 javax.crypto 包,那么加密和解密方法看起来没问题。尝试像这样生成您的 key :

final SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
final SecretKey key = skf.generateSecret(new DESKeySpec(new byte [] {/*The key*/}));
instance.EncrypterDecrypter(key); //Initialization of your Cipher objects
String encrypted = instance.encrypt("This is a test");
System.out.println(instance.decrypt(encrypted)); //"This is a test"

你应该在渔获物上做点什么。

关于java - 产生不同输出的字符串加密/解密类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461115/

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