EasySafeChat
RSA介绍
非对称加密是一种特殊的加密,加密和解密使用不同密钥,RSA是一种重要的非对称加密算法。
RSA有公钥和私钥两个密钥,正如名字所说的那样,公钥可以公开,私钥不能公开。现在没有有效的方法从公钥推出私钥,所以可以放心将公钥公开。
明文使用公钥加密后得到密文,该密文只能由对应的私钥解开。设想Alice将她的公钥公开,Bob收到该公钥,用其加密消息得到密文,然后将密文传给Alice;就算密文被第三者Charlie看到,Charlie不能通过公钥解出私钥,所以解不开该密文;只有Alice收到密文后,才能用她的私钥解开密文。
同理,Bob也可以生成自己的公钥和私钥,并将公钥公开,Alice可以用Bob的公钥加密信息,传递给Bob,该密文只有Bob能解开。这样,Alice和Bob之间就实现了加密通话。
演示
这里使用了自己的公钥。实际使用中应该是填入对方的公钥来加密,然后将密文发送给对方,让对方解密:
软件介绍
头部两个输入框保存密钥,第一行为公钥,第二行为私钥。左侧是自己的公钥和私钥,如果为空会自动生成,右侧填入对方的公钥。双击可以选择一整行,便于复制公钥发送给别人。
这两个密钥文本框分别对应MyKeys.txt
和OthersPublicKey.txt
两个文件,但文本框内容改变,会在500ms后更新文件,所以请不要在软件打开的时候直接更改这两个文件,而应该在文本框中更改。
下面的文本框可以输入明文,或者粘贴进去密文。→
按钮用公钥加密当前文本,←
按钮用私钥解密当前文本。
常见用途
某些论坛没有私信功能,可以使用该软件传递一些私人信息,比如联系方式等。
一种很新的软件分发方式
这个软件非常小,压缩后只有约7KB,base64编码后也不到10KB,也就是一万个字节。有个词叫“万字长文”,也就是一篇长文章的长度。如果软件能以文字的形式传播,那肯定很新奇、有趣。
于是我写了一个cmd脚本来完成这件事,将脚本文本保存为t.cmd
文件,双击生成软件。
演示(注:第一次打开会生成自己的密钥,所以等待时间较长):
脚本内容:
@echo off
set f=EasySafeChat.
echo UEsDBBQAAAAIAHELflWRPATbfRkAAAA+AAAQAAAARWFzeVNhZmVDaGF0LmV4ZcRYZ3DcRBR+u7qTdJLOPrcrdojPEBvlHAx2IPTiEAiGAA7GB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Q5PQpqv/F4S4KEpv6O4d/6S/5M570GFCWt7qQx5mIEf5btAsnlvU9a/zflEtWcGNFo5Nq4OgO7+YN9vpqXB9+22tdA/SP/pM1BLAQI/ABQAAAAIAHELflWRPATbfRkAAAA+AAAQACQAAAAAAAAAIAAAAAAAAABFYXN5U2FmZUNoYXQuZXhlCgAgAAAAAAABABgAf65p3RcE2QEAAAAAAAAAAAAAAAAAAAAAUEsFBgAAAAABAAEAYgAAAKsZAAAAAA==>> "%f%txt"
certutil -decode "%f%txt" "%f%zip"
del "%f%txt"
tar -xf "%f%zip"
del "%f%zip"
其中tar
命令需要Windows版本大于等于Windows 10 17063。如果删掉最后两行,那么会生成zip压缩文件,需要手动解压。
技术细节
-
RSA是4096位的,足够安全。公钥存的是n的base64编码,私钥存的是p的base64编码,e默认都是65537。新密钥的生成使用
new RSACryptoServiceProvider(N_BITS)
,其中N_BITS
就是4096。 -
文本框编码是UTF-8,所以支持中文、Emoji😊等。
-
使用OAEP填充,相同文本每次加密得到的结果也不一样。
-
长文本将会被分块加密。4096位加密支持的最大bytes长度位470,所以文本经过UTF-8编码成bytes后会被按照470的长度切分,然后分别加密,得到一个个长为512的加密bytes块,这些加密块会被连接成一个大的bytes,然后进行base64编码。假设有n个加密块,最终的base64字符串长度为ceil(n*512/3)*4,n=1时的结果为684。