我已经发布了这个寻找帮助,因为我在Android应用程序中使用它来使用Java进行加密和解密。我得到加密工作,但我仍然无法解密字符串。有任何想法吗????代码下面是我得到的错误。
import com.sun.org.apache.xml.internal.security.utils.Base64;
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;
private final String characterEncoding = "UTF-8";
private final String cipherTransformation = "AES/CBC/PKCS5Padding";
private final String aesEncryptionAlgorithm = "AES";
public String decrypt(String plainTextString, String SecretKey) throws
KeyException,
GeneralSecurityException,
GeneralSecurityException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException,
IOException{
byte[] cipheredBytes = Base64.decode(plainTextString, Base64.BASE64DEFAULTLENGTH);
byte[] keyBytes = getKeyBytes(SecretKey);
return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding);
}
public byte[] decrypt(byte[] cipherText, byte[] key, byte [] initialVector) throws
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance(cipherTransformation);
SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
cipherText = cipher.doFinal(cipherText);
return cipherText;
}
public byte[] encrypt(byte[] plainText, byte[] key, byte [] initialVector) throws
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException {
Cipher cipher = Cipher.getInstance(cipherTransformation);
SecretKeySpec secretKeySpec = new SecretKeySpec(key, aesEncryptionAlgorithm);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
plainText = cipher.doFinal(plainText);
return plainText;
}
private byte[] getKeyBytes(String key) throws UnsupportedEncodingException{
byte[] keyBytes= new byte[16];
byte[] parameterKeyBytes= key.getBytes(characterEncoding);
System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length));
return keyBytes;
}
public String encrypt(String plainText, String key) throws
UnsupportedEncodingException,
InvalidKeyException,
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException{
byte[] plainTextbytes = plainText.getBytes(characterEncoding);
byte[] keyBytes = getKeyBytes(key);
return Base64.encode(encrypt(plainTextbytes,keyBytes, keyBytes));
}
NETBEANS错误:
no suitable method found for decode(String,int)
method Base64.decode(InputStream,OutputStream) is not applicable
(actual argument String cannot be converted to InputStream by method invocation conversion)
method Base64.decode(byte[],OutputStream,int) is not applicable
(actual and formal argument lists differ in length)
method Base64.decode(byte[],OutputStream) is not applicable
(actual argument String cannot be converted to byte[] by method invocation conversion)
method Base64.decode(String,OutputStream) is not applicable
(actual argument int cannot be converted to OutputStream by method invocation conversion)
method Base64.decode(String) is not applicable
(actual and formal argument lists differ in length)
method Base64.decode(BufferedReader) is not applicable
(actual and formal argument lists differ in length)
method Base64.decode(byte[]) is not applicable
(actual and formal argument lists differ in length)
method Base64.decode(Element) is not applicable
(actual and formal argument lists differ in length)
答案 0 :(得分:1)
编译器会告诉您问题所在。
no suitable method found for decode(String,int)
你不能这样做
byte[] cipheredBytes = Base64.decode(plainTextString, Base64.BASE64DEFAULTLENGTH);
因为它与Base64
您不应该使用以com.sun.*
或sun.*
开头的类,因为它们是Oracle JRE的内部部分,并且不保证具有稳定的API,甚至不存在于所有JRE中。
相反,如果你想要一个内置类,你可以使用javax.xml.bind.DatatypeConverter
并调用静态方法DataTypeConverter.parseBase64Binary(String)
,传递Base64编码的String作为参数。返回值为byte[]
,这是您所需要的。