继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

两种流密码A5/1和RC4

那怎么办嘛
关注TA
已关注
手记 39
粉丝 7202
获赞 414

因为上课用的英文,所以本文中的一些内容我将直接使用英文表述。
一、关于流密码
流密码的主要思想是对种子密钥使用扩展算法得到密钥流,密钥流与明文进行异或运算得到密文需要注意的是这里的密钥流并非密文,其仅为一个二进制字符串,并且为一次一密。解密则是使用密文与密钥流进行异或便可以得到明文。
二、A5/1
Based on shift registers,Used in GSM mobile phone system.
TIPS:什么是GSM
GSM即Global System of Mobil communication,全球移动通信系统。
GSM网络可解决三种安全问题:匿名性,身份认证以及机密性。
GSM缺陷:基站伪造,加密缺陷,SIM卡 攻击,无效假设。
更为详细的大家可以自行查阅,笔者在这里不再赘述。
接着来看A5/1,A5/1算法使用了三个LFSR,我们将其表示为X,Y,Z
X: 19 bits (x0,x1,x2, …,x18)
Y: 22 bits (y0,y1,y2, …,y21)
Z: 23 bits (z0,z1,z2, …,z22)
The total key bits: 64 bits
与此同时我们使用一个长度也为64位的密钥,在这里我们使用K表示,K用于这三个LFSR的初始填充,当这三个寄存器用K填充以后便可以生成密钥流了。
首先我们引入一个投票函数:m = maj(x8, y10, z10)
Examples: maj(0,1,0) = 0 and maj(1,1,0) = 1,这个投票函数类似于我们开会决定一件事情举手投票,票数多者胜出。在这里由于二进制个数是奇数,所以不存在平票的情况。
接下来If x8 = m then X steps(x寄存器往右移1位)
t = x13 ⨁\bigoplus x16⨁\bigoplus x17 ⨁\bigoplusx18
xi = xi⨁\bigoplus 1 for i = 18,17,…,1 and x0 = t
If y10 = m then Y steps
t = y20 ⨁\bigoplus y21
yi = yi⨁\bigoplus 1 for i = 21,20,…,1 and y0 = t
If z10 = m then Z steps
t = z7 ⨁\bigoplus z20⨁\bigoplus z21 ⨁\bigoplus z22
zi = zi⨁\bigoplus 1 for i = 22,21,…,1 and z0 = t
Keystream bit S= x18⨁\bigoplus y21 ⨁\bigoplus z22 (最右边比特的异或)
C=(M +S) mod 2 得到密文
在这里需要注意的是我们通过上述步骤得到的仅为1位的密文,所以要得到64位为需要循环64次。比如我们的明文长度为64位,而密钥长度为60位,此时我们需要循环4次才能够完成加密。
移位寄存器加密过去常适用于硬件加密,如果在软件方面使用则速率过低,现如今软件飞速发展,移位寄存器的使用率便有所降低,其主要应用在 Resource-constrained devices
三、RC4
RC4 became part of some commonly used encryption protocols and standards, such as WEP in 1997 and WPA in 2003/2004 for wireless cards; and SSL in 1995 and its successor TLS in 1999, until it was prohibited for all versions of TLS by RFC 7465 in 2015, due to the RC4 attacks weakening or breaking RC4 used in SSL/TLS.

RC4之所以使用率比较高是因为其效率高并且简单,RC4每一步会生成密钥字节流,而A5/1仅生成密钥流位。
RC4有一个有趣的地方在于密钥大小可以是从1到256字节之间的任意长度,并且该密钥仅用于初始化排列S
RC4由两部分组成:
1.A permutation of all 256 possible bytes (denoted “S” below).
The key-scheduling algorithm is used to initialize the permutation in the array “S”. “keylength” is defined as the number of bytes in the key and can be in the range 1 ≤ keylength ≤ 256, typically between 5 and 16, corresponding to a key length of 40 – 128 bits. First, the array “S” is initialized to the identity permutation.
2. Two 8-bit index-pointers (denoted “i” and “j”) to find a key byte
Pseudo-random generation algorithm (PRGA) modifies the state and outputs a byte of the keystream
伪代码表示如下

 The key-scheduling algorithm
        //初始化为恒等置换
endfor for i from 0 to 255 
	S[i] := i          
j := 0 
for i from 0 to 255   
	j := (j + S[i] + key[i mod keylength]) mod 256 
	swap values of S[i] and S[j]     //完成S的置换
Endfor
//迭代256次,注意密钥长度可变从5到16个字节
i := 0 
j := 0 
while GeneratingOutput: 
	i := (i + 1) mod 256 
	j := (j + S[i]) mod 256 
	swap values of S[i] and S[j] 
	K := S[(S[i] + S[j]) mod 256] 
	output K   //查询第i和第j个元素,并把其和作为新的密钥字节所在的索引
Endwhile   
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP