对称密钥和非对称密钥
对称密钥顾名思义就是两个end users使用同一个key,Secret Key来进行加密解密。最大的问题就是如何安全的传输SK给另一方。
非对称密钥就是说每个人都拥有一个public key和一个private key。public key来进行加密,private key用于解密。public key一般存放于一个受信任的第三方机构或者自己发放,private key仅私人拥有不对外开放。这种做法拥有绝对安全,但是效率比较慢,解密花费的时间成本较高。
所以通常来说,我们使用非对称加密传输Secret Key,然后用对称加密来完成传输,这样可以保证安全又保证效率。
他们的Differences:
⚫ Symmetric ciphers use only one cryptographic key whereas asymmetric ciphers use two.
⚫ Symmetric ciphers normally use one algorithm which runs forward for encryption and backwards for decryption. Asymmetric ciphers normally have two different algorithms, one for encryption and another for decryption.
⚫ Symmetric ciphers are normally much faster than asymmetric ciphers.
⚫ Symmetric ciphers are prone to the key sharing problem, asymmetric ciphers were introduced to resolve the key sharing problem.
⚫ Symmetric ciphers are secure with smaller key sizes as the keys are always kept secret. Asymmetric ciphers need much larger key sizes as the public key is always available and can be reverse engineered to obtain the private key.
解密加密算法常见的五个部件术语:
Plaintext – The original intelligible message or data.
Encryption Algorithm – An algorithm which performs various substitutions and transformations on the plaintext.
Secret Key – Is an input required for the encryption algorithm and is independent of the plaintext and the algorithm
Ciphertext – This is the scrambled message of the plaintext produced by the encryption algorithm as an output.
Decryption Algorithm – An algorithm which allows for the receiver to obtain the plaintext back from the ciphertext. For a symmetric cipher, the decryption algorithm is normally the encryption algorithm run in reverse.
计算modulo inverse
什么是modulo inverse
就是对于模数n来说,会有两个数a, b,a*b mod n = 1。这时候我们称a是b的modulo inverse(反之亦可),写作b = a-1。
那么通常的计算方式采用Extended Euclid’s algorithm的GCD(最大公约数)来做,也就是保留系数,不多逼逼上例子:
a. 3−1 𝑚𝑜𝑑 7 = 5
b. 5−1 𝑚𝑜𝑑 13 = 8
c. 1473−1 𝑚𝑜𝑑 1562 = 351
d. 73−1 𝑚𝑜𝑑 127 = 87
计算过程如下:
进一步解释,这里用c作为例子:
其实不难看出,就是求完之后反推回去。首先9 = 4 x 2 + 1 => 1 = 9 - 4 x 2……1⃣️
再往上一步40 = 9 * 4 + 4 => 4 = 40 - 9 * 4 ……2⃣️
把2⃣️带入1⃣️可得 1 = 9 - 2 * (40 - 9 * 4)。
然后依次反推最后获得:1 = 1473 * 351 - 1562 * 331
到这一步要记得我们一开始是mod 1562,所以(- 1562 * 331)这一块在mod以后归零,原式子变成1473 * 351 = 1mod 1562,所以1473−1 𝑚𝑜𝑑 1562 = 351。
Possible Key
首先介绍一个简单的加密模式:Caesar Cipher(凯撒加密)
这种加密简单来说就是给定一个offset,将plaintext移动这个offset,就可以获得ciphertext
以英文字母表26个字母来说,通常Caesar Cipher的构造模式为 𝐶 = 𝐸[𝑎,𝑏](𝑝) = (𝑎𝑝 + 𝑏) 𝑚𝑜𝑑 26
这里需要计算可能的a,b组合,也就是possible key到底有多少种其实很简单。
b的取值范围就是[0, 25],因为超过这个范围mod 26以后又回来了。
而a的取值范围是质数于26的数,也就是 {1,3,5,7,9,11,15,17,19,21,23,25},12个。
那么总共组合数量是 12 * 26 = 312 keys。
这里再引入一个概念,叫trivial和non-trivial。A key is called trivial if c=p for all input p。而如果a=1, b=0的情况下原式子就变成 C = p mod 26。因为p的取值是[0,25],A对应0,所以c=p永远成立,这种组合相当于没有加密,所以我们计算的时候一般要减去这种可能。所以non-trivial = 312 - 1。
对于Caesar Cipher来说,也就是式子𝐶 = (𝑎𝑝 + 𝑏) 𝑚𝑜𝑑 26,我们如何解密求p呢?
很简单,先移动式子,不管mod。所以我们可以获得ap = c - b mod 26。
那这里注意,我们其实是想除一个a过去的,但是因为这里有mod 26所以不能直接除,但是我们可以两边都乘一个a-1,因为aa-1 mod 26 = 1,所以可以得到p = (𝑐 − 𝑏)𝑎−1 𝑚𝑜𝑑 26