JS加解密和后台互通




    
    
    稻壳阅读器
    
    
    
    


Java语言实现

public class AESTest {

    //static String data = "123456RWEQR";
    static String key = "abcdef0123456789";  //16位
    static String iv = "0123456789abcdef";  //16位

    public static void main(String args[]) throws Exception {
       System.out.println(encryptAES("123456"));
        System.out.println(decryptAES(encryptAES("123456")));
    }

    public static String encryptAES(String data) throws Exception {

        try {

            Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");   //参数分别代表 算法名称/加密模式/数据填充方式
            int blockSize = cipher.getBlockSize();

            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);

            return new sun.misc.BASE64Encoder().encode(encrypted);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decryptAES(String data) throws Exception {
        try {
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data);

            Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

 

AES三中数据填充方式

PKCS7Padding
Java不支持此方式
PKCS7Padding是缺几个字节就补几个字节的0

PKCS5Padding
JS不支持此方式
PKCS5Padding是缺几个字节就补充几个字节的几,例如缺8个字节,就补充8个字节的8

NOPadding
不补充字节

你可能感兴趣的:(Web,java)