1

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

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: Missing password 异常。

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

还有一个问题,因为我不能使用像 BouncyCastleProvider 这样的第三方,所以默认 java 提供程序中最安全的算法是什么?

谢谢 。

4

1 回答 1

0

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

加密系统的第一个失败不是算法本身,而是系统的实现,通常是密钥是如何生成或存储的。也就是说,Blowfish 和 AES 都备受推崇,如果实施得当,应该就是您所需要的一切。我不能高度推荐http://www.schneier.com/ 。应用密码学有点过时,大约有 10 年的历史,但它是专门针对程序员的领域的有力解释。他的博客信息丰富。如果您需要有关算法的更多详细信息,请前往那里搜索。在 java 实现中不会有很多帮助,但你可以在 SO 上得到它。

于 2013-04-18T14:42:31.463 回答