龙空技术网

「青锋爱分享」springboot-Python之AES加解密实现方案

青锋爱编程 102

前言:

目前你们对“aes加解密c语言实现”大约比较着重,大家都需要学习一些“aes加解密c语言实现”的相关资讯。那么小编也在网摘上收集了一些对于“aes加解密c语言实现””的相关知识,希望我们能喜欢,我们快快来学习一下吧!

关注青锋,可获取更多青锋分享技术知识、下载开源源码。

码云搜索:青锋 会有惊喜哦哦。

AES-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 JPAESUtil {    /**     * 加密用的Key 可以用26个字母和数字组成     * 此处使用AES-128-CBC加密模式,key需要为16位。     */    private static String sKey = "qingfeng@1234598";    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 key = "782129121030322121,"+DateTimeUtil.getDateTimeStr()+","+DateTimeUtil.getNowBeforeTime(-5);        try {            String sec = encrypt(key);            System.out.println(sec);            System.out.println(decrypt(sec));        } catch (Exception e) {            e.printStackTrace();        }    }}
AES Python 实现加解密
# -*- coding: utf-8 -*-import base64from Cryptodome.Cipher import AESfrom urllib import parseAES_SECRET_KEY = 'qingfeng@1234598'  # 此处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 = "123"    e = aes_encrypt.encrypt(my_email)    d = aes_encrypt.decrypt(e)    print(my_email)    print(e)    print(d)    dd = aes_encrypt.decrypt("ZTK5F/G1vF3cxEhp2wGImg==")    print(dd)
青锋开源架构

标签: #aes加解密c语言实现