2

我需要在 Chrome 扩展程序和 Python 网络服务器之间发送以 RSA 编码的字符串,回复还需要包含加密字符串。通信是间接的,因此 SSL 不会这样做,因为两者之间有一个服务器使用 SSL 进行调解。

我不知道图书馆是否使用不同的方案。如果知道的人能告诉我,那就太好了。每个库的输出都不同,我尝试更改编码以适应彼此,但我没有任何运气。

库:https ://sourceforge.net/projects/pidcrypt/和https://www.dlitz.net/

PyCrypto 的代码:

>>> import base64
>>> from Crypto.PublicKey import RSA
>>> 
>>> key = RSA.importKey(open('./keyBR.pub', 'r').read())
>>> privkey = RSA.importKey(open('./privkeyBR.pem', 'r').read())
>>> 
>>> 
>>> message = "This is the story"
>>> ciphertext = key.encrypt( message, None )
>>> print ciphertext
("\xd9\x97o\x85#\xc8\xba\x14\xc9\xa4h\xacg\xc8\xc7\xb3\xbfA\xf1\x8e\x8aI\xe72IT\x03\x1a\xb7\x19\xd4\xb0\x9f\x9a\xde\x13-m\x9dw\x9d\\\x8f\x9fG\xb8(\x91\xa3\xcd\xad\xf6\xea(\x16\xd5]\xeb\x96\xc5\xe3\xecy\x9b\xf9o\x0b\\P3b\xac\xcf\xcf#\xe5\x9e\x07\xe0&\x1f8\x145!\xc8\xc9\xe0x\x7f\xa1\xbf\x03\xda\xa8z]\xa3\xff\xa7\xf0\xa7\t\xef\x80An\xd8i\xd9\xb8_1\xb7\x02\x1e\xa3\xb4z\x99c\x1d\x85\xef#b\xe8a\x18\xd3\xe09\x7f\xb6\x91!\x0b\x06\x07\xb0\xd3\xa3\x7f\xc5\x97\xa1\x90\x94\xa7M\xd8\xf2(\xe3\xe3\xc2\x1es\xba\xae\x0c\xb7\xa6\xe1\xd2\xb03\xc1\xcaU\xca@\xd1\x81\xde@\xa0\t\xf1<c\x9e\x8a\x8b\x88Lj\x16 lc\xae<\xbf\x16R\xe8\x04}\xcd\x7f\x0bZA\xd9y\x14\x03\xb7Hg\xac\xf4\xa5\xce\xe0\xa2\x8b\xdai\x03\xbb5\x99XS\xfe/\xc7I\xd3D\x81'xZ\xed\xce\t,E\xfc\x90\x1a_\xb3\xa7s\x0coA&1/\x06 ",)
>>> encMsg = base64.encodestring( ciphertext[0])
>>> print encMsg
2ZdvhSPIuhTJpGisZ8jHs79B8Y6KSecySVQDGrcZ1LCfmt4TLW2dd51cj59HuCiRo82t9uooFtVd
65bF4+x5m/lvC1xQM2Ksz88j5Z4H4CYfOBQ1IcjJ4Hh/ob8D2qh6XaP/p/CnCe+AQW7Yadm4XzG3
Ah6jtHqZYx2F7yNi6GEY0+A5f7aRIQsGB7DTo3/Fl6GQlKdN2PIo4+PCHnO6rgy3puHSsDPBylXK
QNGB3kCgCfE8Y56Ki4hMahYgbGOuPL8WUugEfc1/C1pB2XkUA7dIZ6z0pc7goovaaQO7NZlYU/4v
x0nTRIEneFrtzgksRfyQGl+zp3MMb0EmMS8GIA==

>>> n = base64.decodestring(encMsg)
>>> print n
o�#Ⱥɤh�g�dz�A�I�2IT�԰���-m�w�\G�(��ͭ��(�]�����y��o
                                                      \P3b���#���&amp;85!���x��ڨz]����� ��An�iٸ_1���z�c��#b�a��9��!
                                                                                                                        �ӣŗ����M��(���s��
             ���Ұ3��U�@с�@� �&lt;c����Lj lc�&lt;�R�}�
                                                       ZA�y�Hg����ࢋ�i�5�XS�/�I�D�'xZ��  ,E��_��s
                                                                                                         oA&1/ 
>>> privkey.decrypt(n)
'This is the story'

