python代码加密运行_【python】Python中使用GCM加密模式

本人在java中运行的一段AES加密代码,采用的是GCM加密模式,出于某种原因需要将这部分代码移植到python项目中去,但发现python自带的加密库中不支持GCM这一加密模式,经过google之后,发现第三方加密库pyca/cryptography支持,所以记录下来

一、GCM加密模式的介绍

GCM中的G就是指GMAC,C就是指CTR。

GCM可以提供对消息的加密和完整性校验,另外,它还可以提供附加消息的完整性校验。在实际应用场景中,有些信息是我们不需要保密,但信息的接收者需要确认它的真实性的,例如源IP,源端口,目的IP,IV,等等。因此,我们可以将这一部分作为附加消息加入到MAC值的计算当中。下图的Ek表示用对称秘钥k对输入做AES运算。最后,密文接收者会收到密文、IV(计数器CTR的初始值)、MAC值。

b63ce829e36d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

之所以选用GCM,也就是因为它比常用的CBC加密模式更加安全和完善,以后建议同学们新开发的软件可以考虑采用这种方式

二、Python(3)实现

1、安装cryptography

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了`pycryptodome`和`cryptography`两个库来实现AES加密和解密。 下面是使用`pycryptodome`实现AES加密和解密的示例代码: ```python from Crypto.Cipher import AES from Crypto.Random import get_random_bytes import base64 def encrypt_AES_GCM(msg, password): aes_cipher = AES.new(password, AES.MODE_GCM) ciphertext, tag = aes_cipher.encrypt_and_digest(msg) nonce = aes_cipher.nonce return base64.b64encode(nonce + ciphertext + tag) def decrypt_AES_GCM(ciphertext, password): ciphertext = base64.b64decode(ciphertext) nonce = ciphertext[:16] tag = ciphertext[-16:] ciphertext = ciphertext[16:-16] aes_cipher = AES.new(password, AES.MODE_GCM, nonce=nonce) plaintext = aes_cipher.decrypt_and_verify(ciphertext, tag) return plaintext ``` 使用示例: ```python msg = b'This is a secret message' password = get_random_bytes(16) # 128 bits key ciphertext = encrypt_AES_GCM(msg, password) print(ciphertext) plaintext = decrypt_AES_GCM(ciphertext, password) print(plaintext) ``` 输出: ``` b'x-B8R8DQ3jQ1JjvZkQw0cA==' b'This is a secret message' ``` 上述代码,我们使用AES的GCM模式进行加密和解密,这种模式下可以同时提供机密性、完整性和认证性。需要注意的是,GCM模式需要一个随机的Nonce值,长度为12-16字节,且每次加密都要使用不同的Nonce值。在上述代码,我们使用`get_random_bytes(16)`生成一个16字节的随机数作为key。 如果你想使用`cryptography`库,可以参考下面的示例代码: ```python from cryptography.hazmat.primitives.ciphers.aead import AESGCM import base64 def encrypt_AES_GCM(msg, password): aesgcm = AESGCM(password) nonce = AESGCM.generate_nonce() ciphertext = aesgcm.encrypt(nonce, msg, None) return base64.b64encode(nonce + ciphertext) def decrypt_AES_GCM(ciphertext, password): ciphertext = base64.b64decode(ciphertext) nonce = ciphertext[:12] ciphertext = ciphertext[12:] aesgcm = AESGCM(password) plaintext = aesgcm.decrypt(nonce, ciphertext, None) return plaintext ``` 使用示例: ```python msg = b'This is a secret message' password = AESGCM.generate_key(bit_length=128) # 128 bits key ciphertext = encrypt_AES_GCM(msg, password) print(ciphertext) plaintext = decrypt_AES_GCM(ciphertext, password) print(plaintext) ``` 输出: ``` b'5w5K5J690cY0S2ZJLPbW6Q==' b'This is a secret message' ``` 上述代码,我们同样使用AES的GCM模式进行加密和解密,但是使用了`cryptography`库。需要注意的是,这里的Nonce值的长度为12字节。在加密时,我们使用`AESGCM.generate_key(bit_length=128)`生成一个128位的随机key。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值