AES对称加密算法

  1. 简介:

AES:高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是加密和解密用相同的密钥,具体的加密流程如下图:
在这里插入图片描述
密钥:用来加密明文的密码,在对称加密算法中,加密与解密的密钥是相同的。密钥为接收方与发送方协商产生,但不可以直接在网络上传输,否则会导致密钥泄漏,通常是通过非对称加密算法加密密钥,然后再通过网络传输给对方,或者直接面对面商量密钥。密钥是绝对不可以泄漏的,否则会被攻击者还原密文,窃取机密数据。

  1. 对称加密算法:
    加密过程:
    将明文分成N个组,然后对各个组进行加密,形成各自的密文,最后把所有的分组密文 进行合并,形成最终的密文。
    优点:
    算法公开、计算量小、加密速度快、加密效率高
    缺点:
    交易双方都使用同样钥匙,安全性得不到保证
    密钥管理困难,尤其是在分布式网络中
    常用算法:
    DES、3DES(TripleDES)、AES、RC2、RC4、RC5和Blowfish
  2. 代码实现:
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

//import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;


public class AESTest {

    /**
     * 使用AES算法进行加密
     * @param str
     * @return
     */
    public static byte[] jdkAESEncode(String str,Key key){
        try {
           
            //加密
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE,key);
            byte [] result = cipher.doFinal(str.getBytes());
            //String string = Base64.encode(result);
            return result;//string;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     *使用AES对加密后的字符串进行解密
     * @param str
     * @return
     */
    public static String jdkAESDecode(byte[] bytesContent,Key key){
        try {
            //加密
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE,key);
            //byte [] bytesContent = Base64.decode(str);
            byte [] result = cipher.doFinal(bytesContent);
            return new String(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException
    {
    	 //生成key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128,new SecureRandom());
        //生成密钥
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytes = secretKey.getEncoded();
        //key转换
        Key key = new SecretKeySpec(bytes ,"AES");
        byte[] str = jdkAESEncode("welcome to first AES",key);
        System.out.println(str);
        System.out.println(jdkAESDecode(str,key));
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Python实现AES对称加密算法的示例代码: ```python from Crypto.Cipher import AES import base64 # 加密函数 def encrypt(text, key): # 将密钥转换为16字节长度的bytes类型 key = key.encode('utf-8') key = key.ljust(16, b'\0') # 将明文转换为bytes类型 text = text.encode('utf-8') # 使用AES加密算法进行加密 cipher = AES.new(key, AES.MODE_ECB) encrypted_text = cipher.encrypt(text) # 将加密后的bytes类型数据转换为base64编码的字符串 encrypted_text = base64.b64encode(encrypted_text).decode('utf-8') return encrypted_text # 解密函数 def decrypt(encrypted_text, key): # 将密钥转换为16字节长度的bytes类型 key = key.encode('utf-8') key = key.ljust(16, b'\0') # 将密文转换为bytes类型 encrypted_text = base64.b64decode(encrypted_text) # 使用AES加密算法进行解密 cipher = AES.new(key, AES.MODE_ECB) decrypted_text = cipher.decrypt(encrypted_text) # 将解密后的bytes类型数据转换为字符串 decrypted_text = decrypted_text.decode('utf-8') return decrypted_text # 测试 text = 'Hello, world!' key = '1234567890123456' encrypted_text = encrypt(text, key) print('加密后的密文:', encrypted_text) decrypted_text = decrypt(encrypted_text, key) print('解密后的明文:', decrypted_text) ``` 上述代码使用了Python的`Crypto`库实现了AES对称加密算法。其中,`encrypt`函数用于加密明文,`decrypt`函数用于解密密文。在加密和解密过程中,需要使用相同的密钥。在本例中,密钥为16字节长度的字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值