当前位置:   article > 正文

对称加密(symmetric encryption)和非对称加密(Asymmetric Encryption)(密钥、公钥加密、私钥解密)AES、RSA_对称加密和非对称加密

对称加密和非对称加密

对称加密与非对称加密

在今天的数字化世界中,保护信息安全已经成为一个至关重要的任务。因此,了解和使用有效的加密技术是必不可少的。本文将介绍两种主要的加密类型:对称加密和非对称加密。

对称加密

1.1 定义

对称加密是一种早期的加密技术,其特点是在加密和解密过程中使用同一密钥(密码)。这就意味着,任何知道这个密钥的人都能解密被加密的信息。对称加密因其高效性和易于实施而受到广泛的欢迎。

1.2 工作原理

当发送者需要发送加密消息时,他们首先会使用一个密钥(也可以称之为密码)来转换或"加密"原始信息。接收者在收到加密消息后,需要用同一个密钥来"解密"这些信息,以获取原始的明文内容。

1.3 场景分析

假设Alice想要向Bob发送一个加密的消息,但是他们只能通过不安全的通信渠道交流。他们可以选择对称加密方法,步骤如下:

  1. 密钥生成:首先,Alice需要生成一个秘密密钥。这个密钥可以是随机生成的一串字节,其长度取决于所选的加密算法。例如,如果他们使用AES-128,那么密钥就应该是128位(或16字节)。

  2. 加密消息:然后,Alice使用这个密钥和加密算法(如AES)对她想要发送的消息进行加密。

  3. 密钥共享:为了让Bob能够解密这个消息,Alice需要找到一种安全的方式把密钥传给他。这可能需要另外的安全通信渠道(比如直接口头告知,小声点不要被人听到,因为我们要发送的是私㊙消息,防止被人窃取),或者他们之前就已经共享过密钥。

  4. 解密消息:Bob收到加密的消息和密钥后,就可以使用相同的加密算法和密钥对消息进行解密,从而获得原始的明文消息。

1.4 算法示例(以AES为例)

常见的对称加密算法有AES(高级加密标准)、DES(数据加密标准)、3DES、Blowfish等。其中,AES是目前最常用的对称加密算法。

# Import the AES module from Crypto.Cipher. This will be used to create the cipher objects for encryption and decryption.
# pip install pycryptodome
from Crypto.Cipher import AES

# Import base64 module for encoding the encrypted text into ASCII so that it can be printed and stored easily.
import base64

# The key used for encryption and decryption. It should be 16 bytes (128 bits), 24 bytes (192 bits) or 32 bytes (256 bits) long.
# Here we're using a 16-byte key, which means our AES cipher is operating in 128-bit mode.
key = b'abcdefghabcdefgh'
print("Encryption Key: ", key)

# The data that we want to encrypt. In a real use case, this could be sensitive information like a password or a secret message.
data = "Hello, World!"
print("Original Data: ", data)

# AES encryption requires the input to be a multiple of 16 bytes in length. If our data is not long enough, we need to pad it.
# Here we're padding the data with spaces on the right until it's 32 bytes long.
# We also need to convert our data into bytes, since AES works on bytes, not strings.
data_bytes = data.ljust(32).encode()
print("Padded and Encoded Data: ", data_bytes)

# Create a new AES cipher object with our key, using ECB (Electronic Codebook) mode.
cipher = AES.new(key, AES.MODE_ECB)

# Encrypt the data using our cipher, then encode the encrypted data into base64 so it can be printed and stored easily.
encrypted_text = base64.b64encode(cipher.encrypt(data_bytes))
print("Encrypted Text: ", encrypted_text)

# Now let's decrypt the data. We'll create another AES cipher with the same key.
cipher2 = AES.new(key, AES.MODE_ECB)

# Decrypt the data using our second cipher. First we need to decode the base64 back into bytes.
# After decryption, we strip any trailing spaces that were added during the padding process.
decrypted_text = cipher2.decrypt(base64.b64decode(encrypted_text)).strip()

# Finally, print the decrypted text. We need to decode it from bytes back into a string.
print("Decrypted Text: ", decrypted_text.decode())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
# 导入Crypto.Cipher模块下的AES。这将被用来创建加密和解密的密码对象。
from Crypto.Cipher import AES

# 导入base64模块以将加密的文本编码为ASCII,以便可以轻松地打印和存储。
import base64

# 用于加密和解密的密钥。它应该是16字节(128位)、24字节(192位)或32字节(256位)长。
# 这里我们使用了一个16字节的密钥,这意味着我们的AES密码器工作在128位模式下。
key = b'abcdefghabcdefgh'
print("加密密钥: ", key)

# 我们想要加密的数据。在真实的使用场景中,这可能是敏感信息,如密码或秘密消息。
data = "Hello, World!"
print("原始数据: ", data)

# AES加密要求输入的长度是16字节的倍数。如果我们的数据不够长,我们需要对其进行填充。
# 这里我们通过右边添加空格来填充数据,直到它达到32字节的长度。
# 我们还需要将数据转换为字节,因为AES处理的是字节,而不是字符串。
data_bytes = data.ljust(32).encode()
print("填充和编码后的数据: ", data_bytes)

# 使用我们的密钥创建一个新的AES密码对象,使用ECB(电子密码本)模式。
cipher = AES.new(key, AES.MODE_ECB)

# 使用我们的密码对数据进行加密,然后将加密的数据编码为base64,以便可以轻松地打印和存储。
encrypted_text = base64.b64encode(cipher.encrypt(data_bytes))
print("加密文本: ", encrypted_text)

