js的RSA和AES加密解密

转自:http://www.oschina.net/code/snippet_552425_46178

在看web api时发现浏览器竟然提供了原生的RSA和AES的加密解密方法。然后就试了一下,速度还是很快的。相关内容https://developer.mozilla.org/en-US/docs/Web/API/Crypto, google的例子 https://chromium.googlesource.com/chromium/blink/+/72fef91ac1ef679207f51def8133b336a6f6588f/LayoutTests/crypto(这个是chromium的测试代码,里面包含了使用的方法)
标签: <无>

代码片段(1)[全屏查看所有代码]

1. [代码]原生的js RSA加密和解密     跳至 [1] [全屏预览]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!doctype html>
<html>
    <head>
        <meta charset='UTF-8'>
    </head>
    <body>
        <div class='test'></div>
        <script type="text/javascript">
 
            function encrypt(data, keyJSON){
                var data = new TextEncoder("UTF-8").encode(data);
                var randomsKeys = geneRandomHexStr(64); // 128 bit keys
                var encryptedKey = hexStringToUint8Array(randomsKeys);
                var aesAlgo = {name: 'aes-cbc', iv: hexStringToUint8Array("000102030405060708090a0b0c0d0e0f")};
                return crypto.subtle.importKey("jwk", keyJSON, {name: "rsa-oaep", hash: {name: "sha-256"}},true, ['encrypt'])
                    .then(function(publicKey){
                        return crypto.subtle.encrypt({name: "rsa-oaep"}, publicKey, encryptedKey);
                    }).then(function(res){
                        encryptedKey = bytesToHexString(res)
                        // use aes to encrypt data
                        // import aes key
                        return crypto.subtle.importKey('raw',
                            hexStringToUint8Array(randomsKeys) , aesAlgo, false, ['encrypt', 'decrypt']);
                         
                    }).then(function(result){
                        // use aes to encode
                        return crypto.subtle.encrypt(aesAlgo,
                         result, data);
                    }).then(function(encryptedData){
                        return Promise.resolve({
                            'encrypted': bytesToHexString(encryptedData),
                            'encryptedKey': encryptedKey,
                        });
                    });
 
                //console.log(new TextDecoder("UTF-8").decode(data));
                // use server public key to encrypt
                 
            }
 
            function decrypt(data, keyJSON){
                // use local private key to decrypt
                var encryptedKey = new hexStringToUint8Array(data.encryptedKey);
                var encryptedData = new hexStringToUint8Array(data.encrypted);
                var aesAlgo = {name: 'aes-cbc', iv: hexStringToUint8Array("000102030405060708090a0b0c0d0e0f")};
                // decrypt key
                return crypto.subtle.importKey('jwk', keyJSON, {name: "rsa-oaep", hash: {name: "sha-256"}}, true,
                    ['decrypt']).then(function(privateKey){
                        return crypto.subtle.decrypt({name: 'rsa-oaep'}, privateKey, encryptedKey);
                    }).then(function(decryptedKey){
                        // import aes key
                        return crypto.subtle.importKey('raw',
                            decryptedKey, aesAlgo, false, ['encrypt', 'decrypt']);
                    }).catch(function(){
                        console.error("decrypt error");
                    }).then(function(result){
                        // decode encrypted data
                        return crypto.subtle.decrypt(aesAlgo, result, encryptedData);
                    }).then(function(data){
                        return Promise.resolve(new TextDecoder("UTF-8").decode(new Uint8Array(data)));
                    })
 
            }
 
            function createNewUserKey(){
                var algorithmKeyGen = {
                    name: "RSA-OAEP",
                    hash: {name: "sha-256"},
                    // RsaKeyGenParams
                    modulusLength: 2048,
                    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),  // Equivalent to 65537
                };
                var nonExtractable = false;
                 
                var publicKey = "";
                var privateKey = "";
                var keyPairs = "";
                return crypto.subtle.generateKey(algorithmKeyGen, true, ['encrypt', 'decrypt']).then(function(result) {
                    // gene key pair
                    keyPairs = result;
                    return Promise.all([crypto.subtle.exportKey("jwk", keyPairs.publicKey),
                        crypto.subtle.exportKey("jwk", keyPairs.privateKey)]);
                })
                 
            }
 
            function _arrayBufferToBase64( buffer ) {
                var binary = '';
                var bytes = new Uint8Array( buffer );
                var len = bytes.byteLength;
                for (var i = 0; i < len; i++) {
                    binary += String.fromCharCode( bytes[ i ] );
                }
                return window.btoa( binary );
            }
 
            function hexStringToUint8Array(hexString) {
                if (hexString.length % 2 != 0)
                    throw "Invalid hexString";
                var arrayBuffer = new Uint8Array(hexString.length / 2);
                for (var i = 0; i < hexString.length; i += 2) {
                    var byteValue = parseInt(hexString.substr(i, 2), 16);
                    if (byteValue == NaN)
                        throw "Invalid hexString";
                    arrayBuffer[i/2] = byteValue;
                }
                return arrayBuffer;
            }
 
            function bytesToHexString(bytes) {
                if (!bytes)
                    return null;
                bytes = new Uint8Array(bytes);
                var hexBytes = [];
                for (var i = 0; i < bytes.length; ++i) {
                    var byteString = bytes[i].toString(16);
                    if (byteString.length < 2)
                        byteString = "0" + byteString;
                    hexBytes.push(byteString);
                }
                return hexBytes.join("");
            }
 
            function geneRandomHexStr(length){
                var text = "";
                var possible = "0123456789abcdef";
 
                for( var i=0; i < length; i++ )
                    text += possible.charAt(Math.floor(Math.random() * possible.length));
 
                return text;
            }
 
            createNewUserKey().then(function(keyPairs){
                encrypt("this is origin text", keyPairs[0]).then(function(res){
                    console.log('public', JSON.stringify(keyPairs[0]));
                    console.log('private', JSON.stringify(keyPairs[1]));
                    decrypt(res, keyPairs[1]).then(function(decrypted){
                        console.log('decrypted', decrypted);
                    });
                });
            })
 
        </script>
    </body>
</html>

展开阅读全文

没有更多推荐了,返回首页