2013-06-26 44 views
1

我的紅寶石,做使用OpenSSL
但是加密有一箇舊的代碼,我想在Java翻譯這一點,我迷路了。 到目前爲止,我最大的障礙是找出如何基於這段代碼生成IV。 任何幫助,將不勝感激翻譯紅寶石加密碼的Java

def func_enc(data, key) 
     cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") 
     cipher.encrypt 
     cipher.pkcs5_keyivgen(key) 
     cipher.update(data) 
     encrypted_data << cipher.final 
     return encryptedData 
    end 

編輯
只是爲了澄清,我想使用Java加密此。這是我想出了迄今爲止代碼:

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithMD5And256AES-CBC"); 
    KeySpec spec = new PBEKeySpec("Password".toCharArray(), null, 2048, 256); 
    SecretKey tmp = factory.generateSecret(spec); 
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
    cipher.init(Cipher.ENCRYPT_MODE, secret); 
    AlgorithmParameters params = cipher.getParameters(); 
    byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); 

"PBKDF2WithMD5And256AES-CBC"沒有任何供應商,我得到NoSuchAlgorithm例外。

java.security.NoSuchAlgorithmException: PBKDF2WithMD5And256AES-CBC SecretKeyFactory not available 

也是pkcs5_keyivgen默認使用的鹽爲空!!我不確定Java是否讓我使用null鹽。

如何生成正確的IV?

+0

看看這個:http://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption – Aguardientico

+0

是否有某些原因,你不能利用現有的加密庫?重新使用現有的和經過良好測試的車輪要好得多。 –

+0

閱讀文檔:http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#SecretKeyFactory – rossum

回答

0

this documentation page警告表明,與AES一起使用時棄用pkcs5_keyivgen方法做了非標準。首先,它使用PBKDF1,而不是PBKDF2。

這可能是很難複製它做什麼,並實施加密算法通常是不可取的,除非你知道自己在做什麼 - 甚至專家經常搞錯。