我是加密新手。我想在将数据发送到服务器之前使用 JavaScript RSA 库对数据进行签名。
我正在尝试使用 JavaScript 库 jsrsasign:http://kjur.github.io/jsrsasign/签署数据。
我已经生成了一个模数 (n)、公共(public)指数 (e) 和一个私有(private)指数 (d),并尝试让它们对数据进行签名。
var privateExponent = "4B48F60D02460811DABF74D79770E8DD72B5EEFF1324F651D4543D98B8C9F0F7ED3E245C49D967A30B279564AFF4FE53E2D92704FB4D6E923732F4AF8CFDA2114A49B6E425FD44AEACB80F00125AF6468F1A666EDDD5CFD6BFAB27950EF3DFB78671734BF1BB782CBB654A7FBB9BFB897E37F0324710360E1515B1FF0EE7FAF1";
var modulus = "00A68A945DE1175E4EBA23C53B8A1237CE9AA292020ECB373D90837FBD8DB8164687B7913543EA838E2B669BD1626FA9872397C5398485D951CFC229E102A78654B61E9EB2BC3B36BD2624ACF5C8B82ABD33A5AF4144C55034A3E5BF8282F3D5CB0CF9F550844E68A58DADC3BCC7CF726F227F8EC5FAC2494F1FBDEEF39212B5FF";
var publicExponent = "10001";
我一直在尝试使用它们对一段数据进行如下签名:
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'));
但是验证函数一直返回 false。我不知道我做错了什么。
setPublic 和 setPrivate 函数是 rsa2.js 的一部分
最佳答案
verifyString
调用有错误。 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
意思是你需要调用的代码是:
rsa.verifyString(signature, result)
然而,变量名称令人困惑。 result
var 实际上是签名。
signString
函数使用 PCSK#1 v1.5 填充,因此您无需将摘要算法传递给验证函数。此信息已存在于签名中。
关于javascript - JavaScript 中的 RSA 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566668/