- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将 CryptoKey 导出为 PEM 样式,现在我想将其导入回来。
我使用以下代码生成了我的 key :
function generate() {
return window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: "SHA-256"},
},
true,
["encrypt", "decrypt"]
).then(function (key) {
return key;
})
.catch(function (err) {
console.error(err);
});
}
function importPrivateKey(pemKey) {
return crypto.subtle.importKey("pkcs8", convertPemToBinary(pemKey), {name:"RSA-OAEP", hash:{name:"SHA-256"}}, true, ["encrypt", "decrypt"]);}
SyntaxError: Cannot create a key using the specified key usages.
function convertPemToBinary(pem) {
var lines = pem.split('\n');
var encoded = '';
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim().length > 0 &&
lines[i].indexOf('-----BEGIN RSA PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----BEGIN RSA PUBLIC KEY-----') < 0 &&
lines[i].indexOf('-----BEGIN PUBLIC KEY-----') < 0 &&
lines[i].indexOf('-----END PUBLIC KEY-----') < 0 &&
lines[i].indexOf('-----BEGIN PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----END PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----END RSA PRIVATE KEY-----') < 0 &&
lines[i].indexOf('-----END RSA PUBLIC KEY-----') < 0) {
encoded += lines[i].trim();
}
}
return base64StringToArrayBuffer(encoded);
}
function base64StringToArrayBuffer(b64str) {
b64str = b64EncodeUnicode(b64str);
var byteStr = atob(b64str);
var bytes = new Uint8Array(byteStr.length);
for (var i = 0; i < byteStr.length; i++) {
bytes[i] = byteStr.charCodeAt(i);
}
return bytes.buffer;
}
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
最佳答案
您不能使用 RSA-OAEP 私钥进行加密。该问题可能是由于您设置了 encrypt
导入时在 key 用法中标记。
参见 webcrypto 规范,第 22.4 节 https://w3c.github.io/webcrypto/Overview.html#rsa-oaep
If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
base64StringToArrayBuffer
是不正确的
b64str = b64EncodeUnicode(b64str);
var byteStr = atob(b64str);
b64EncodeUnicode
正在对内容进行两次编码。
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
关于javascript - 使用 Web Crypto 导入 PEM key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41529138/
如何从一个 PEM 中确定 SSL 证书到期日期,该 PEM 在一个文件 (pem) 上串联了很多证书? 例子: # cat cert.pem -----BEGIN CERTIFICATE-----
我想让运行测试更容易。为了实现这一点,我想提供项目内部测试中使用的 secret 。 我使用以下命令创建了“fullchain.pem”、“privkey.pem”和“dhparams.pem”: o
注意图片中证书的图标图像上写有标准或个人。有什么不同?最近一次我在处理它,我只制作了带有“个人”的图标。我不知道为什么。有什么不同?两者是如何产生的? 最佳答案 我会这样说,因为您的 .p12 文件包
我正在尝试安装这个项目:https://github.com/versatica/mediasoup-demo 它需要 fullchain.pem和 privkey.pem文件。 如何在 Ubuntu
我正在使用 curl 使用公共(public)证书文件从 https 站点下载数据。 系统信息: 操作系统:软呢帽 14 curl : curl 7.30.0 openssl: OpenSSL 1.0
我正在尝试使用 openssl 加密短文本,但我不知道如何继续。更具体地说:我发现一段代码似乎可以工作,但必须从终端执行指令。是的,我知道,存在 system() 函数,但我想避免它,因为它需要文件夹
我正在使用 OpenSSL 将“me.p12”转换为 PEM。当我生成“me.p12”时,我为其设置了密码。 “me.p12”包含私钥和证书。 当我将其转换为 PEM 时,我运行命令: openssl
我正在使用 Amazon 的 EC2 集群来运行 GraphLab。他们想要我的 .pem 文件的位置,这是我的私钥。 搜索后我仍然在ubuntu中找不到该文件。我正在使用 PuTTY。 最佳答案 .
我正在尝试了解 .pem 文件的布局方式。密码短语是如何验证的? .pem 文件如何检查您尝试打开它时使用的密码? 最佳答案 多种不同类型的数据存储在扩展名为.pem 的文件中。所以你的问题不准确。但
我需要生成一个 cert.pem 和 key.pem 文件来使用 Nginx 加密 http 请求。 在 Linux 上,我会执行以下 OpenSSL 命令: openssl req -x509 -n
我必须以 pem 格式提交 CSR。 我已经使用 OpenSSL 生成了 CSR,但在将其转换为 PEM 格式时遇到了困难。 我必须从 example.csr 获取示例 - csr.pem。如何实现?
我知道这是一个与许多其他问题非常相似的问题,但他们都没有给出直接的答案或对我有用的答案...... 我从 Let's encrypt 获得了两个文件: 证书.pem key.pem 我需要让他们进入
首先,我看过this thread但它不相关并且有不同的问题。 我的 Kafka 属性文件中有以下设置片段: ssl.keystore.type=PEM ssl.keystore.key=/path/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我想下载 Cpanel 自动生成的 SSL 作为 key.pem 和 cert.pem,我在 Cpanel 中找不到任何下载链接,有什么建议吗? 我曾尝试将证书文本复制到文本文件中并使用它,但没有成功
这个问题在这里已经有了答案: Converting pfx to pem using openssl (5 个答案) 关闭 6 年前。
我想通过 AWS Lambda 使用 pysftp 库连接 EC2。我使用以下代码进行连接。 mysftp = pysftp.Connection( host=Constants.MY_HOS
我有这两个文件(certificate.pem 和private_key.pem),它们由我使用的 API 提供。我需要使用这些文件在 Swift 项目中签署我的 http 请求。 我可以用 Post
我的 Pem 文件就是这种格式。 Bag Attributes localKeyID: friendlyName: test subject=/C=GB/ST=London/L=Soho/O=Righ
我从证书颁发机构获得了这些文件: domain.com.p7b domain.com.crt domain.com.ca-bundle 我尝试了这个小代码: var express = require
我是一名优秀的程序员,十分优秀!