You can not select more than 25 topics 
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long. 
		
		
		
		
		
			
	
	
		
			
	
		
			This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
		 
	
		
		
			
				
				
					
						
						
						
							---
 
						
						
						
							layout: post
 
						
						
						
							title: Web Crypto Api学习笔记
 
						
						
						
							tags: [学习笔记, Web Crypto Api]
 
						
						
						
							---
 
						
						
						
							  感觉要爆炸了……<!--more-->     
 
						
						
						
							  
 
						
						
						
							# 学习前
  
						
						
						
							 
						
						
						
							  Javascript是真的垃圾, , , , , , , , , , , , , ~~(跑题了:-P)     
 
						
						
						
							  不过浏览器只支持Javascript语言……没办法, & Paste大法了。不过国内用Web Crypto Api的人好少, ,  
						
						
						
							  
 
						
						
						
							# 学习过程
  
						
						
						
							, , ! , ( , ) , , ,  
						
						
						
							  看着示例代码, , , , , ?  
						
						
						
							  不过我还是发挥了作为辣鸡程序员的特长——Copy& Paste大法,  
						
						
						
							
 
						
						
						
							# 辣鸡代码
  
						
						
						
							```js
  
						
						
						
							function  getByteLen ( val )  { 
 
						
						
						
							    var  len  =  0 ; 
 
						
						
						
							    for  ( var  i  =  0 ;  i  <  val . length ;  i ++ )  { 
 
						
						
						
							        if  ( val [ i ]. match ( /[^\x00-\xff]/ig )  !=  null )  len  +=  3 ; 
 
						
						
						
							        else  len  +=  1 ; 
 
						
						
						
							    } 
 
						
						
						
							    return  len ; 
 
						
						
						
							} 
 
						
						
						
							  function  importSecretKey ( rawKey )  { 
 
						
						
						
							    return  window . crypto . subtle . importKey ( 
 
						
						
						
							      "raw" , 
 
						
						
						
							      rawKey , 
 
						
						
						
							      "AES-GCM" , 
 
						
						
						
							      true , 
 
						
						
						
							      [ "encrypt" ,  "decrypt" ] 
 
						
						
						
							    ); 
 
						
						
						
							  } 
 
						
						
						
							  async  function  encryptMessage ( key )  { 
 
						
						
						
								iv  =  window . crypto . getRandomValues ( new  Uint8Array ( 12 )); 
 
						
						
						
							    ciphertext  =  await  window . crypto . subtle . encrypt ( 
 
						
						
						
							      { 
 
						
						
						
							        name :  "AES-GCM" , 
 
						
						
						
							        iv :  iv 
 
						
						
						
							      }, 
 
						
						
						
							      key , 
 
						
						
						
							      encoded 
 
						
						
						
							    ); 
 
						
						
						
							
 
						
						
						
							console . log ( ciphertext ) 
 
						
						
						
							  } 
 
						
						
						
							    let  secretKey ; 
 
						
						
						
							const  enc  =  new  TextEncoder (); 
 
						
						
						
							keyword = "Mayx" 
 
						
						
						
							    while  ( getByteLen ( keyword )  %  16  !=  0 )  { 
 
						
						
						
							        keyword  =  keyword  +  "\0" ; 
 
						
						
						
							    } 
 
						
						
						
							const  rawKey  =  enc . encode ( keyword ); 
 
						
						
						
							
 
						
						
						
							const  encoded  =  enc . encode ( "Mayx is Good" ); 
 
						
						
						
							( async  ()  =>  { 
 
						
						
						
							    secretKey  =  await  importSecretKey ( rawKey ); 
 
						
						
						
								encryptMessage ( secretKey ); 
 
						
						
						
							})(); 
 
						
						
						
							
 
						
						
						
							``` 
 
						
						
						
							
 
						
						
						
							# 感想