JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致

时间:2021-08-18 20:25:42

终于有机会生产JAVA的东东了。

有点兴奋。

花了一天搞完。。

java(关键key及算法有缩减):

package com.security;

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

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 实现AES加密解密
 * cg882
 * 2016-11-16
 */
public class AESencrp {

    // 加密算法
    private String ALGO = "AES";
    private String ALGO_MODE = "AES/CBC/NoPadding";
    private String akey = "16bit";
        private String aiv = "16bit";

    /**
     * 用来进行加密的操作
     *
     * @param Data
     * @return
     * @throws Exception
     */
    public String encrypt(String Data) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance(ALGO_MODE);
        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(akey.getBytes("utf-8"), ALGO);
        IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8"));
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(plaintext);
        String EncStr = new sun.misc.BASE64Encoder().encode(encrypted);
        return EncStr ;
      } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    }

    /**
     * 用来进行解密的操作
     *
     * @param encryptedData
     * @return
     * @throws Exception
     */
    public String decrypt(String encryptedData) throws Exception {
        try {
            byte[] encrypted1 = new sun.misc.BASE64Decoder().decodeBuffer(encryptedData);

        Cipher cipher = Cipher.getInstance(ALGO_MODE);
        SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);
        IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8"));

        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;
    }
    }

}    

测试JAVA的代码:

package com.security;

/**
 * 实现AES加密
 * cg882
 * 2016-11-16
 */
public class Checker {
    public static void main(String[] args) throws Exception {
        // 创建加解密
        AESencrp aes = new AESencrp();
        // 要进行加密的密码
        String password = "password^*(&( 09-8ADF";
        // 进行加密后的字符串
        String passwordEnc = aes.encrypt(password);
        String passwordDec = aes.decrypt(passwordEnc);
        System.out.println("原来的密码 : " + password);
        System.out.println("加密后的密码 : " + passwordEnc);
        System.out.println("解密后的原密码 : " + passwordDec);
    }
}

python就简单多罗。。。。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 16
PADDING = '\0'
pad_it = lambda s: s+(16 - len(s)%16)*PADDING
key = '16bit'
iv = '16bit'

#使用aes算法,进行加密解密操作
#为跟java实现同样的编码,注意PADDING符号自定义
def encrypt_aes(sourceStr):
    generator = AES.new(key, AES.MODE_CBC, iv)
    crypt = generator.encrypt(pad_it(sourceStr))
    cryptedStr = base64.b64encode(crypt)
    return cryptedStr

def decrypt_aes(cryptedStr):
    generator = AES.new(key, AES.MODE_CBC, iv)
    cryptedStr = base64.b64decode(cryptedStr)
    recovery = generator.decrypt(cryptedStr)
    decryptedStr = recovery.rstrip(PADDING)
    return decryptedStr

sourceStr = 'password^*(&( 09-8ADF'

print encrypt_aes(sourceStr)
print decrypt_aes(encrypt_aes(sourceStr))

结果图:

JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致

有几个小技巧跟进解决一下:

1,如果安装PYTHON扩展库时,需要C库进行编译,而自己又没安装权限或是BAT文件不对时,

直接从网上找预编译好的WHL文件,或者可以解决问题。

2,JAVA代码手工编译源码,执行程序,生成jar的例子如下(注意按包结构建好目录,执行命令时,在包顶层目录执行相关命令)

A编译java源代码

javac  -cp com/security com/security/*.java

B执行main文件,并指定包路径

java -cp . com/security/Checker

C指定class生成jar包

jar cvf AESencrp.jar com/security/AESencrp.class