javaaes-128加密解密

javaaes-128加密解密,java,encryption,aes,Java,Encryption,Aes,我发布这篇文章是为了寻求帮助,让它在Java中进行加密和解密,因为我在Android应用程序中使用了它。我得到了加密工作,但我仍然无法得到这个解密字符串。有什么想法吗????代码下面是我得到的错误 import com.sun.org.apache.xml.internal.security.utils.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto

我发布这篇文章是为了寻求帮助,让它在Java中进行加密和解密,因为我在Android应用程序中使用了它。我得到了加密工作,但我仍然无法得到这个解密字符串。有什么想法吗????代码下面是我得到的错误

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)

编译器会告诉您问题所在

未找到适合解码的方法(字符串,int)

你不能这么做

byte[] cipheredBytes = Base64.decode(plainTextString, Base64.BASE64DEFAULTLENGTH);
因为它与
Base64上的任何方法签名都不匹配

您不应该使用以
com.sun.*
sun.*
开头的类,因为它们是Oracle JRE的内部部分,不保证有稳定的API,甚至不保证在所有JRE中都存在


相反,如果需要内置类,可以使用并调用静态方法,将Base64编码的字符串作为参数传递。返回值将是一个
字节[]
,这是您需要的。

这里有什么问题吗?是的。我想我并没有把它放在那个格式,但我正试图让它在Java中工作。上面代码中的两行注释是我对代码所做的更改,目的是让它从我的Android应用程序在Java中工作。我没有运行它,因为我在netbeans中遇到了一个简单的“找不到符号”错误。我知道编译器在告诉我什么。我不知道的是我需要做什么才能让它工作。它在我的Android应用程序中运行良好。使用Rijndael的类似版本在我的VB.net和C#.net工作站应用程序中工作,但在这里我迷路了。我遗漏了一些东西。@TyJacobs编译错误与密码无关。简单地说,
Base64。解码(String,int)
不存在。您不能调用不存在的方法。@TyJacobs如果您可以发布您正在使用的
Base64
类的完全限定类名,这将非常有用。(即您使用的
Base64
类)将代码更新为包含类并加密代码。周一我刚开始使用Java,而不是Android。这很容易,但这是踢我的屁股。我已经在网上、我的其他应用程序之间以及其他几个论坛上查找过,但就我的一生而言,我看不到我遗漏了什么。@TyJacobs我更新了我的答案,提供了有关您代码的新信息。