44/*
55
66
7- aes encrypt / decrypt
7+ aes encrypt / decrypt browser
88
99
101030-04-26
@@ -20,72 +20,127 @@ aes encrypt / decrypt
2020 var key = await generateAesKey ( ) ;
2121 var encrypted = await aesEncrypt ( key , 'hello world' ) ;
2222 var txt = await aesDecrypt ( key , encrypted ) ;
23-
24- console . log ( txt ) ;
25-
26-
27-
23+ console . log ( txt ) ;
24+
25+
26+
2827 async function generateAesKey ( ) {
2928
30- var key = await crypto . subtle . generateKey ( { name :"AES-GCM" , length : 256 } , true , [ "encrypt" , "decrypt" ] ) ;
29+ var algorithm = { name :'AES-GCM' , length :256 } ;
30+ var extractable = true ;
31+ var keyusages = [ 'encrypt' , 'decrypt' ] ;
32+
33+ var key = await crypto . subtle . generateKey ( algorithm , extractable , keyusages ) ;
34+
3135 return key ;
3236
3337 } //generateAesKey
3438
3539
3640 async function exportAesKey ( key ) {
3741
38- const raw = await crypto . subtle . exportKey ( "raw" , key ) ;
39- var b64 = btoa ( String . fromCharCode ( ...new Uint8Array ( raw ) ) ) ;
42+ var buf = await crypto . subtle . exportKey ( 'raw' , key ) ;
43+
44+ var b64 = buf_b64 ( buf ) ;
4045 return b64 ;
4146
4247 } //exportAesKey
4348
4449
45- async function importAesKey ( base64 ) {
50+ async function importAesKey ( b64 ) {
4651
47- const raw = Uint8Array . from ( atob ( base64 ) , c => c . charCodeAt ( 0 ) ) ;
48- var key = await crypto . subtle . importKey ( "raw" , raw , "AES-GCM" , true , [ "encrypt" , "decrypt" ] ) ;
52+ var format = 'raw' ;
53+ var keydata = b64_uint8 ( b64 ) ;
54+ var algorithm = 'AES-GCM' ;
55+ var extractable = true ;
56+ var keyusage = [ 'encrypt' , 'decrypt' ] ;
57+
58+ var key = await crypto . subtle . importKey ( format , keydata , algorithm , extractable , keyusage ) ;
59+
4960 return key ;
5061
5162 } //importAesKey
5263
5364
54- async function aesEncrypt ( key , plaintext ) {
65+ async function aesEncrypt ( key , txt ) {
5566
56- const iv = crypto . getRandomValues ( new Uint8Array ( 12 ) ) ; // 96-bit IV recommended
67+ var buf = txt_buf ( txt ) ;
68+ // 96-bit IV recommended
69+ var iv = crypto . getRandomValues ( new Uint8Array ( 12 ) ) ;
70+
71+ var algorithm = { name :'AES-GCM' , iv}
72+ var key = key ;
73+ var data = encoded ;
5774
58- const encoded = new TextEncoder ( ) . encode ( plaintext ) ;
75+ var buf = await crypto . subtle . encrypt ( algorithm , key , data ) ;
5976
60- const ciphertext = await crypto . subtle . encrypt ( { name : "AES-GCM" , iv} , key , encoded ) ;
77+ iv = buf_b64 ( iv ) ;
78+ var data = buf_b64 ( buf ) ;
6179
62- return {
63- iv : btoa ( String . fromCharCode ( ...iv ) ) ,
64- data : btoa ( String . fromCharCode ( ...new Uint8Array ( ciphertext ) ) ) ,
65- } ;
80+ var encrypted = { iv, data} ;
81+ return encrypted ;
6682
6783 } //aesEncrypt
6884
6985
70- async function aesDecrypt ( key , encrypted ) {
86+ async function aesDecrypt ( key , encrypted ) {
7187
72- const iv = Uint8Array . from ( atob ( encrypted . iv ) , c => c . charCodeAt ( 0 ) ) ;
73- const data = Uint8Array . from ( atob ( encrypted . data ) , c => c . charCodeAt ( 0 ) ) ;
88+ var { iv, data} = encrypted ;
7489
75- const plaintext = await crypto . subtle . decrypt (
76- {
77- name : "AES-GCM" ,
78- iv,
79- } ,
80- key ,
81- data
82- ) ;
90+ iv = b64_uint8 ( iv ) ;
8391
84- return new TextDecoder ( ) . decode ( plaintext ) ;
92+ var algorithm = { name :'AES-GCM' , iv} ;
93+ key = key ;
94+ data = b64_uint8 ( data ) ;
95+
96+ var buf = await crypto . subtle . decrypt ( algorithm , key , data ) ;
97+ var txt = buf_txt ( buf ) ;
98+
99+ return txt ;
85100
86101 } //aesDecrypt
87102
88103
104+ //:
105+
106+
107+ function buf_b64 ( buf ) {
108+
109+ var uint8 = new Uint8Array ( buf ) ;
110+ var bin = String . fromCharCode ( ...uint8 ) ;
111+ var b64 = btoa ( bin ) ;
112+ return b64 ;
113+
114+ } //buf_b64
115+
116+
117+ function b64_uint8 ( b64 ) {
118+
119+ var bin = atob ( b64 ) ;
120+ var uint8 = Uint8Array . from ( bin , c => c . charCodeAt ( 0 ) ) ;
121+ return uint8 ;
122+
123+ } //b64_buf
124+
125+
126+ function txt_buf ( txt ) {
127+
128+ var buf = new TextEncoder ( ) . encode ( plaintext ) ;
129+ return buf ;
130+
131+ } //txt_buf
132+
133+
134+ function buf_txt ( buf ) {
135+
136+ var txt = new TextDecoder ( ) . decode ( buf ) ;
137+ return txt ;
138+
139+ } //buf_txt
140+
141+
142+
143+
89144
90145
91146} ) ( ) ;
0 commit comments