AES密码加密

AES密码加密

AESUtil

public class AESUtil {
    // 密钥
    public static String key = "8888888888888888";
    private static String charset = "utf-8";
    // 偏移量
    private static int offset = 16;
    private static String transformation = "AES/CBC/NoPadding";
    private static String algorithm = "AES";


    public static void main(String[] args) {
        String s = "123456";
        String encrypt = encrypt(s);
        System.out.println(encrypt);

        String decrypt = decrypt(encrypt);
        System.out.println(decrypt);

    }



    /**
     * 加密
     * @param content
     * @return
     */
    public static String encrypt(String content) {
        try {
            return encrypt(content, key);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 解密
     * @param content
     * @return
     */
    public static String decrypt(String content) {
        return decrypt(content, key);
    }


    public static String encrypt(String data, String key) 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(key.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            String encode = new Base64().encodeToString(encrypted);
            String dest = "";
            if (encode!=null) {
                Pattern p = Pattern.compile("\\s*|\t|\r|\n");
                Matcher m = p.matcher(encode);
                dest = m.replaceAll("");
            }
            return dest;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * AES(256)解密
     * @param content  解密内容
     * @param key  密钥
     * @return 
     */
    public static String decrypt(String content, String key) {
        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset);
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE, skey, iv);
            byte[] result = cipher.doFinal(new Base64().decode(content));
            return new String(result); 
        } catch (Exception e) {
        }
        return null;
    }


以下是使用Python的PyCryptodome库实现AES加密的示例代码: ```python from Crypto.Cipher import AES import base64 # 加密函数 def aes_encrypt(key, text): # 将密钥和明文转为bytes类型 key = bytes(key, encoding='utf-8') text = bytes(text, encoding='utf-8') # 补全明文长度为16的倍数 text_length = len(text) add = 16 - (text_length % 16) text += (b'\0' * add) # 创建AES对象并进行加密 aes = AES.new(key, AES.MODE_ECB) encrypt_text = aes.encrypt(text) # 将加密后的bytes类型转为base64编码字符串并返回 return str(base64.b64encode(encrypt_text), encoding='utf-8') # 解密函数 def aes_decrypt(key, encrypt_text): # 将密钥和加密后的字符串转为bytes类型 key = bytes(key, encoding='utf-8') encrypt_text = bytes(encrypt_text, encoding='utf-8') # 将base64编码字符串转为bytes类型并解密 encrypt_text = base64.b64decode(encrypt_text) # 创建AES对象并进行解密 aes = AES.new(key, AES.MODE_ECB) decrypt_text = aes.decrypt(encrypt_text) # 去掉补全的字符并将解密后的bytes类型转为字符串并返回 return str(decrypt_text.rstrip(b'\0'), encoding='utf-8') ``` 使用方法: ```python key = '1234567890123456' text = 'hello world' encrypt_text = aes_encrypt(key, text) print('加密后的字符串:', encrypt_text) decrypt_text = aes_decrypt(key, encrypt_text) print('解密后的字符串:', decrypt_text) ``` 其中,`key`为16、24或32字节的字符串,代表AES的密钥;`text`为要加密的明文字符串;`encrypt_text`为加密后的字符串;`decrypt_text`为解密后的字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值