웹 암호화 API로 생성 된 RSA 개인 키를 암호로 보호하려고했습니다. ,AES 키로 RSA 개인 키 래핑 후 풀기
- 을 이렇게하려면 내가 먼저 RSA 키 쌍
- 을 생성 그럼 그럼 난 2 단계에서 AES 키를 1 단계에서 RSA 개인 키를 랩 암호
- 에서 AES 대칭 키를 파생. 나는 끝났어요 때
- , 나는 내가 그것을
랩을 해제하려고 할 곳이 바로 unwrap
방법이 모두를 전달하여 일을 확인하려고 코드입니다 :
<html>
<script>
function wrap(password) {
var iterations = 1000000;
// Utility function
var stringToByteArray = function(s){
if ("TextEncoder" in window) {
encoder = new window.TextEncoder;
return encoder.encode(s);
}
var result = new Uint8Array(s.length);
for (var i=0; i<s.length; i++){
result[i] = s.charCodeAt(i);
}
return result;
}
var saltBytes = stringToByteArray("NaCl");
var passphraseBytes = stringToByteArray(password);
return crypto.subtle.generateKey({
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: {name: "SHA-256"}
}, true, ["encrypt", "decrypt"]).then(function(keyPair) {
return crypto.subtle.importKey(
"raw", passphraseBytes, {name: "PBKDF2"}, false, ["deriveKey"]
).then(function(baseKey) {
return window.crypto.subtle.deriveKey(
{name: "PBKDF2", salt: saltBytes, iterations: iterations, hash: "SHA-1"},
baseKey,
{name: "AES-CBC", length: 256},
false,
["encrypt", "decrypt", "wrapKey", "unwrapKey"]
).then(function(wrapperKey) {
var iv = crypto.getRandomValues(new Uint8Array(16));
return crypto.subtle.wrapKey(
"pkcs8",
keyPair.privateKey,
wrapperKey,
{name: "AES-CBC", iv: iv }
).then(function(wrappedKey) {
return {
iv: iv,
wrapper: wrapperKey,
wrapped: wrappedKey
}
})
});
}).catch(function(err) {
console.log(err);
});
})
}
function unwrap(account) {
console.log(account);
crypto.subtle.unwrapKey(
"pkcs8",
account.wrapped,
account.wrapper,
{
name: "AES-CBC",
iv: account.iv
},
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: {name: "SHA-256"}
},
true,
['decrypt', 'encrypt']
).then(function(privateKey) {
console.log("unwrapped = ", privateKey);
}).catch(function(e) {
console.log(e)
})
}
// Finally I call "wrap" and then "unwrap"
wrap("password").then(unwrap)
</script>
</html>
그러나 코드는 작동하지 않습니다. 래핑 코드는 오류를 발생시키지 않으며 키를 생성하는 것으로 보입니다 (유효하지 않더라도 이러한 것이 있는지는 모르겠지만). 나는이 작업을 얻을 수 없기 때문에
DOMException: Cannot create a key using the specified key usages.
내가 지난 24 시간 동안 내 머리 밖으로 당겨 봤는데 : 나는 unwrapKey
방법을 실행하려고 할 때, 나는 다음과 같은 오류가 발생합니다. 누구든지 문제를 발견 할 수 있습니까? 이 코드는 완벽하게 자체 포함 된 코드이므로 HTML 파일로 복사하여 붙여 넣기하여 브라우저에서 열어 볼 수 있습니다.
당신이 모질라 문서를 기반으로 말하기하는 경우는 내가 그들의 문서는 불완전하고 때로는 적어도 WebCrypto의 API에 대한 오해의 소지가 있음을 알게하기 때문에 WebCrypto의 API에 올 때, 나는 그들을 신뢰하지 않습니다. 최초의 W3C 제안서에는 이러한 제한이 없으며 스펙에는 단지 암호화 메커니즘이라고 명시되어 있습니다. 또한이 질문을했기 때문에이 라이브러리를 발견했습니다. https://github.com/safebash/OpenCrypto – Vlad
실제로 Mozilla 문서이며 현재 대부분의 (모든?) 현재 주요 브라우저의 상태입니다. . 또한 W3C는 랩핑을 제한하지 않는다고 말합니다. 그러나 위의 내용은 개발자 용 broswer API를 통해 사용할 수있는 것입니다. 귀하의 요구 사항을 구현하는 솔루션을 찾았다면 좋았습니다. 모두 : MBC의 나쁜 구현 (주요 브라우저 컨소시엄) ... 새해 복 많이 받으세요! –