一、前端的base64使用方法

<html>
	<head>
		<meta charset="utf-8">
		<title>前端的base64使用方法</title>
	</head>
	<body>
	</body>
	<script>
		var str = "hello";
		var str64 = window.btoa(str);
		console.log("字符串是:" + str);
		console.log("经base64编码后:" + str64);
		console.log("base64解码后:" + window.atob(str64));
	</script>
</html>

二、base64加密

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<title>base64加密</title>
		<script type="text/javascript" src="js/base64.js"></script>
		<script type="text/javascript">
			var b = new Base64();
			var str = b.encode("hello");
			alert("base64 encode:" + str);
			//base64 encode:MTIzZGFmZA==
			//解密
			str = b.decode(str);
			alert("base64 decode:" + str);
			//base64 decode:123dafd
		</script>
	</head>
	<body>
	</body>
</html>

base64.js

/**
 *
 * Base64 encode / decode
 *
 * @author haitao.tu
 * @date 2010-04-26
 * @email tuhaitao@foxmail.com
 *
 */
function Base64() {
	// private property
	_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	// public method for encoding
	this.encode = function(input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = _utf8_encode(input);
		while(i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if(isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if(isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
				_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
				_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
		}
		return output;
	}
	// public method for decoding
	this.decode = function(input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while(i < input.length) {
			enc1 = _keyStr.indexOf(input.charAt(i++));
			enc2 = _keyStr.indexOf(input.charAt(i++));
			enc3 = _keyStr.indexOf(input.charAt(i++));
			enc4 = _keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if(enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if(enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = _utf8_decode(output);
		return output;
	}
	// private method for UTF-8 encoding
	_utf8_encode = function(string) {
		string = string.replace(/\r\n/g, "\n");
		var utftext = "";
		for(var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if(c < 128) {
				utftext += String.fromCharCode(c);
			} else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	}
	// private method for UTF-8 decoding
	_utf8_decode = function(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while(i < utftext.length) {
			c = utftext.charCodeAt(i);
			if(c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

三、JS函数的escape()加密 和 unescape()解密

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
        <!--JS函数的escape()和unescape(),分别是编码和解码字符串-->
		<script type="text/javascript">
			var escape1 = escape("我的名字是:mosquito~"); //编码
			console.log(escape1);
			var unescape1 = unescape(escape1); //解码
			console.log(unescape1);
		</script>
	</head>
	<body>
	</body>
</html>

四、sha1加密

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<title>sha1加密</title>
		<script src="https://cdn.bootcss.com/js-sha1/0.6.0/sha1.js"></script>
		<script type="text/javascript">
			var sha1_1 = sha1("mosquito~");
			console.log(sha1_1);
			var sha1_2 = sha1("admin:1001");
			console.log(sha1_2);
		</script>
	</head>
	<body>
	</body>
</html>

五、md5加密

<!DOCTYPE HTML>
<html>
	<!--关于MD5:
		MD5.js是通过前台js加密的方式对用户信息,密码等私密信息进行加密处理的工具,也可称为插件。
		
		MD5共有6种加密方法:
		1, hex_md5(value)
		2, b64_md5(value)
		3, str_md5(value)
		4, hex_hmac_md5(key, data)
		5, b64_hmac_md5(key, data)
		6, str_hmac_md5(key, data)
	-->
	<head>
		<meta charset="utf-8">
		<title>md5加密</title>
		<script type="text/ecmascript" src="js/md5.js"></script>
		
	    <script>
	        var code = "123456";
	        var username = "123456";
	        var password = "123456";
	        var str1 = hex_md5("123456");
	        var str2 = b64_md5("123456");
	        var str3 = str_md5("123456");
	        var str4 = hex_hmac_md5(code,code);
	        var str5 = b64_hmac_md5(username,username);
	        var str6 = str_hmac_md5(password,password);
	        console.log(str1);            // e10adc3949ba59abbe56e057f20f883e
	        console.log(str2);            // 4QrcOUm6Wau+VuBX8g+IPg
	        console.log(str3);            // áÜ9IºY«¾VàWò��>
	        console.log(str4);            // 30ce71a73bdd908c3955a90e8f7429ef
	        console.log(str5);            // MM5xpzvdkIw5VakOj3Qp7w
	        console.log(str6);            // 0Îq§;Ý��9U©��t)ï
		</script>	
		
	</head>

	<body>
	</body>

</html>

六、AES/DES加密解密

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>AES/DES加密解密</title>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

		<script>
			var aseKey = "12345678" //秘钥必须为:8/16/32位
			var message = "80018000142";
			//加密
			var encrypt = CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(aseKey), {
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			}).toString();
			console.log(encrypt); //VKrZlqykem73x8/T2oCfCQ==

			//解密
			var decrypt = CryptoJS.AES.decrypt(encrypt, CryptoJS.enc.Utf8.parse(aseKey), {
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			}).toString(CryptoJS.enc.Utf8);
			console.log(decrypt); //80018000142
		</script>
	</head>

	<body>
	</body>

</html>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