使用AES和Base64编码进行加密和解密 [英] Encrypt and decrypt with AES and Base64 encoding

查看:139
本文介绍了使用AES和Base64编码进行加密和解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.security.Key; 

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class Test {

private static final String ALGORITHM =AES;
private static final byte [] keyValue =ADBSJHJS12547896.getBytes();

public static void main(String args [])throws Exception {
String encriptValue = encrypt(dude5);
decrypt(encriptValue);

}

/ **
* @param args
* @throws异常
* /

public static String encrypt(String valueToEnc)throws Exception {

Key key = generateKey();
密码c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE,key);

System.out.println(valueToEnc.getBytes()。length+ valueToEnc.getBytes()。length);
byte [] encValue = c.doFinal(valueToEnc.getBytes());
System.out.println(encValue length+ encValue.length);
byte [] encryptedByteValue = new Base64()。encode(encValue);
String encryptedValue = encryptedByteValue.toString();
System.out.println(encryptedValue+ encryptedValue);

return encryptedValue;
}

public static String decrypt(String encryptedValue)throws异常{
Key key = generateKey();
密码c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE,key);

byte [] enctVal = c.doFinal(encryptedValue.getBytes());
System.out.println(enctVal length+ enctVal.length);

byte [] decordedValue = new Base64()。decode(enctVal);

return decordedValue.toString();
}

private static Key generateKey()throws Exception {
Key key = new SecretKeySpec(keyValue,ALGORITHM);
返回键;
}

}

这里我得到以下内容放弃例外?

  valueToEnc.getBytes()。length 5 
encValue length16
encryptedValue [B @ aa9835
线程main中的异常javax.crypto.IllegalBlockSizeException:在com.sun.crypto.provider.SunJCE_f.b(DashoA13 * ..)解密时使用填充密码
时,输入长度必须为16。 )
at com.sun.crypto.provider.SunJCE_f.b(DashoA13 * ..)



<有人可以解释我的原因吗?为什么解密那个长度应该是16的唯一的说法就是它不会像使用doFinal方法加密一样转换为16。



正如如何解密而不填补密码?

解决方案

您的加密订单 getBytes,加密,编码,toString

您的解密顺序: getBytes,decrypt,decode,toString



两个问题:


  1. 反转解密操作的顺序。你不是这样做的。

  2. 加密给你16个字节,编码24个字节,但是toString会给出106个字节。与无效字符有关,占用额外的空间。

注意:此外,您不需要调用 generateKey()两次。



使用相反的顺序解决问题#1。 正确的顺序: getBytes,解码,解密,toString

通过替换修复问题#2 xxx.toString() code> with new String(xxx)。在加密和解密功能中执行此操作。



您的解密应如下所示:

  c.init(Cipher.DECRYPT_MODE,key)
val decodingValue = new Base64()。decode(encryptedValue.getBytes())
val decryptptedVal = c.doFinal(decodedValue)
返回新的String(decryptptedVal)

dude5


I have following program for encrypting data.

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class Test {

    private static final String ALGORITHM = "AES";
    private static final byte[] keyValue = "ADBSJHJS12547896".getBytes();

    public static void main(String args[]) throws Exception {
        String encriptValue = encrypt("dude5");
        decrypt(encriptValue);

    }

    /**
     * @param args
     * @throws Exception
     */

    public static String encrypt(String valueToEnc) throws Exception {

        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);

        System.out.println("valueToEnc.getBytes().length "+valueToEnc.getBytes().length);
        byte[] encValue = c.doFinal(valueToEnc.getBytes());
        System.out.println("encValue length" + encValue.length);
        byte[] encryptedByteValue = new Base64().encode(encValue);
        String encryptedValue = encryptedByteValue.toString();
        System.out.println("encryptedValue " + encryptedValue);

        return encryptedValue;
    }

    public static String decrypt(String encryptedValue) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);

        byte[] enctVal = c.doFinal(encryptedValue.getBytes());
        System.out.println("enctVal length " + enctVal.length);

        byte[] decordedValue = new Base64().decode(enctVal);

        return decordedValue.toString();
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGORITHM);
        return key;
    }

}

Here I am getting the following out put with exception?

valueToEnc.getBytes().length 5
encValue length16
encryptedValue [B@aa9835
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)

Can some one explain me the cause? Why its only saying when decrypting that length should be 16. Doesn't it convert to 16 as like encrypting with the doFinal method.

And as the exception says "how to decrypting without padded cipher?"

解决方案

Your Order for encrypt: getBytes, encrypt, encode, toString
Your Order for decrypt: getBytes, decrypt, decode, toString

Two problems:

  1. As someone already mentioned you should reverse the order of operations for decryption. You are not doing that.
  2. encrypt gives you 16 bytes, encode 24 bytes, but toString gives 106 bytes. Something to do with invalid chars taking up additional space.

Note: Also, you don't need to call generateKey() twice.

Fix problem #1 by using the reverse order for decryption. Correct order: getBytes, decode, decrypt, toString
Fix problem #2 by replacing xxx.toString() with new String(xxx). Do this in both the encrypt and decrypt functions.

Your decrypt should look like this:

c.init(Cipher.DECRYPT_MODE, key)
val decodedValue = new Base64().decode(encryptedValue.getBytes())
val decryptedVal = c.doFinal(decodedValue)
return new String(decryptedVal)

This should give you back "dude5"

这篇关于使用AES和Base64编码进行加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