問題描述
有人讓我用 PHP 解密一個用以下 Java 類加密的字符串.
Someone asked me to decrypt with PHP a string encrypted with the following Java Class.
public class CryptoLibrary {
private Cipher encryptCipher;
private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
public CryptoLibrary() throws SecurityException{
java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());
char[] pass = "NNSHHETJKKSNKH".toCharArray();
byte[] salt = {
(byte) 0xa3, (byte) 0x21, (byte) 0x24, (byte) 0x2c,
(byte) 0xf2, (byte) 0xd2, (byte) 0x3e, (byte) 0x19 };
init(pass, salt, iterations);
}
public void init(char[] pass, byte[] salt, int iterations)throws SecurityException{
PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass));
encryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, k, ps);
}
}
public synchronized String encrypt(String str) throws SecurityException{
if(str!=null){
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = encryptCipher.doFinal(utf8);
return encoder.encode(enc);
}
else {
return null;
}
}
}
我不懂任何 Java,所以我需要一些幫助來理解這種加密.
I don't know any Java so I need some help to understand this encryption.
1) 這條線是什么意思?PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt,20);
1) what is the meaning of this line?
PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt,20);
2) 我應(yīng)該為第一個參數(shù)使用什么值string mcrypt_encrypt(string $cipher, string $key, string $data, string $mode [, string $iv])
2) what value should I use for the first parameter of
string mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] )
3) 我什么時候應(yīng)該在我的 php 腳本中使用 MD5?
3) When should I use MD5 in my php script?
推薦答案
1) 創(chuàng)建基于密碼的加密的參數(shù),包含在哈希計算中的鹽,以及哈希方法執(zhí)行的迭代次數(shù)(在它自己的輸出上).它用于擊敗彩虹表攻擊,基本上攻擊者必須經(jīng)過相同次數(shù)的迭代來檢查密碼是否正確,并且他不能使用預(yù)先計算的表,因為每個密碼的鹽都會不同(所以你看不到如果某人與另一個用戶的密碼相同).
1) It creates the parameters for Password Based Encryption, the salt, which is included in the hash calculations, and the number of iterations that the hash method is executed (on it's own output). It is used to defeat rainbow table attacks, basically an attacker has to go through the same number of iterations to check if the password is correct, and he cannot use a precalculated table because the salt will be different for each password (so you cannot see if somebody has the same password as another user).
2) MCRYPT_DES,你需要 MCRYPT_MODE_CBC 作為模式,當(dāng)然還有 PKCS#5 填充.
2) MCRYPT_DES, and you will need MCRYPT_MODE_CBC for the mode, and PKCS#5 padding of course.
3) 僅當(dāng)您絕對確定其弱點未暴露或絕對需要兼容性時.幸運的是,它對于密鑰派生功能相對安全.下載 PHP 的 pbkdf1 方法并將其放入其中 - 如果尚未包含.
3) Only when you are absolutely sure that its weaknesses are not exposed or when absolutely required for compatibility. Fortunately, it is relatively secure for key derivation functions. Download a pbkdf1 method for PHP and put it in there - if not already included.
這篇關(guān)于解密(使用 PHP)Java 加密(PBEWithMD5AndDES)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!