首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SubtleCrypto.wrapKey用法:可以exportKey但不能wrapKey?

SubtleCrypto.wrapKey用法:可以exportKey但不能wrapKey?
EN

Stack Overflow用户
提问于 2020-02-03 08:13:34
回答 1查看 305关注 0票数 0

我正在尝试使用SubtleCrypto应用程序接口,使用AES-256密钥包装密钥来包装RSA私有密钥。我已经设法使用generateKey生成了RSA密钥和密钥包装AES密钥(外加另一个对称的AES密钥),但是wrapKey在RSA私有密钥上失败了。

我可以成功地export然后import RSA私有密钥,并且我可以成功地wrapunwrap一个通用的AES密钥。但是当我试图包装RSA私钥时,我得到了一个“提供给操作的数据不满足要求”的错误,并且我不知道我做错了什么。

用于测试的HTML文件:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="eng-US">
  <head>
    <meta charset="UTF-8">
    <title>Test WebCrypto.subtle</title>
  </head>
  <body>
    <h1>Test WebCrypto.subtle Functions</h1>
    <div id="output">
    </div>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.js"></script>
    <script type="text/javascript">
     function TestCrypto( ) {
       $('#output').append( 'Starting test...<br/>' );
       let info = {};

       window.test_result = info;

       let seq = crypto.subtle.generateKey( { name: 'RSA-OAEP',
                                              modulusLength: 1024,
                                              publicExponent: new Uint8Array( [ 1, 0, 1 ] ),
                                              hash: 'SHA-256' },
                                            true,   // extractable
                                            [ 'encrypt', 'decrypt' ] )
                       .then( (rsaKey) =>
                         {
                           $('#output').append( 'Created RSA key<br/>' );
                           info.RSA = rsaKey;
                           return crypto.subtle.generateKey( { name: 'AES-GCM',
                                                               length: 256 },
                                                             true,
                                                             [ 'encrypt', 'decrypt' ] )
                         } )
                       .then( (symKey) =>
                         {
                           $('#output').append( 'Created symmetric AES key<br/>' );
                           info.AES = symKey;
                           return crypto.subtle.generateKey( { name: 'AES-KW',
                                                               length: 256 },
                                                             true,
                                                             [ 'wrapKey', 'unwrapKey' ] )
                         } )
                       .then( (kek) =>
                         {
                           $('#output').append( 'Created key-encryption AES key<br/>' );
                           info.KEK = kek;
                           return crypto.subtle.exportKey( 'pkcs8',
                                                           info.RSA.privateKey );
                         } )
                       .then( (expPrivKey) =>
                         {
                           $('#output').append( 'Exported RSA private key<br/>' );
                           info.exportedPrivateKey = expPrivKey;
                           return crypto.subtle.importKey( 'pkcs8',
                                                           info.exportedPrivateKey,
                                                           { name: 'RSA-OAEP',
                                                             hash: 'SHA-256' },
                                                           true,
                                                           [ 'decrypt', 'unwrapKey' ] );
                         } )
                       .then( (impPrivKey) =>
                         {
                           $('#output').append( 'Imported RSA private key<br/>' );
                           info.importedPrivateKey = impPrivKey;
                           /* Wrapping & unwrapping an AES key works...
                           return crypto.subtle.wrapKey( 'raw',
                                                         info.AES,
                                                         info.KEK,
                                                         'AES-KW' );
                            */
                           /* Wrapping & unwrapping an RSA key doesn't work... */
                           return crypto.subtle.wrapKey( 'pkcs8',
                                                         info.RSA.privateKey,
                                                         info.KEK,
                                                         'AES-KW' );
                         } )
                       .then( (wrappedKey) =>
                         {
                           $('#output').append( 'Wrapped a key<br/>' );
                           info.wrappedKey = wrappedKey;
                           /*
                           return crypto.subtle.unwrapKey( 'raw',
                                                           info.wrappedKey,
                                                           info.KEK,
                                                           'AES-KW',
                                                           { name: 'AES-GCM',
                                                             length: 256 },
                                                           true,
                                                           [ 'encrypt', 'decrypt' ] );
                           */
                           return crypto.subtle.unwrapKey( 'pkcs8',
                                                           info.wrappedPrivateKey,
                                                           info.KEK,
                                                           'AES-KW',
                                                           { name: 'RSA-OAEP',
                                                             // These items shouldn't be needed, but don't help
                                                             // even if you add them.
                                                             // modulusLength: 1024,
                                                             // publicExponent: new Uint8Array( [ 1, 0, 1 ] ),
                                                             hash: 'SHA-256' },
                                                           true,
                                                           [ 'decrypt', 'unwrapKey' ] );
                         } )
                       .then( (unwrappedKey) =>
                         {
                           $('#output').append( 'Unwrapped the key<br/>' );
                           info.unwrappedKey = unwrappedKey;
                           $('#output').append( 'Testing complete, check console for results<br/>' );
                           console.log( info );
                           window.test_result = info;
                           return info;
                         } )
                       .catch( (e) =>
                         {
                           $('#output').append( 'Key testing failed<br/>' )
                                       .append( e.toString( ) )
                                       .append( '<br/>' );
                         } );
     }

     $(document).ready( TestCrypto );
    </script>
  </body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2020-02-03 23:17:10

经过大量的测试和重新阅读文档,我找到了解决这个问题的部分:

与另一种

模式相比,使用AES-KW的一个优点是AES-KW不需要初始化向量。要使用AES-KW,输入必须是64 bits.

倍数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60312259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档