最新版-PythonとJava実装Aes相互復号

3644 ワード

前情
PythonとJavaを使用して同じAES復号アルゴリズムを実現し、Pythonバージョンで暗号化された暗号文をJavaコードで復号できるようにする必要があります.逆も同様です.
Python実現
Pythonは3.6バージョン
# -*- coding: utf-8 -*-
import base64
from Crypto.Cipher import AES
from urllib import parse
 
AES_SECRET_KEY = 'lingyejunAesTest' #  16|24|32   
IV = "1234567890123456"
 
# padding  
BS = len(AES_SECRET_KEY)
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[0:-ord(s[-1:])]
 
 
class AES_ENCRYPT(object):
    def __init__(self):
        self.key = AES_SECRET_KEY
        self.mode = AES.MODE_CBC
 
    #    
    def encrypt(self, text):
        cryptor = AES.new(self.key.encode("utf8"), self.mode, IV.encode("utf8"))
        self.ciphertext = cryptor.encrypt(bytes(pad(text), encoding="utf8"))
        #AES              ascii    ,                 ,  base64  
        return base64.b64encode(self.ciphertext)
 
    #    
    def decrypt(self, text):
        decode = base64.b64decode(text)
        cryptor = AES.new(self.key.encode("utf8"), self.mode, IV.encode("utf8"))
        plain_text = cryptor.decrypt(decode)
        return unpad(plain_text)
 
if __name__ == '__main__':
    aes_encrypt = AES_ENCRYPT()
    my_email = "[email protected]"
    e = aes_encrypt.encrypt(my_email)
    d = aes_encrypt.decrypt(e)
    print(my_email)
    print(e)
    print(d)

Java実装
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
public class AesTest {
 
    /**
     *     Key    26        
     *     AES-128-CBC    ,key   16 。
     */
    private static String sKey = "lingyejunAesTest";
    private static String ivParameter = "1234567890123456";
 
    //   
    public static String encrypt(String sSrc) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//  CBC  ,      iv,          
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        return new BASE64Encoder().encode(encrypted);//    BASE64   。
    }
 
    //   
    public static String decrypt(String sSrc) {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//  base64  
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }
 
    public static void main(String[] args) {
        String email = "[email protected]";
        try {
            String sec = encrypt(email);
            System.out.println(sec);
            System.out.println(decrypt("CcOtM9WXv0N+Owh/xxedZJnuNUaTU7y3aUBESQLUvVM="));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Javaコードで暗号化された鍵をPythonに入れて復号する
大きな成果を上げ、JavaとPythonでのAESの相互回転を実現した.