-1

自从我在 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)
4

1 回答 1

1

编译器会告诉您问题所在。

no suitable method found for decode(String,int)

你不能这样做

byte[] cipheredBytes = Base64.decode(plainTextString, Base64.BASE64DEFAULTLENGTH);

因为它不匹配任何方法签名Base64

您不应该使用以 Oracle JRE 的内部部分开头的类,com.sun.*并且sun.*不能保证它们具有稳定的 API,甚至不保证存在于所有 JRE 中。

相反,如果您想要一个内置类,您可以使用javax.xml.bind.DatatypeConverter并调用静态方法DataTypeConverter.parseBase64Binary(String),将 Base64 编码的字符串作为参数传递。返回值将byte[]是您需要的。

于 2013-09-12T20:42:51.233 回答