# 现在让我们解密数据。我们将使用相同的密钥创建另一个AES密码器。
cipher2 = AES.new(key, AES.MODE_ECB)

# 使用我们的第二个密码对数据进行解密。首先我们需要将base64解码回字节。
# 解密后,我们去掉在填充过程中添加的任何尾随空格。
decrypted_text = cipher2.decrypt(base64.b64decode(encrypted_text)).strip()

# 最后,打印解密的文本。我们需要将其从字节解码回字符串。
print("解密文本: ", decrypted_text.decode())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

运行结果:
(可以看到,每次结果都一样,不带随机的)

在这里插入图片描述

1.5 对称加密的优点与缺点

优点
  • 效率高:对称加密算法比非对称加密算法快得多。
  • 简单易用:对称加密很容易实施,只需要管理一个密钥。
缺点
  • 密钥分发问题:对称加密的最大问题是如何安全地将密钥传输给接收者。如果密钥在传输过程中被截获,那么加密的信息就可能被解密和滥用。

非对称加密

2.1 定义

非对称加密,也被称为公钥加密,它使用一对密钥进行加密和解密操作。这两个密钥通常被称为公钥和私钥。其中,公钥可以公开给任何人,而私钥必须保持秘密。

2.2 工作原理

发送者使用接收者的公钥对消息进行加密,只有使用匹配的私钥才能解密这些消息。因此,即使攻击者截获了加密消息,他们也无法解密它,除非他们有接收者的私钥。

注意:每次生成的RSA密钥对都会不一样

每次生成的RSA密钥对都会不一样。这是因为在生成密钥对的过程中,涉及到了随机数的产生。

具体来说,RSA密钥对的生成包括以下步骤:

  1. 随机选择两个大素数p和q。
  2. 计算n = p * q和φ(n) = (p - 1) * (q - 1)。
  3. 选择一个整数e,使得1 < e < φ(n),且e和φ(n)互质。
  4. 计算d,使得d * e ≡ 1 (mod φ(n))。

其中,公钥就是(e, n),私钥就是(d, n)。

因为步骤1中的素数p和q是随机选择的,所以每次生成的密钥对都会不同。这也是为什么RSA加密算法能够提供高安全性的原因之一:即使攻击者知道了你的公钥和加密算法,他们也无法预测你下一次生成的密钥对会是什么。

2.3 场景分析

非对称加密的过程比对称加密稍微复杂一些。假设Alice想要安全地向Bob发送一个消息,他们选择非对称加密方法,步骤如下:

  1. 密钥对生成:首先,Bob需要生成一对公钥和私钥。私钥必须保密,而公钥可以公开。

  2. 公钥共享:然后,Bob把他的公钥发送给Alice。因为即使公钥被别人获取,没有对应的私钥,他们也无法解密经过公钥加密的信息,所以这个步骤不需要额外的安全通信渠道。

  3. 加密消息:Alice收到Bob的公钥后,就可以用它来加密她想要发送的消息。

  4. 解密消息:Alice将加密的消息发送给Bob,只有Bob可以使用他的私钥对消息进行解密。

2.4 算法示例(以RSA为例)

RSA和ElGamal是最常见的非对称加密算法。以下是一个使用RSA进行加密和解密的Python示例:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64

# 创建密钥对
# RSA.generate函数用于生成新的RSA密钥对。2048是密钥长度,单位是比特。
key = RSA.generate(2048)
print("Raw Key: ", key)

# 使用export_key方法将原始密钥转换为可以存储或传输的格式
private_key = key.export_key()
print("Private Key: ", private_key)

public_key = key.publickey().export_key()
print("Public Key: ", public_key)

# 将消息用公钥加密
# RSA.import_key方法用于从字符串形式的密钥恢复为原始的密钥格式
recipient_key = RSA.import_key(public_key)

# PKCS1_OAEP是一种填充方案,用于将明文扩展到合适的长度以便进行RSA加密
cipher_rsa = PKCS1_OAEP.new(recipient_key)

# 加密后的数据通常包含不可打印的字符,所以我们用base64编码将其转换为可打印的字符串形式
encrypted_data = base64.b64encode(cipher_rsa.encrypt(b"Hello, World!"))
print("Encrypted Text: ", encrypted_data)

# 使用私钥解密消息
private_key = RSA.import_key(private_key)
cipher_rsa = PKCS1_OAEP.new(private_key)

# 解密时,首先要将数据从base64编码的字符串形式恢复为原始的字节串形式
decrypted_data = cipher_rsa.decrypt(base64.b64decode(encrypted_data))

# 最后,我们将解密得到的字节串转换为字符串形式以便阅读和处理
print("Decrypted Text: ", decrypted_data.decode())

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

运行结果:

在这里插入图片描述

(每次结果都不一样的)

2.5 非对称加密的优点与缺点

优点
  • 安全性高:由于公钥可以公开分发,因此不需要像对称加密那样安全地传输密钥。
缺点
  • 效率低:相比对称加密,非对称加密的速度较慢。
  • 复杂性高:非对称加密需要更复杂的管理和实施。

结论

对称加密和非对称加密都有各自的优点和缺点。在选择加密方法时,需要考虑到数据安全性、性能、可用性等因素。在许多情况下,这两种加密技术会结合使用,以充分利用各自的优点,例如SSL/TLS协议就是一个很好的例子。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号