当前位置: 首页 > 知识库问答 >
问题:

PBEWithSHAAnd128BitRC4的java实现

纪俊良
2023-03-14

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

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).
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");
    }

多谢了。

共有1个答案

周龙光
2023-03-14

安全是一个移动的目标。防什么,防多长时间。如果您正在加密一小时后没有价值的事务数据,几乎任何事情都可以。如果你需要保持一些东西的安全很长时间,你想要一个长的密钥为你的PK系统,越长越好。但是您确实在密钥生成和某些类型的流加密/解密上付出了代价。

加密系统的头号故障不是算法本身,而是系统的实现,通常是如何生成或存储密钥。这就是说,河豚鱼和AES都被很好地考虑,当适当地实施时,应该是您所需要的一切。我再怎么推荐http://www.schneier.com/也不为过。应用密码学是一个有点过时,大约10年,但它是一个令人信服的解释领域,专门针对程序员。而他的博客是一个丰富的信息。如果你需要更多关于算法的细节,去那里搜索。在java实现中不会有很大的帮助,但是您可以在这里得到它。

 类似资料:
  • 问题内容: 标准api不包含AtomicBitSet实现。我可以将自己放在AtomicIntegerArray之上,但也不要这样做。 是否有人知道根据与Apache 2兼容的许可证发布的现有实现?我只需要基本操作即可设置和检查位。 编辑: 代码是性能和内存的关键,因此我想避免同步或每个标志使用整数(如果可能)。 问题答案: 我将使用AtomicIntegerArray,并且每个整数将使用32个标志

  • 问题内容: SCIM是Google,Salesforce,Ping Identity..etc等提出的用于用户配置的新标准。是否存在现有的Java实现来支持此功能? 问题答案: WSO2 Charon是另一个基于Java的SCIM实现。 http://www.slideshare.net/HasiniG/wso2-charon

  • 我是新来的,所以请原谅我的小错误。我目前正在做一个我的小项目,看到我处理长度在四万或更多的数字。 我目前正在使用BigInteger来处理这些值,我需要一些执行速度更快的东西。我读到BigInteger在其实现中使用了一个整数数组,我需要知道的是BigInteger是使用这个数组中的每个索引来表示每个小数点,就像1-9一样,还是使用了更有效的方法。 我这么问是因为我已经想到了一个使用位操作的实现,

  • 标准api不包括原子位集实现。我可以在AtomicIntegerArray上滚动我自己的,但我不太喜欢。 有人知道在与Apache 2兼容的许可下发布的现有实现吗?我只需要基本操作来设置和检查位。 编辑: 代码对性能和内存都很关键,所以如果可能的话,我希望避免同步或每个标志一个整数。

  • 我是Qpid的新手,我正在尝试设置环境。在阅读了大量文档之后,我设法下载了Apache Qpid并在localhost:8080中运行它 我还安装了maven、cmake。现在我正在尝试在Eclipse中启动一个新的maven项目以发送一条简单的消息。我根本不熟悉API接口和概念。我必须在我的项目解决方案中包含任何类型的jar吗?或者我如何使用服务器? 我也找到了这个https://github.

  • 问题内容: 是否有针对JAX-WS RI,Axis2,CXF或其他工具包的WS-Discovery规范的任何实现? 问题答案: 我知道的唯一Java实现是以下一种:http : //code.google.com/p/java-ws-discovery/ Wiki中有JAX-WS示例。