带有 JS 的代码:Chrome JS 控制台

message = "This is the story"
p1 = certParser(public_key);

var key = pidCryptUtil.decodeBase64(p1.b64);
//new RSA instance
var rsa = new pidCrypt.RSA();
//RSA encryption
//ASN1 parsing
var asn = pidCrypt.ASN1.decode(pidCryptUtil.toByteArray(key));
var tree = asn.toHexTree();
//setting the public key for encryption
rsa.setPublicKeyFromASN(tree);

crypted = rsa.encrypt(message);
"6f1df9260ff3b6cf5a78a3b7090424e6357735843a888bc9cac9de3db07d3b52d870b11e8e84fb5ece2b03d6f756fa2df1bacce8cc91eb17b2841ccbd694214773722b8ef4bbb58d58aeb09845e3e05dcf39cd139137950099eef8b8ed3944b41f785d90f40573a88147ea4eb8507fa9734db353b2a08d9f7e41a270baa618c790702f8f935ba20d8d07b20d327cc1ac61055cb49aba95b3c2cdce3559d8e06def39e48653bcd931c9a4f41c8de3ee62ecaa2c76133e8da23c88eb4eeb2911a69f823f7709ced465aebac9a7c15311e43caf40019809a632436f87d1ab7add8c5185d9fde91fbea8533f9b6a387937b8ddf2293d1a89d38f74308136df269259"
r1 = pidCryptUtil.formatHex(crypted,63);
"6f:1d:f9:26:0f:f3:b6:cf:5a:78:a3:b7:09:04:24:e6:35:77:35:84:3a:
88:8b:c9:ca:c9:de:3d:b0:7d:3b:52:d8:70:b1:1e:8e:84:fb:5e:ce:2b:
03:d6:f7:56:fa:2d:f1:ba:cc:e8:cc:91:eb:17:b2:84:1c:cb:d6:94:21:
47:73:72:2b:8e:f4:bb:b5:8d:58:ae:b0:98:45:e3:e0:5d:cf:39:cd:13:
91:37:95:00:99:ee:f8:b8:ed:39:44:b4:1f:78:5d:90:f4:05:73:a8:81:
47:ea:4e:b8:50:7f:a9:73:4d:b3:53:b2:a0:8d:9f:7e:41:a2:70:ba:a6:
18:c7:90:70:2f:8f:93:5b:a2:0d:8d:07:b2:0d:32:7c:c1:ac:61:05:5c:
b4:9a:ba:95:b3:c2:cd:ce:35:59:d8:e0:6d:ef:39:e4:86:53:bc:d9:31:
c9:a4:f4:1c:8d:e3:ee:62:ec:aa:2c:76:13:3e:8d:a2:3c:88:eb:4e:eb:
29:11:a6:9f:82:3f:77:09:ce:d4:65:ae:ba:c9:a7:c1:53:11:e4:3c:af:
40:01:98:09:a6:32:43:6f:87:d1:ab:7a:dd:8c:51:85:d9:fd:e9:1f:be:
a8:53:3f:9b:6a:38:79:37:b8:dd:f2:29:3d:1a:89:d3:8f:74:30:81:36:
df:26:92:59:
"
r2 = pidCryptUtil.fragment(pidCryptUtil.encodeBase64(pidCryptUtil.convertFromHex(crypted)),64);
"bx35Jg/zts9aeKO3CQQk5jV3NYQ6iIvJysnePbB9O1LYcLEejoT7Xs4rA9b3Vvot
8brM6MyR6xeyhBzL1pQhR3NyK470u7WNWK6wmEXj4F3POc0TkTeVAJnu+LjtOUS0
H3hdkPQFc6iBR+pOuFB/qXNNs1OyoI2ffkGicLqmGMeQcC+Pk1uiDY0Hsg0yfMGs
YQVctJq6lbPCzc41Wdjgbe855IZTvNkxyaT0HI3j7mLsqix2Ez6NojyI607rKRGm
n4I/dwnO1GWuusmnwVMR5DyvQAGYCaYyQ2+H0at63YxRhdn96R++qFM/m2o4eTe4
3fIpPRqJ0490MIE23yaSWQ==
"
r3 = pidCryptUtil.stripLineFeeds(r2)
"bx35Jg/zts9aeKO3CQQk5jV3NYQ6iIvJysnePbB9O1LYcLEejoT7Xs4rA9b3Vvot8brM6MyR6xeyhBzL1pQhR3NyK470u7WNWK6wmEXj4F3POc0TkTeVAJnu+LjtOUS0H3hdkPQFc6iBR+pOuFB/qXNNs1OyoI2ffkGicLqmGMeQcC+Pk1uiDY0Hsg0yfMGsYQVctJq6lbPCzc41Wdjgbe855IZTvNkxyaT0HI3j7mLsqix2Ez6NojyI607rKRGmn4I/dwnO1GWuusmnwVMR5DyvQAGYCaYyQ2+H0at63YxRhdn96R++qFM/m2o4eTe43fIpPRqJ0490MIE23yaSWQ=="
params = certParser(private_key);
Object {info: "", salt: "", b64: "MIIEpgIBAAKCAQEA3S3eeuD2aeTg+FLEUoWg5X/A62pK3qlt6G…xfI7t+kR0SN7VJ6QPI4FTmz0bhzcwYcEvjc+Qf3+9RtNgkKeD", aes: false, mode: ""…}

