摘要:
大部分动态网站都支持从客户端到服务器传递数据,如果传递的数据被别人截取就非常危险,尤其是一些用户名密码之类的。这时候就需要我们在传递数据之前对数据进行加密。现在的加密算法已经有很多了,例如:md5,SHA-1,SHA-256,AES,Rabbit,MARC4,HMAC等等。下面分享几款前端加密的插件。
1 var clientSideEncryptionKey = "YOUR_ENCRYPTION_KEY";2 var braintree = Braintree.create(clientSideEncryptionKey);3 $(#transaction_form).submit(function() {4 encryptedForm().submit();5 return false;6 });
1 var rsa = require("./src/rsa.js"); 2 var key = new rsa.Key(); 3 ? 4 var message = "All your bases are belong to us."; 5 console.log("Message:/n"+message+"/n"); 6 7 // Generate a key 8 key.generate(1024, "10001"); 9 console.log("Key:/n");10 console.log("n:" + key.n.toString(16));11 console.log("e:" + key.e.toString(16));12 console.log("d:" + key.d.toString(16));13 console.log("/n");14 15 // Encrypt16 var encrypted = key.encrypt(message);17 console.log("Encrypted:/n" + rsa.linebrk(encrypted, 64) + "/n" );18 19 // Decrypt20 var decrypted = key.decrypt(encrypted);21 console.log("Decrypted:" + rsa.linebrk(decrypted, 64) + "/n");22 23 var sig = key.signString(message, "sha256");24 console.log("String signature: /n" + rsa.linebrk(sig, 64));25 26 var pubkey = new rsa.Key();27 pubkey.n = key.n;28 pubkey.e = key.e;29 30 var verified = pubkey.verifyString(message, sig);31 32 console.log("Verfied: " + verified);
1 // quick test 2 var input1 = "ABC"; 3 var input2 = ["A","B","C"]; 4 var input3 = [0x41, 0x42, 0x43]; 5 alert(faultylabs.MD5(input1) == "902FBDD2B1DF0C4F70B4A5D23525E932"); 6 alert(faultylabs.MD5(input2) == "902FBDD2B1DF0C4F70B4A5D23525E932"); 7 alert(faultylabs.MD5(input3) == "902FBDD2B1DF0C4F70B4A5D23525E932"); 8 9 // ArrayBuffer10 var abuf = new ArrayBuffer(3)11 var vu8 = new Uint8Array(abuf)12 vu8[0] = 0x41; vu8[1] = 0x42; vu8[2] = 0x4313 alert(faultylabs.MD5(abuf) == "902FBDD2B1DF0C4F70B4A5D23525E932")14 15 // Typed Array Uint32Array16 var au32 = new Uint32Array(1)17 au32[0] = 0xDDCCBBAA 18 alert(faultylabs.MD5(au32) == "CA6FFBF95B47864FD4E73F2601326304")
1 var CryptoJS = require("crypto-js");2 console.log(CryptoJS.HmacSHA1("Message", "Key"));
1 sjcl.encrypt("passWord", "data")2 sjcl.decrypt("password", "encrypted-data")
1 $(function() {2 $("form").jCryption();3 });
1 // new MD5 instance 2 var MD5 = new Hashes.MD5; 3 // new SHA1 instance 4 var SHA1 = new Hashes.SHA1; 5 // new SHA256 instance 6 var SHA256 = new Hashes.SHA256; 7 // new SHA512 instace 8 var SHA512 = new Hashes.SHA512; 9 // new RIPEMD-160 instace10 var RMD160 = new Hashes.RMD160;11 // sample string12 var str = 'Sample text!';13 // output to console14 console.log('MD5: ' + MD5.hex(str));15 console.log('SHA1: ' + SHA1.hex(str));16 console.log('SHA256: ' + SHA256.hex(str));17 console.log('SHA512: ' + SHA512.hex(str));18 console.log('RIPEMD-160: ' + RMD160.hex(str));
网上也有很多关于数据加密的,大部分思路是使用非对称加密方法,如果你有好的方法可以多多交流。
另外附上两个在node.js环境上加密解密的插件:
https://github.com/gwjjeff/cryptojs
https://github.com/cyphrd/crypto
新闻热点
疑难解答