gpt4 book ai didi

java - PBEWithSHAAnd128BitRC4在java中的实现

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

我正在使用IBM JRE,我想为我的密码实现PBEWithSHAAnd128BitRC4算法,所以我应该为我的SecretKeyFactory和SecretKeySpec使用哪种算法,下面是我从IBMJCE提供商的provider.getInfo()方法获得的支持算法的 key .

Cipher algorithms                  : Blowfish, AES, DES, TripleDES, PBEWithMD2AndDES, 
PBEWithMD2AndTripleDES, PBEWithMD2AndRC2,
PBEWithMD5AndDES, PBEWithMD5AndTripleDES,
PBEWithMD5AndRC2, PBEWithSHA1AndDES
PBEWithSHA1AndTripleDES, PBEWithSHA1AndRC2
PBEWithSHAAnd40BitRC2, PBEWithSHAAnd128BitRC2
PBEWithSHAAnd40BitRC4, PBEWithSHAAnd128BitRC4
PBEWithSHAAnd2KeyTripleDES, PBEWithSHAAnd3KeyTripleDES
Mars, RC2, RC4, ARCFOUR
RSA, Seal
Key agreement algorithm : DiffieHellman
Key (pair) generator : Blowfish, DiffieHellman, DSA, AES, DES, TripleDES, HmacMD5,
HmacSHA1, Mars, RC2, RC4, RSA, Seal, ARCFOUR
Algorithm parameter generator : DiffieHellman, DSA
Algorithm parameter : Blowfish, DiffieHellman, AES, DES, TripleDES, DSA, Mars,
PBEwithMD5AndDES, RC2
Key factory : DiffieHellman, DSA, RSA
Secret key factory : Blowfish, AES, DES, TripleDES, Mars, RC2, RC4, Seal, ARCFOUR
PKCS5Key, PBKDF1 and PBKDF2(PKCS5Derived Key).

下面是我的代码,它给出了 java.security.InvalidKeyException: 缺少密码异常。

Decrypter(String passPhrase) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount, keyStrength);
SecretKey tmp = factory.generateSecret(spec);
key = new SecretKeySpec(tmp.getEncoded(), "RC4");
dcipher = Cipher.getInstance("PBEWithSHAAnd128BitRC4");
}

public String encrypt(String data) throws Exception {
dcipher.init(Cipher.ENCRYPT_MODE, key);
AlgorithmParameters params = dcipher.getParameters();
System.out.println("getAlgorithm : "+params.getAlgorithm());
iv = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] utf8EncryptedData = dcipher.doFinal(data.getBytes());
String base64EncryptedData = new sun.misc.BASE64Encoder().encodeBuffer(utf8EncryptedData);
System.out.println("IV " + new sun.misc.BASE64Encoder().encodeBuffer(iv));
System.out.println("Encrypted Data " + base64EncryptedData);
return base64EncryptedData;
}

public String decrypt(String base64EncryptedData) throws Exception {
dcipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] decryptedData = new sun.misc.BASE64Decoder().decodeBuffer(base64EncryptedData);
byte[] utf8 = dcipher.doFinal(decryptedData);
return new String(utf8, "UTF8");
}

还有一个问题,默认 java 提供程序中哪种算法最安全,因为我不能使用像 BouncyCaSTLeProvider 这样的第三方?

谢谢。

最佳答案

安全是一个不断变化的目标。防范什么以及持续多久。如果您要加密一小时后没有值(value)的交易数据,那么几乎任何事情都可以。如果您需要长期保护某些东西的安全,您的 PK 系统需要一个长 key ,越长越好。但您确实要为 key 生成和某些类型的流加密/解密付出代价。

加密系统的首要失败不是算法本身,而是系统的实现,通常是 key 的生成或存储方式。也就是说,Blowfish 和 AES 都广受好评,如果实现得当,应该可以满足您的一切需求。我不能推荐http://www.schneier.com/足够高。应用密码学有点过时,大约有 10 年左右的时间,但它是专门针对程序员的领域的令人信服的解释。他的博客信息丰富。如果您需要有关算法的更多详细信息,请前往那里进行搜索。在 java 实现中不会有太多帮助,但你可以在 SO 上得到它。

关于java - PBEWithSHAAnd128BitRC4在java中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16085467/

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