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));
}
ネットビーンズ エラー:
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)