它可以很容易解密,因爲它會使用DES(CBC)操作模式。 DES只有一個有效的密鑰大小爲56位。因此,無論(PBKDF1)密鑰派生如何,密鑰和強制都可能被強制。
MD5雖然被認爲是自行破解,但在PBKDF1中使用時不會出現問題 - 只要密碼中包含足夠的熵。
如果可能,您應該使用PBKDF2和AES升級到基於密碼的加密(PBE)。請注意,PBE通常使用CBC模式加密,因此它不適用於傳輸協議。
它是一個完整的任務,你只需要導入並使用它......
package com.example.siman.friend_pro;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import static javax.crypto.Cipher.DECRYPT_MODE;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static javax.crypto.Cipher.getInstance;
public class Encryptor4j
{
private static byte[] salt = {
(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
(byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
};
private static Cipher ecipher;
private static Cipher dcipher;
private static String Property = "youkey";
private static int iterationCount = 19;
public static String encrypt(String Text)
{
String returnvalue=null;
try {
returnvalue = Encryptor4j.form1(Text);
}
catch (NoSuchAlgorithmException | InvalidKeySpecException |
NoSuchPaddingException | InvalidKeyException |
InvalidAlgorithmParameterException | IllegalBlockSizeException |
BadPaddingException | IOException e) {
e.printStackTrace();
}
return returnvalue;
}
public static String decrypt(String Text)
{
String returnvalue=null;
try {
returnvalue = Encryptor4j.form2(Text);
}
catch (NoSuchAlgorithmException | InvalidKeySpecException |
NoSuchPaddingException | InvalidKeyException |
InvalidAlgorithmParameterException | IllegalBlockSizeException |
BadPaddingException | IOException e) {
e.printStackTrace();
}
return returnvalue;
}
private static String form1(String Text)
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException
{
//Key generation for enc and desc
KeySpec keySpec = new PBEKeySpec(Property.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
//Enc process
ecipher = getInstance(key.getAlgorithm());
ecipher.init(ENCRYPT_MODE, key, paramSpec);
String charSet = "UTF-8";
byte[] in = Text.getBytes(charSet);
byte[] out = ecipher.doFinal(in);
String encStr = new String(android.util.Base64.encode(out,0));
//String encStr = new String(Base64.getEncoder().encode(out));
return encStr;
}
private static String form2(String Text)
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, IOException
{
//Key generation for enc and desc
KeySpec keySpec = new PBEKeySpec(Property.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
//Decryption process; same key will be used for decr
dcipher = getInstance(key.getAlgorithm());
dcipher.init(DECRYPT_MODE, key, paramSpec);
//byte[] enc = Base64.getDecoder().decode(encryptedText);
byte[] enc = android.util.Base64.decode(Text.getBytes(),0);
byte[] utf8 = dcipher.doFinal(enc);
String charSet = "UTF-8";
String plainStr = new String(utf8, charSet);
return plainStr;
}
}
當然,一些/你的Android廠商可能會在未來的某個時候卸下PBEWithMD5AndDES。可以考慮使用DES,現在不應該使用MD5。不幸的是,這個答案並不是真正的答案,因爲我不認爲Google要求每個供應商保留PBEWithMD5AndDES。 –
任何人都可以肯定某些供應商將來會做什麼? –
@ArtjomB。 aha,我明白了,據我所知,使用Cipher並不「安全」,因爲每種算法都可以在將來被刪除? –