sm2加密算法实例_实例说明加密算法

本文介绍了加密算法的基础知识,包括Diffie-Hellman和RSA的工作原理。Diffie-Hellman解决了对称算法中安全密钥交换的问题,提供了完美的前向保密性,而RSA则在身份验证方面更强大。两者各有优缺点,适用于不同的场景,如SSL/TLS实现中通常使用Diffie-Hellman来确保前向保密性。
摘要由CSDN通过智能技术生成

sm2加密算法实例

Cryptography, at its most basic, is the science of using codes and ciphers to protect messages.

密码学从根本上讲就是使用代码和密码保护消息的科学。

Encryption is encoding messages with the intent of only allowing the intended recipient to understand the meaning of the message. It is a two way function (you need to be able to undo whatever scrambling you’ve done to the message). This is designed to protect data in transit.

加密是在对消息进行编码时,仅允许目标收件人理解消息的含义。 这是一种双向功能(您需要能够撤消对消息所做的任何加扰操作)。 旨在保护传输中的数据。

If you're looking for a general background on the difference between symmetric and asymmetric algorithms and a general overview of what encryption is, start here. This article will primarily cover two of the most commonly used encryption algorithms.

如果您正在寻找有关对称和非对称算法区别的一般背景以及什么是加密的一般概述,请从此处开始。 本文将主要介绍两种最常用的加密算法。

As a general overview, there was a major problem with symmetric algorithms when they were first created - they only functioned effectively if both parties already knew the shared secret. If they didn't, securely exchanging a key without a third party eves-dropping was extremely difficult.

总的来说,对称算法在首次创建时就存在一个主要问题-只有在双方已经知道共享机密的情况下,对称算法才能有效发挥作用。 如果他们不这样做,那么在没有第三方窃听的情况下安全地交换密钥非常困难。

And if a third party obtained the key, it was very easy for them to then break the encryption, defeating the purpose of secure communication.

如果第三方获得了密钥,那么他们很容易破坏加密,从而破坏了安全通信的目的。

Diffie-Hellman solved this problem by allowing strangers to exchange information over public channels which can be used to form a shared key. A shared key is difficult to crack, even if all communications are monitored.

Diffie-Hellman通过允许陌生人通过可用于形成共享密钥的公共渠道交换信息来解决此问题。 即使监视所有通信,也很难破解共享密钥。

Diffie-Hellman如何工作? (How does Diffie-Hellman work?)

Diffie-Hellman is what's called a key exchange protocol. This is the primary use for Diffie-Hellman, though it could be used for encryption as well (it typically isn't, because it's more efficient to use D-H to exchange keys, then switch to a (significantly faster) symmetric encryption for data transmission).

Diffie-Hellman是所谓的密钥交换协议。 这是Diffie-Hellman的主要用途,尽管它也可以用于加密(通常不是,因为使用DH交换密钥效率更高,然后切换到(明显更快)对称加密进行数据传输) )。

The way this works is as follows:

其工作方式如下:

Basically, there are two parties, Alice and Bob, which agree on a starting color (arbitrary but has to be different every time). They also have a secret color they keep to themselves. They then mix this color with the shared color, resulting in two different colors. They then pass this color to the other party, who mixes it with their secret color, resulting in the same ending secret color.

基本上,有两方,爱丽丝(Alice)和鲍勃(Bob),就起始颜色达成共识(任意,但每次都必须有所不同)。 它们还具有自己保留的秘密颜色。 然后,他们将此颜色与共享颜色混合,从而得到两种不同的颜色。 然后,他们将此颜色传递给另一方,后者将其与自己的秘密颜色混合在一起,从而得到相同的结尾秘密颜色。

This relies upon the idea that it's relatively easy to mix two colors together, but it is very difficult to separate them in order to find the secret color. In practice, this is done with mathematics.

这是基于这样的想法:将两种颜色混合在一起相对容易,但是很难将它们分开以找到秘密颜色。 实际上,这是通过数学完成的。

For example:

例如:

  1. Bob and Alice agree on two numbers, a large prime, p = 29, and base g = 5

    鲍勃和爱丽丝在两个数字上达成共识,大质数p = 29,底数g = 5
  2. Now Bob picks a secret number, x (x = 4) and does the following: X = g^x % p (in this case % indicates the remainder. For example 3%2 is 3/2, where the remainder is 1). X = 5 ^4 % 29 = 625 % 29 = 16

    现在,Bob选择一个秘密数字x(x = 4)并执行以下操作:X = g ^ x%p(在这种情况下,%表示余数。例如3%2为3/2,余数为1) 。 X = 5 ^ 4%29 = 625%29 = 16
  3. Alice also picks a secret number, y (y = 8) and does the following: Y = g^y % p.  Y = 5 ^ 8 % 29 = 390,625 % 29 = 24

    爱丽丝还选择一个秘密数字y(y = 8),然后执行以下操作:Y = g ^ y%p。 Y = 5 ^ 8%29 = 390,625%29 = 24
  4. Bob sends X to Alice and Alice sends Y to Bob.

    鲍勃向爱丽丝发送X,爱丽丝向鲍勃发送Y。
  5. Then Bob does the following: K = Y^x % p, K = 24 ^ 4 % 29 = 331,776 % 29 = 16

    然后Bob执行以下操作:K =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值