key = pidCryptUtil.decodeBase64(params.b64);
var rsa = new pidCrypt.RSA();
//RSA decryption
//ASN1 parsing
asn = pidCrypt.ASN1.decode(pidCryptUtil.toByteArray(key));
tree = asn.toHexTree();
//alert(showData(tree));
//setting the private key for encryption
rsa.setPrivateKeyFromASN(tree);
t = new Date();  // timer
crypted2 = pidCryptUtil.decodeBase64(pidCryptUtil.stripLineFeeds(r3));

var decrypted = rsa.decrypt(pidCryptUtil.convertToHex(crypted2));

"où&ó¶ÏZx£· $æ5w5:ÉÊÉÞ=°};RØp±û^Î+Ö÷Vú-ñºÌèÌë²ËÖ!Gsr+ô»µX®°Eãà]Ï9Í7îø¸í9D´x]ôs¨GêN¸P©sM³S² ~A¢pº¦Çp/[¢
²
2|Á¬a\´º³ÂÍÎ5YØàmï9äS¼Ù1ɤôãîbìª,v>¢<ëNë)¦?w    ÎÔe®ºÉ§ÁSä<¯@   ¦2CoÑ«zÝQÙý龨S?j8y7¸Ýò)=Ót06ß&Y"
decrypted
"This is the story"

js的功能:

function certParser(cert){
  var lines = cert.split('\n');
  var read = false;
  var b64 = false;
  var end = false;
  var flag = '';
  var retObj = {};
  retObj.info = '';
  retObj.salt = '';
  retObj.iv;
  retObj.b64 = '';
  retObj.aes = false;
  retObj.mode = '';
  retObj.bits = 0;
  for(var i=0; i< lines.length; i++){
    flag = lines[i].substr(0,9);
    if(i==1 && flag != 'Proc-Type' && flag.indexOf('M') == 0)//unencrypted cert?
      b64 = true;
    switch(flag){
      case '-----BEGI':
        read = true;
        break;
      case 'Proc-Type':
        if(read)
          retObj.info = lines[i];
        break;
      case 'DEK-Info:':
        if(read){
          var tmp = lines[i].split(',');
          var dek = tmp[0].split(': ');
          var aes = dek[1].split('-');
          retObj.aes = (aes[0] == 'AES')?true:false;
          retObj.mode = aes[2];
          retObj.bits = parseInt(aes[1]);
          retObj.salt = tmp[1].substr(0,16);
          retObj.iv = tmp[1];
        }
        break;
      case '':
        if(read)
          b64 = true;
        break;
      case '-----END ':
        if(read){
          b64 = false;
          read = false;
        }
      break;
      default:
        if(read && b64)
          retObj.b64 += pidCryptUtil.stripLineFeeds(lines[i]);
    }
  }
  return retObj;
}

如果我尝试使用从一个到另一个的任何加密输出,我只会得到垃圾。有任何想法吗?

4

1 回答 1

0

我不确定 JavaScript 库 PidCrypt。但是,我在使用 JSEncrypt 库时遇到了同样的问题。

我能够通过删除正在添加的不必要的填充来解决它。您可以提供以下解决方案的链接:

https://stackoverflow.com/a/42196983/1161911

于 2017-02-13T05:03:54.063 回答