The latest version of Java -Python and achieve mutual Aes encryption and decryption

antecedent

Python and requires the use of one and the same Java AES encryption algorithm, so that the encrypted version of the Python ciphertext can be decrypted by the Java code, and vice versa.

Python implementation

Python 3.6 version

# - * - Coding: UTF-. 8 - * - 
Import Base64 
from the AES Crypto.Cipher Import 
from the urllib Import the parse 

AES_SECRET_KEY = 'lingyejunAesTest' here # 16 | 24 | 32 characters 
IV = "1234567890123456" 

# padding algorithm 
BS = len (AES_SECRET_KEY) 
PAD = the lambda S: S + (the BS - len (S)% the BS) CHR * (the BS - len (S)% the BS) 
unpad the lambda = S: S [0: -ORd (S [-1:] )] 


class AES_ENCRYPT (Object): 
    DEF the __init __ (Self): 
        self.key = AES_SECRET_KEY 
        self.mode = AES.MODE_CBC 

    # encryption function 
    DEF the encrypt (Self, text): 
        Cryptor = AES.new (self.key.encode ( " utf8 "), self.mode, IV.encode ( " utf8 ")) 
        self.ciphertext = cryptor.encrypt(bytes(pad(text), encoding="utf8"))
        string encryption is not necessarily obtained when #AES ascii character set, output to the terminal or stored can be problematic when using base64 encoding 
        return base64.b64encode (self.ciphertext) 

    # decryption function 
    DEF the 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) 

the __name__ == IF '__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 implementation

sun.misc.BASE64Decoder Import; 
Import sun.misc.BASE64Encoder; 

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

public class AesTest { 

    / ** 
     * encrypted with Key 26 may be letters and numbers 
     * as used herein, AES-128-CBC encryption mode, key 16 is required. 
     * / 
    Private sKey static String = "lingyejunAesTest"; 
    Private ivParameter static String = "1234567890123456"; 

    // encryption 
    public static String the encrypt (String SSRC) throws Exception { 
        the Cipher cipher = Cipher.getInstance ( "the AES / the CBC / PKCS5Padding"); 
        byte [] raw = sKey.getBytes () ; 
        SecretKeySpec skeySpec = new new SecretKeySpec (RAW, "
        IvParameterSpec iv = new IvParameterSpec (ivParameter.getBytes ( )); // CBC mode requires an IV vector, increase the strength of the encryption algorithm 
        cipher.init (Cipher.ENCRYPT_MODE, skeySpec, IV); 
        byte [] = ENCRYPTED cipher. the doFinal (sSrc.getBytes ( "UTF-. 8")); 
        return new new Base64Encoder () encode (ENCRYPTED);. As used herein, // do BASE64 transcoding. 
    } 

    // decryption 
    public static String the decrypt (String SSRC) { 
        the try { 
            byte [] = sKey.getBytes RAW ( "the ASCII"); 
            SecretKeySpec skeySpec = new new SecretKeySpec (RAW, "the AES"); 
            the Cipher cipher = Cipher.getInstance ( " the AES / the CBC / PKCS5Padding "); 
            IvParameterSpec IV = new 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();
        }
    }
}

Then out of the Java code encryption key to decrypt put in Python

Done to achieve the AES system conversion in Java and Python.

Guess you like

Origin www.cnblogs.com/lingyejun/p/10971308.html