آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

مشکل در رمزنگاری با الگوریتم Aes

farzaneh___  8 سال پیش  8 سال پیش
0 0

من یه کدی دارم برا رمزگذاری و رمزگشایی داده تو اندروید. در کل این کد مشکلی نداره  و برای خیلی از داده ها درست اجرا میشه  اما برا بعضی داده ها یا به ارور میخوره یا catchاجرا میشه ممنون میشم راهنمایی کنین:

مثلا برا این داده:

SmQ0YMiq+SHn34m8h3gWw==

 این ارور رو میده: java.lang.IllegalArgumentException: bad base-64

یا برا این داده 

u8/OgSllm2agXlDrGcdqWg==

این catchاجرا میشه

 

private final String characterEncoding = "UTF-8";

private final String cipherTransformation = "AES/CBC/PKCS5Padding";

private final String aesEncryptionAlgorithm = "AES";

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.encodeToString(encrypt(plainTextbytes,keyBytes, keyBytes), Base64.DEFAULT);

}

 

public String decrypt(String encryptedText, String key) throws KeyException, GeneralSecurityException, GeneralSecurityException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {

byte[] cipheredBytes = Base64.decode(encryptedText, Base64.DEFAULT);

byte[] keyBytes = getKeyBytes(key);

return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding);

}

 

public byte[] decrypt(byte[] cipherText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidParameterSpecException {

 

 

/* Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm);

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpecy);

AlgorithmParameters params = cipher.getParameters();

iv = params.getParameterSpec(IvParameterSpec.class).getIV();

cipherText = cipher.doFinal(cipherText);*/

 

//System.out.println("IV:" + Base64.encode(iv));

//System.out.println("Cipher text:" + Base64.encode(cipherText));

 

 

 

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;

}

 

 

 برای این سوال پاسخی وجود ندارد.

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .