[讨论]RAR加密,亮了呢
以下说明下为啥我觉得撞大运了。
好奇是菜鸟的天性,Google查了下rar的加密过程:
一、Rar文件生成的流程。
Winrar加密文件时,总的分两个步骤:
1:先把源文件压缩,压成一段数据段。
2:再将压缩完的数据段加密。
对于同一个源文件来说,不进行加密,压缩完,其rar文件中的数据段是一模一样的。但是如果对同一个源文件来说,即使使用同一个密码,加密完rar文件中的数据段是不一样的,加密密钥依赖于Salt(8个字节的密钥,用来加密时使用,存放在rar文件头中里,应该是随机生成的)
二、加密“压缩完的数据段”的流程
1、获取密钥:
将明文的口令与Salt一起,通过HASH算法,生成两个16字节的密钥。(一个是KEY(AES算法的参数),一个是initVector)
2、以Key和initVector来加密压缩数据:
这里,是一个循环加密的结构,每16字节作为一个块,进行加密(这可能正是为什么加密完的文件长度总为16倍数的原因)。加密采用AES算法(RAR采用的是AES的rijndael的标准应用)。这里注意:AES加密前,有一个异或运算,是先将每16字节块与上一个16字节块加密结果进行异或,然后再进行AES算法的。
;===============================================
packblock[0]=packblock^initVector
encryptBlock[0]=AES(packblock[0]) ;(KEY为AES的密钥)
for i=1 to 块数量-1
packblock=packblock^encryptBlock[i-1]
encryptBlock=AES(packblock) ;(KEY为AES的密钥)
next
;packblock表示压缩完的每16字节数据
;encryptBlock表示加密完的每16字节数据
;===============================================
三、解密的过程
由于AES算法是对称的,所以解密的过程,是加密过程的逆运算。但解密时AES算法过程与加密所用的不一样(是因为解密过程中由KEY生成的子密钥表不一样)。仍然需要我们将密码输入,与salt一起生成两个16字节密钥,KEY和initVector。
;===============================================
packblock[0]=AES1(encryptBlock[0]) ;(KEY为AES的密钥)
packblock[0]=packblock^initVector
for i=1 to 块数量-1
packblock=AES1(encryptBlock) ;(KEY为AES的密钥)
packblock=packblock^encryptBlock[i-1]
next
;===============================================
那判断密码是否正确的在什么地方呢?
解密的过程是解密后的数据块进行解压缩,然后解成源文件,对该文件进行CRC校验,与保存在RAR文件中的源文件CRC校验码比较,相同则密码正确,不相同则密码错误。