我有一个非常小的节点脚本来创建公钥/私钥是否有任何方法可以在客户端进行,而无需浏览孔加密模块?
var crypto = require('crypto');
var userCurve = crypto.createECDH('prime256v1');
var userPublicKey = userCurve.generateKeys()
var userPrivateKey = userCurve.getPrivateKey();
Run Code Online (Sandbox Code Playgroud)
到目前为止我试过这个:
// https://github.com/diafygi/webcrypto-examples#ecdh---generatekey
window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256", //can be "P-256", "P-384", or "P-521"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["deriveKey", "deriveBits"] //can be any combination of "deriveKey" and "deriveBits"
)
.then(function(key){
//returns a keypair object
console.log(key);
console.log(key.publicKey);
console.log(key.privateKey);
})
.catch(function(err){
console.error(err);
});
Run Code Online (Sandbox Code Playgroud)
但是当我记录它时,它看起来与节点版本完全不同
现在,我正在通过简单的测试来测试 Web Crypto API。所以,我有用户的公钥(作为字符串),我想让他传递他的私钥(也作为字符串),所以我的应用程序可以做一些加密/解密。因此,我尝试通过执行以下操作将他的密钥导入到 Web Crypto API:
var textEncoder = new TextEncoder();
var alg = {
name: "RSA-OAEP",
hash: {name: "SHA-256"}
}
window.crypto.subtle.importKey('raw', textEncoder.encode(myPublicKey), alg, false, ['encrypt'])
Run Code Online (Sandbox Code Playgroud)
密钥由
openssl genrsa -out mykey.pem 4096
openssl rsa -in mykey.pem -pubout > mykey.pub
Run Code Online (Sandbox Code Playgroud)
WCAPI 抛出
Unsupported import key format for algorithm
Run Code Online (Sandbox Code Playgroud)
我在 alg 中尝试了其他哈希,但仍然没有成功。
一个例子的帮助会很好。
我想使用 sha512 和迭代计数将我的字符串转换为 PBKDF2。我在 nodejs 中使用“pbkdf2”模块做了。我如何在 angular JS 中实现相同的目标。
我正在尝试导入现有的密钥,但无论我做什么,我都会得到:“AES 密钥数据必须是 128 位或 256 位”
我有一个从 0 到 255 的 128 个 int ArrayBuffer,即使我用 Uint8Array 包装它也不起作用。即使是新的 Uint8Array(128) 也会返回相同的错误。
crypto.subtle.importKey("raw", new Uint8Array(128), { name: "AES-CBC" }, true, ["encrypt", "decrypt"]).then(cryptoKey => {
console.log(cryptoKey);
}).catch(err => {
console.log(err);
});Run Code Online (Sandbox Code Playgroud)
我正在使用 WebCrypto,但输出令人困惑。
以下测试用例使用新生成的 128 位密钥和 128 位随机 IV 加密随机 16 字节(128 位)纯文本,但输出 32 字节(256 位)输出。
如果我记得 AES-CBC 的细节,它应该输出 128 位块。
function test() {
var data = new Uint8Array(16);
window.crypto.getRandomValues(data);
console.log(data)
window.crypto.subtle.generateKey(
{
name: "AES-CBC",
length: 128,
},
false,
["encrypt", "decrypt"]
)
.then(function(key){
//returns a key object
console.log(key);
window.crypto.subtle.encrypt(
{
name: "AES-CBC",
iv: window.crypto.getRandomValues(new Uint8Array(16)),
},
key,
data
)
.then(function(encrypted){
console.log(new Uint8Array(encrypted));
})
.catch(function(err){
console.error(err);
});
})
.catch(function(err){
console.error(err);
});
}
Run Code Online (Sandbox Code Playgroud)
示例输出:
Uint8Array(16) [146, 207, 22, 56, 56, 151, 125, 174, …Run Code Online (Sandbox Code Playgroud) 我想在浏览器中使用加密算法。我看到有两种方法可以做到这一点。首先,使用可用的 javascript 加密框架,如 cryptojs、sjcl 等,或浏览器的内置 Web Crypto API。我很困惑哪个更好。任何人都可以帮我解决这个问题吗?
谢谢
在该用例中,服务器向浏览器发送加密的blob,并且浏览器上的javascript随后从服务器请求解密密钥并将blob解密为可用内容.
有没有办法保护浏览器上的这个键免受书签或浏览器插件的攻击或用户在浏览器上单步调试javascript调试器?或至少使攻击者的问题稍微困难一些.
编辑:问题的上下文是EME规范中指定的HTML Video DRM.有一个ClearKey api是该标准的一部分,并且不需要来自WideVine或FairPlay等的封闭源插件.但是,正如多个响应指出的那样,ClearKey无法得到保护.(遗憾的是,这意味着使用propinent DRM插件).
javascript ×6
cryptography ×4
aes ×2
node.js ×2
angularjs ×1
cryptojs ×1
encryption ×1
openssl ×1
pbkdf2 ×1
rsa ×1
webassembly ×1