我正在尝试使用加密模块在Node.js 和使用Web密码API的Web客户端之间完成密钥交换。
到目前为止,我设法将密钥交换处理到在服务器和客户端上导出共享密钥的程度。在服务器端,我使用了“ secp521r1”曲线,在客户端端,使用了名称为“ P-521”的等效曲线。
在客户端上,我收到一个共享密码,该密码的长度为32个字节,我认为这是256位AES-GCM密钥的正确长度。但是在服务器上,我收到一个长度为66个字节的密钥。以下是生成的密钥的两个示例,一个示例以base64编码,另一个示例为字节数组:
Base64 encoded:
Client AES-Key: AJQQpnOjNe2/QQz5T9NmSPFpFgUG/20739EhdjVt//I=
Server AES-Key: AJQQpnOjNe2/QQz5T9NmSPFpFgUG/20739EhdjVt//LYZ+XeuTgkwv7CJFQrqNWxnny8R+VP3nJuk1SUyDJsa7+f
Byte array:
Client AES-Key: 0,249,8,221,38,57,84,243,202,83,90,68,4,41,49,224,69,89,162,74,47,72,134,169,32,3,133,55,109,105,144,249
Server AES-Key: 0,249,8,221,38,57,84,243,202,83,90,68,4,41,49,224,69,89,162,74,47,72,134,169,32,3,133,55,109,105,144,249,66,66,131,19,81,11,27,161,132,7,244,2,191,221,162,169,247,108,128,229,211,217,109,78,5,71,232,252,243,36,214,42,199,175
Run Code Online (Sandbox Code Playgroud)
我认为我在Node.js上配置错误,但是在文档中找不到有关如何正确执行操作的提示。
这是我在Node.js上提取密钥的操作:
var ecdh = crypto.createECDH('secp521r1');
ecdh.generateKeys();
ecdh.getPublicKey('base64'); // passed this to the client
// After I receive the public key of the client
var shared = ecdh.computeSecret(clientPublicKey, 'base64');
Run Code Online (Sandbox Code Playgroud)
我的问题是:
任何指导表示赞赏。
我有以下JavaScript代码使用Web Cryptography API实现公钥加密.它适用于Firefox和Chrome,但Microsoft Edge失败.我从Edge获得的错误是"由于错误80700011无法完成操作".我错过了什么?
<script>
var data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
var crypto = window.crypto || window.msCrypto;
var cryptoSubtle = crypto.subtle;
cryptoSubtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: "SHA-256" },
},
true,
["encrypt", "decrypt"]
).then(function (key) {
console.log(key);
console.log(key.publicKey);
return cryptoSubtle.encrypt(
{
name: "RSA-OAEP"
},
key.publicKey,
data
);
}).then(function (encrypted) {
console.log(new Uint8Array(encrypted));
}).catch(function (err) {
console.error(err);
});
</script>
Run Code Online (Sandbox Code Playgroud) encryption public-key-encryption webcrypto-api microsoft-edge
IE11的Web Crypto位于其中window.msCrypto,而对于Firefox或Chrome,它可以访问window.crypto.
Web Workers无法访问window上下文,但幸运的是,Chrome和Firefox还在cryptoWeb Worker上下文中公开了变量(因此,在全局级别,您可以使用this.cryptoWeb Worker中的Web Crypto套件).不过,看来,这IE11并没有暴露this.msCrypto在他们的Web工作环境.
那是对的吗?有没有办法在IE11 Web Worker中使用Web Crypto?
我已经在Windows 10上使用IE 11设法使用AES-GCM加密了一些数据,但无法进行解密。加密JS代码示例:
let plainText = new Uint8Array([1]);
let key;
let keyBuf = window.msCrypto.getRandomValues(new Uint8Array(32));
let iv = window.msCrypto.getRandomValues(new Uint8Array(12));
let additionalData = window.msCrypto.getRandomValues(new Uint8Array(16));
let encResult;
let importOp = window.msCrypto.subtle.importKey('raw',
keyBuf,
{ name: 'AES-GCM' },
false,
['encrypt', 'decrypt']);
importOp.oncomplete = function(e) {
key = e.target.result;
let encryptOp = window.msCrypto.subtle.encrypt({
name: 'AES-GCM',
iv: iv,
tagLength: 128,
additionalData: additionalData
}, key, plainText);
encryptOp.oncomplete = function (e) {
encResult = e.target.result;
};
};
Run Code Online (Sandbox Code Playgroud)
结果项目(encResult)是AesGcmEncryptResult,它具有2个不同属性的加密值和标记。据我了解,我需要将它们连接起来,并将它们作为要解密的密文传递,如下所示:
let cipherText = new Uint8Array(plainText.length + 16); // …Run Code Online (Sandbox Code Playgroud) javascript encryption aes-gcm internet-explorer-11 webcrypto-api
我正在Chrome(自从第一个Web Crypto支持以来),Firefox(自有第一个Web Crypto支持以来)以及甚至在Safari TP(10.2)上都成功使用了Web Crypto API(https://www.w3.org/TR/WebCryptoAPI/)在WebCrypto Liner的支持下,WebCrypto API的pollyfill(https://github.com/PeculiarVentures/webcrypto-liner)。
现在,我想使用Microsoft Edge测试我们的代码。但是加密和解密样本ArrayBuffer已经失败。这里的代码:
var crypto = window.crypto;
if (crypto.subtle) {
var aesGcmKey = null;
// always create a new, random iv in production systems!!!
var tempIv = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
// needed for edge, if additional data missing decrypting is failing
var tempAdditionalData = new Uint8Array(0);
var dataToEncrypt = new Uint8Array([1, 2, 3, 4, 5]); …Run Code Online (Sandbox Code Playgroud) 我有公钥和私钥作为字符串,它们是从Webcrypto API RSA-OAEP算法生成的.我想通过使用那些纯文本加密和解密,并在尝试将字符串转换为字节数组时获得异常
Java代码:
package mailsend;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
public class RsaOaep {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
//Encryption
byte[] input = "{\"userid\":\"1242\",\"appid\":\"1234\",\"tenentid\":\"4567\",\"sessionid\":\"session1234567\"}".getBytes();
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
SecureRandom random = new SecureRandom();
String publicKey="{\n" +
" \"alg\": \"RSA-OAEP-256\",\n" +
" \"e\": \"AQAB\",\n" +
" \"ext\": true,\n" +
" \"key_ops\": [\n" +
" \"encrypt\"\n" +
" ],\n" +
" \"kty\": \"RSA\",\n" + …Run Code Online (Sandbox Code Playgroud) 我正在成功使用webcrypto API加密服务器和客户端之间的消息(假设我需要手动执行此操作)。
我的问题是我需要检查用户和服务器的密钥对是否已经存在,而不是一直生成新的密钥对。有没有办法检查它是否存在并检索它以解密服务器消息?
为了明确起见,我privateKey在浏览器上,publicKey并被发送到服务器。
我有一个nodejs服务器和普通的JS前端。
提前致谢。
我想知道如何解决这个问题。我使用 WebCrypto API 生成 RSA-OAEP 密钥对,然后从导出为 ArrayBuffer 的密钥对中导出 pkcs8 中的私钥,并且我想将此 ArrayBuffer 编码为 base64,以便可以将其存储为 PEM。
\n\n在此测试示例中,我将密钥导出为 pkcs8 并将此 pkcs8 导入回 CryptoKey。问题是有时有效有时无效。
\n\n这些是代码的结果:\n注意:仅发生这些状态之一,而不是同时发生。\n注意2:此示例不包含 -----BEGIN PRIVATE KEY----- 前缀和后缀 我仅对钥匙。
\n\nCase1:未捕获(在承诺中)URIError:URI格式错误(\ xe2 \ x80 \ xa6)b64DecodeUnicode @ try.php:20b64toab @ try.php:70wayBack @ try.php:66(匿名函数)@ try.php:56
\n\nCase2:未定义:1未捕获(承诺中)DOMException
\n\nCase3:好的 - 一直可以正常工作。
\n\n我不知道是什么原因导致了错误,但我认为这与 base64 编码有关。正如我所说,有时私钥生成正常,有时则不行。
\n\n非常感谢您提前提供的每一个帮助。
\n\nfunction b64EncodeUnicode(str) {\n return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {\n return String.fromCharCode(\'0x\' + p1);\n }));\n}\n\nfunction b64DecodeUnicode(str) {\n return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {\n return \'%\' + (\'00\' + c.charCodeAt(0).toString(16)).slice(-2);\n …Run Code Online (Sandbox Code Playgroud) 当我尝试在 Firefox 上使用 Crypto API 时遇到了问题。(文档)当我尝试使用此函数加密明文时,出现类型错误:
window.crypto.subtle.encrypt(algo_enc,key,padded_clear_txt);
Run Code Online (Sandbox Code Playgroud)
(文档)
这就是我定义参数的方式:
算法_enc:
var iv = new Int32Array(4) ;//4-32 bit integers (128 bits)
window.crypto.getRandomValues(iv); //defining the IV
var algo_enc = {"name": "AES-CBC", iv}
Run Code Online (Sandbox Code Playgroud)
钥匙:
var alg_key = {"name":"AES-CBC","length":128};
var key = window.crypto.subtle.generateKey(alg_key,false,["encrypt","decrypt"]);
Run Code Online (Sandbox Code Playgroud)
padded-clear-txt 是我想要加密的 256 位 (2*128) 消息。
这是我执行加密函数时遇到的错误:
SubtleCrypto.encrypt 的参数 2 没有实现 CryptoKey 接口。
密钥的生成很顺利,它是一个 CryptoKey 对象,但我仍然收到此错误。所以也许这是我应该报告的错误......
我有以下简单的代码:
inputBytes = new TextEncoder().encode(inputString)
hashBytes = await window.crypto.subtle.digest("SHA-256", inputBytes)
console.log((typeof hashBytes) + ": " + JSON.stringify(hashBytes))
Run Code Online (Sandbox Code Playgroud)
为什么结果是空对象?我怎样才能得到真正的结果?
这太奇怪了,非常感谢任何帮助
webcrypto-api ×10
javascript ×7
encryption ×5
cryptography ×3
base64 ×2
aes-gcm ×1
arraybuffer ×1
firefox ×1
hash ×1
java ×1
node-crypto ×1
node.js ×1
pem ×1
promise ×1
rsa ×1
web-worker ×1