請在將此標記爲重複項之前,請先了解我無法在有關河豚的計算器上找到任何有用的信息。我正在嘗試使用Android Studio對blowfish進行加密和解密。我的加密似乎工作。但是,當我嘗試解密所述字符串時,它實際上更短並且用字符編碼。我對加密非常陌生,不勝感激。河豚加密問題
加密
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Encryption {
private String algorithm = "Blowfish/CBC/PKCS5Padding";
private SecretKeySpec keySpec;
private Cipher cipher;
public void setupForEncryption(String keyString) throws java.security.GeneralSecurityException, UnsupportedEncodingException {
byte[] keyData = keyString.getBytes();
keySpec = new SecretKeySpec(keyString.getBytes("UTF-8"), "Blowfish");
cipher = Cipher.getInstance(algorithm);
}
public SecretKeySpec getSecretKey() {
return keySpec;
}
public boolean checkForKeySpec() {
if (keySpec != null) {
return true;
}
return false;
}
public String encryptString(String inputString) throws java.security.GeneralSecurityException, UnsupportedEncodingException {
IvParameterSpec ivSpec = new IvParameterSpec(keySpec.getEncoded());
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(),ivSpec);
byte[] encryptedBytes = cipher.doFinal(inputString.getBytes("UTF-8"));
return new String(encryptedBytes);
}
解密
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Decryption {
private SecretKeySpec keySpec;
private String algorithm = "Blowfish/CBC/PKCS5Padding";
private Cipher cipher;
public void setupForDecryption(String key) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException {
byte[] keyData = key.getBytes();
keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "Blowfish");
cipher = Cipher.getInstance(algorithm);
}
public boolean checkForKeySpec() {
if(keySpec != null){
return true;
}
return false;
}
public SecretKeySpec getSecretKey() {
return keySpec;
}
public String decryptString(String inputString) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
IvParameterSpec ivSpec = new IvParameterSpec(keySpec.getEncoded());
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(),ivSpec);
byte[] decryptedBytes = Base64.decode(inputString,0);
String decrypted = new String(decryptedBytes);
return decrypted;
}
}
爲什麼要使用像Blowfish這樣十多年前的加密算法? –
河豚,整潔的名字,使用它十年前,舊的算法。目前的標準是AES(高級加密算法),名稱說明了一切。 – zaph