Skip to content

TanixLu/EasySafeChat

Repository files navigation

EasySafeChat

RSA介绍

非对称加密是一种特殊的加密,加密和解密使用不同密钥,RSA是一种重要的非对称加密算法。

RSA有公钥私钥两个密钥,正如名字所说的那样,公钥可以公开,私钥不能公开。现在没有有效的方法从公钥推出私钥,所以可以放心将公钥公开。

明文使用公钥加密后得到密文,该密文只能由对应的私钥解开。设想Alice将她的公钥公开,Bob收到该公钥,用其加密消息得到密文,然后将密文传给Alice;就算密文被第三者Charlie看到,Charlie不能通过公钥解出私钥,所以解不开该密文;只有Alice收到密文后,才能用她的私钥解开密文。

同理,Bob也可以生成自己的公钥和私钥,并将公钥公开,Alice可以用Bob的公钥加密信息,传递给Bob,该密文只有Bob能解开。这样,Alice和Bob之间就实现了加密通话。

演示

这里使用了自己的公钥。实际使用中应该是填入对方的公钥来加密,然后将密文发送给对方,让对方解密:

软件介绍

头部两个输入框,左侧是自己的公钥,如果为空会自动生成(公私钥),右侧填入对方的公钥。双击可以选择一整行,便于复制公钥发送给别人。

这两个密钥文本框分别对应MyPublicKey.txtOthersPublicKey.txt两个文件,但文本框内容改变,会在500ms后更新文件,所以请不要在软件打开的时候直接更改这两个文件,而应该在文本框中更改。

自己的私钥保存在MyPrivateKey.txt,请妥善保管,不要泄露。

下面的文本框可以输入明文,或者粘贴进去密文。按钮用公钥加密当前文本,按钮用私钥解密当前文本。

一种很新的软件分发方式

这个软件非常小,压缩后只有约7KB,base64编码后也不到10KB,也就是一万个字节。有个词叫“万字长文”,也就是一篇长文章的长度。如果软件能以文字的形式传播,那肯定很新奇、有趣。

于是我写了一个cmd脚本来完成这件事,将脚本文本保存为t.cmd文件,双击生成软件。

演示(注:第一次打开会生成自己的密钥,所以等待时间较长):

脚本内容:

@echo off
set f=EasySafeChat
echo UEsDBD8AAgAOACMHPlcUVGJHrRcAAAA+AAAQAAAARWFzeVNhZmVDaGF0LmV4ZQkEBQBdAACAAAAmlo5wABf37AW76vT/lAEvRO985vUJGUQCKf9TzdbRFP6e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P8p3arKbL2hn3VFZ+Uikr/9kA8/vXOL8h4u9Pz5WyKPfmC5uSG25JqkpJzQpmEAOSp5UnwrddTX5EE6HoPhF+8wP2k4lLxDtVPcJmyHuAx/lC0i/qjLbNAjyBnbzMgGIHZ+wMjLQYGuN38XlfUPC8+n739nq+khL51vK3m74wFd+/AOkL6J9X7nUCUcp5sogLkqRayxiwSJePWNHqQ45GvlYOB9tuY8YV3+RlPz1LlI+z7rlbBEGoK6xzIoqkNaCujFqBYsbT/DFiyvY/3dnIDianfvSxkYgli3+9QNiKE2hUHxZv6N0bkQYDGg3GC7y+8isvCoKSq2oyllIsGvhzZ/NZ7ObeeKODsrxa7diGqorPU9yONI0x1oBG1Z4VFr/gQcf+48wbEXCUKIV28EcvzyQD9Q0wOdgmD1GPDdBmtCfktG9BLFCwIF4X/lLS2Q6+tlNh8ySoyaBnD8e21asckV/gAwTtA1dH3WS7Ig4T3enIiXImqnOkTfcDoosf//e3MORQSwECPwA/AAIADgAjBz5XFFRiR60XAAAAPgAAEAAAAAAAAAAA>> "%f%.txt"
echo AAAA/4EAAAAARWFzeVNhZmVDaGF0LmV4ZVBLBQYAAAAAAQABAD4AAADbFwAAAAA=>> "%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。