如何将public_key解析为erlang记录格式

时间:2016-08-11 12:15:44

标签: cryptography erlang rsa sha256

我有下一个问题。 通过http获取public_key并拥有这样的二进制文件(它更大但我只留下了关键的虚假内容):

<<"-----BEGIN PUBLIC KEY-----\nMIB\n-----END PUBLIC KEY-----">>

需要致电crypto:verify/5。 但它等待rsa_public() = [key_value()] = [E, N] 我无法找到方法/信息如何从我的二进制公钥指数和模块中获取。

感谢您的帮助

更新

我找到了这样的方式

[{_, Modulus, Exponent}] = public_key:pem_decode(PublicKey)
RSAPublicKey = [Exponent, Modulus]

crypto:verify(rsa, sha256, Binary, Sign, RSAPublicKey)

崩溃
** exception error: bad argument
     in function  crypto:rsa_verify_nif/4
        called as crypto:rsa_verify_nif(sha256,
                                        {digest,<<138,12,163,24,186,81,122,113,15,221,229,18,66,
                                                  68,254,150,205,251,145,83,107,139,192,236,65,
                                                  ...>>},
                                        <<40,169,124,140,188,173,31,150,44,227,57,150,41,119,67,
                                          12,100,69,14,17,153,221,182,12,22,55,...>>,
                                        [not_encrypted,
                                         <<48,130,1,34,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,
                                           3,130,1,15,...>>])
     in call from crypto:verify/5 (crypto.erl, line 491)


Binary = <<250,22,62,160,112,157,51,51,128,0,19,136,87,172,119,11,1,234,46,241,
          195,26,161,249,36,88,36,113,47,229,93,206,11>>,

Sign =  <<72,138,112,61,19,195,159,50,225,213,41,161,74,220,141,134,242,22,124,
        250,29,82,163,240,192,255,183,226,60,156,3,85,51,251,53,44,191,113,98,
        188,128,211,182,82,109,239,190,30,45,31,97,0,241,134,138,97,46,133,22,
        169,109,134,33,73,98,171,206,53,12,68,201,155,230,9,132,63,169,141,73,
        164,99,194,230,121,254,50,230,95,255,61,197,126,95,66,48,227,48,88,65,
        95,8,222,10,164,133,192,47,238,21,172,103,80,53,115,132,246,140,34,36,
        135,108,12,214,180,178,198,191,129,147,133,177,32,160,221,18,244,255,
        254,14,148,163,169,31,251,148,170,246,235,46,16,206,42,156,215,199,50,
        78,93,210,215,223,170,59,135,70,166,138,224,13,224,151,59,28,203,30,
        244,178,33,91,4,212,204,25,127,45,98,174,199,74,147,172,248,53,60,141,
        68,1,10,18,71,8,141,196,28,63,30,239,2,192,246,117,11,235,81,173,143,
        207,55,32,171,6,236,223,194,223,206,140,202,193,241,63,17,165,146,136,
        252,119,241,229,4,178,186,132,191,42,134,98,85,141,9,44,79,79,165,197,
        43>>,


RSAPublicKey =      [not_encrypted,
               <<48,130,1,34,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,3,130,1,
                 15,0,48,130,1,10,2,130,1,1,0,167,250,126,245,154,26,64,78,95,
                 230,173,52,133,251,82,139,189,140,204,18,176,83,193,38,111,
                 24,167,216,68,216,139,188,48,246,133,34,117,27,180,254,181,
                 112,153,4,144,249,19,214,87,99,68,242,139,55,106,183,247,21,
                 126,42,121,166,57,82,51,43,54,121,188,21,122,175,50,108,138,
                 242,160,191,121,1,161,151,208,177,206,60,126,37,196,163,144,
                 141,60,140,16,72,63,27,160,56,1,194,43,48,153,75,197,42,223,
                 20,201,232,10,176,84,150,64,188,241,88,216,234,95,49,48,53,
                 196,241,78,216,31,10,154,123,237,236,97,10,115,192,38,168,
                 152,78,22,216,180,214,48,69,191,191,173,125,197,103,98,98,
                 241,173,13,88,6,237,169,99,179,216,219,64,76,15,214,113,248,
                 181,222,5,22,239,117,113,4,200,62,210,180,212,157,209,209,18,
                 50,133,103,26,118,155,93,28,110,85,17,236,252,19,191,126,115,
                 244,138,81,234,94,219,131,213,82,80,184,49,125,99,156,95,91,
                 196,9,119,251,213,109,84,20,228,28,224,172,146,3,95,103,7,42,
                 213,17,169,236,230,31,108,226,188,140,163,199,2,3,1,0,1>>] 

1 个答案:

答案 0 :(得分:1)

最后,我找到了可行的解决方案:

[RSa] = public_key:pem_decode(PublicKey),
RSAPublicKey = public_key:pem_entry_decode(RSa),
public_key:verify(Binary, sha256, Sign, RSAPublicKey).