简体   繁体   English

JavaScript中的RSA签名

[英]RSA signature in JavaScript

I am new to encryption. 我是加密新手。 I would like to sign a piece of data using a JavaScript RSA library before sending it to the server. 我想先使用JavaScript RSA库对数据进行签名,然后再将其发送到服务器。

I am trying to use the JavaScript library jsrsasign: http://kjur.github.io/jsrsasign/ to sign the data. 我正在尝试使用JavaScript库jsrsasign: http ://kjur.github.io/jsrsasign/对数据进行签名。

I have generated a modulus (n), public exponent (e) and a private exponent (d) and trying to them to sign the data. 我生成了一个模数(n),公共指数(e)和私有指数(d),并尝试对它们进行数据签名。

var privateExponent = "4B48F60D02460811DABF74D79770E8DD72B5EEFF1324F651D4543D98B8C9F0F7ED3E245C49D967A30B279564AFF4FE53E2D92704FB4D6E923732F4AF8CFDA2114A49B6E425FD44AEACB80F00125AF6468F1A666EDDD5CFD6BFAB27950EF3DFB78671734BF1BB782CBB654A7FBB9BFB897E37F0324710360E1515B1FF0EE7FAF1";

var modulus = "00A68A945DE1175E4EBA23C53B8A1237CE9AA292020ECB373D90837FBD8DB8164687B7913543EA838E2B669BD1626FA9872397C5398485D951CFC229E102A78654B61E9EB2BC3B36BD2624ACF5C8B82ABD33A5AF4144C55034A3E5BF8282F3D5CB0CF9F550844E68A58DADC3BCC7CF726F227F8EC5FAC2494F1FBDEEF39212B5FF";

var publicExponent = "10001";

I have been trying to use them to sign a piece of the data as follows: 我一直在尝试使用它们来对数据签名,如下所示:

var signature = "Hello world";
var rsa = new RSAKey();
rsa.setPrivate(modulus, publicExponent, privateExponent);

var result = rsa.signString(signature, 'sha256');
rsa.setPublic(modulus, publicExponent);

console.log(result);
console.log(rsa.verifyString(result, 'sha256'));

However the verify function keeps returning false. 但是,verify函数不断返回false。 I have no idea what I am doing wrong. 我不知道我在做什么错。

The functions setPublic and setPrivate are part rsa2.js 函数setPublic和setPrivate是rsa2.js的一部分

There is an error in the verifyString call. verifyString调用中有错误。 The API says: API说:

verifyString(sMsg, hSig)
  verifies a sigature for a message string with RSA public key.
  Parameters:
    {String} sMsg
        message string to be verified.
    {String} hSig
        hexadecimal string of siganture.
        non-hexadecimal charactors including new lines will be ignored.

    Returns:
        returns 1 if valid, otherwise 0

It means that the code you need to call is: 这意味着您需要调用的代码是:

rsa.verifyString(signature, result)

However the variable names are confusing. 但是,变量名令人困惑。 The result var is actually the signature. result var实际上是签名。

signString function is using PCSK#1 v1.5 padding therefore you do not need to pass the digest algorithm to the verify function. signString函数使用的是PCSK#1 v1.5填充,因此您无需将摘要算法传递给verify函数。 This information is already present in the signature. 此信息已经存在于签名中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM