如何将RSA密钥对(字符串)转换为加密密钥以签名数据
我使用此C#方法作为字符串将私钥发送到我的JavaScript:
public static string SignData(string certPath,string certPass)
{
X509Certificate2 keyStore = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + "Certifikatat\\" + certPath, certPass, X509KeyStorageFlags.Exportable);
RSA privateKey = keyStore.GetRSAPrivateKey();
TextWriter textWriter = new StringWriter();
var eky = DotNetUtilities.GetRsaKeyPair(privateKey);
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(eky);
pemWriter.Writer.Flush();
return pemWriter.Writer.ToString();
}
现在我需要将字符串转换为cryptokey
,以便用于签署某些数据。 我尝试了以下代码:
window.crypto.subtle.importKey(
"pkcs8",
pemToArrayBuffer(pk),
{
name: "RSASSA-PKCS1-v1_5",
hash: { name: "SHA-256" },
},
false,
["sign"]
)
.then(function (publicKey) {
//returns a publicKey (or privateKey if you are importing a private key)
console.log(publicKey);
})
.catch(function (err) {
console.error(err);
});
}
function removeLines(str) {
str = str.replace("\r", "");
return str.replace("\n", "");
}
function base64ToArrayBuffer(b64) {
var byteString = btoa(b64);
var byteArray = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
byteArray[i] = byteString.charCodeAt(i);
}
return byteArray;
}
function pemToArrayBuffer(pem) {
var b64Lines = removeLines(pem);
var b64Prefix = b64Lines.replace('-----BEGIN RSA PRIVATE KEY-----', '');
var b64Final = b64Prefix.replace('----- END RSA PRIVATE KEY-----', '');
return base64ToArrayBuffer(b64Final);
}
pk
是RSA对密钥,现在我需要将其转换为:
var signature = window.crypto.subtle.sign("RSA_PKCS1_SHA256", pks, bytes);
pks
应该是cryptokey 由
pk
字符串生成。
I used this C# method to send a private key to my javascript as a string:
public static string SignData(string certPath,string certPass)
{
X509Certificate2 keyStore = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + "Certifikatat\\" + certPath, certPass, X509KeyStorageFlags.Exportable);
RSA privateKey = keyStore.GetRSAPrivateKey();
TextWriter textWriter = new StringWriter();
var eky = DotNetUtilities.GetRsaKeyPair(privateKey);
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(eky);
pemWriter.Writer.Flush();
return pemWriter.Writer.ToString();
}
Now I need to convert the string to a CryptoKey
in order to be used for signing some data.
I tried the following code:
window.crypto.subtle.importKey(
"pkcs8",
pemToArrayBuffer(pk),
{
name: "RSASSA-PKCS1-v1_5",
hash: { name: "SHA-256" },
},
false,
["sign"]
)
.then(function (publicKey) {
//returns a publicKey (or privateKey if you are importing a private key)
console.log(publicKey);
})
.catch(function (err) {
console.error(err);
});
}
function removeLines(str) {
str = str.replace("\r", "");
return str.replace("\n", "");
}
function base64ToArrayBuffer(b64) {
var byteString = btoa(b64);
var byteArray = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
byteArray[i] = byteString.charCodeAt(i);
}
return byteArray;
}
function pemToArrayBuffer(pem) {
var b64Lines = removeLines(pem);
var b64Prefix = b64Lines.replace('-----BEGIN RSA PRIVATE KEY-----', '');
var b64Final = b64Prefix.replace('----- END RSA PRIVATE KEY-----', '');
return base64ToArrayBuffer(b64Final);
}
pk
is the rsa pair key and now i need to convert it in order to be used by:
var signature = window.crypto.subtle.sign("RSA_PKCS1_SHA256", pks, bytes);
pks
should be the CryptoKey
generated by the pk
string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C#代码不会以PKCS#8格式导出密钥,而是以PKCS#1格式导出密钥。由于WebCrypto API仅支持PKCS#8格式,因此必须更改:
中间的两行要照顾转换为PKCS#8格式键。
此外,在
base64toArrayBuffer()
中的JavaScript代码中,btoa()
函数必须替换为atob()
函数。另外,在pemtoarraybuffer()
中,必须删除PKCS#8标头和页脚,即------ /code>和
-----结束私钥-----
。通过这些更改,在C#代码中,PKCS#8密钥被导出,在JavaScript代码中,PKCS#8密钥将导入到
cryptokey
中。完整的JavaScript代码(包括签名):
The C# code does not export the key in PKCS#8 format, but in PKCS#1 format. Since the WebCrypto API only supports the PKCS#8 format, this must be changed:
The two lines in the middle take care of the conversion to a PKCS#8 formatted key.
Additionally, in the JavaScript code in
base64ToArrayBuffer()
, thebtoa()
function must be replaced with theatob()
function. Also, inpemToArrayBuffer()
, instead of the PKCS#1, the PKCS#8 headers and footers must be removed, i.e.-----BEGIN PRIVATE KEY-----
and-----END PRIVATE KEY-----
.With these changes, in the C# code, a PKCS#8 key is exported and in the JavaScript code, a PKCS#8 key is imported into a
CryptoKey
.Full JavaScript code (including signing):