我正在使用IBM JRE,我想为我的密码实现PBEWithSHAAnd128BitRC4算法,因此我应该为SecretKeyFactory和SecretKeySpec使用哪个算法,下面是从IBMJCE提供者的provider.getInfo()方法获得的支持算法的秘密密钥。

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之类的第三方?

谢谢 。

最佳答案

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

加密系统的头号失败不是算法本身,而是系统的实现,通常是如何生成或存储密钥。就是说,Blowfish和AES都得到了很好的考虑,正确实施时应该是您所需要的一切。我不能高度推荐http://www.schneier.com/。应用密码学的时间大约是10年左右,但这是专门针对程序员的对该领域的有力解释。他的博客内容丰富。去那里搜索是否需要有关算法的更多详细信息。在Java实现中不会提供很多帮助,但是您可以在SO上获得帮助。

07-24 09:38
查看更多