2013-04-18 29 views
1

我正在使用IBM JRE,並且我想爲我的密碼實現PBEWithSHAAnd128BitRC4 algorothm,所以這個算法應該用於我的SecretKeyFactory和SecretKeySpec,下面是支持algos的密鑰,我從provider.getInfo() IBMJCE提供程序的方法。java中的PBEWithSHAAnd128BitRC4實現

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

一個問題,這是最安全的算法amoung默認的Java供應商,因爲我不能用THRID黨像BouncyCastleProvider?

謝謝。

回答

0

安全是一個移動的目標。抵禦什麼和多久。如果您在加密一小時後沒有任何價值的交易數據,則幾乎所有事情都會執行。如果您需要長時間保持安全,您需要PK系統的長鑰匙,時間越長越好。但是你真的爲密鑰生成和某些類型的流加密/解密付出了代價。

加密系統的頭號故障不是算法本身,而是系統的實現,通常是如何生成或存儲密鑰。也就是說,Blowfish和AES都受到好評,正確實施應該是您需要的一切。我不能推薦http://www.schneier.com/夠高。應用密碼學有點過時了10年左右,但它是專門面向程序員的領域的一個有說服力的解釋。他的博客是豐富的信息。去那裏搜索是否需要關於算法的更多細節。在java實現中不會有很多幫助,但是你可以在這裏得到它。