原文:https://blog.csdn.net/qq_41485414/article/details/80323023
js的加密沒特別多的辦法串塑,常見的就三種, MD5加密北苟、Base64加密和shal加密桩匪,
MD5加密
H5源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>md5加密</title>
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/md5.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<input type="password" name="" id="md5" value="" />
<input type="button" name="" id="" value="MD5加密" onclick="testmd5()" />
</body>
<script type="text/javascript">
function testmd5(){
var password = $("#md5").val();
var md5password = $.md5(password);
console.log("沒有加密之前的是:"+password);
console.log("加密以后是:"+md5password);
}
</script>
</html>
Base64加密
H5源碼
ps:理論上這個不可以稱為加密,這個只是將您的文本按照一定的編碼格式重新寫一遍罷了友鼻,但是可以起到一定的加密作用傻昙。
第一種寫法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>base64加密</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<input type="password" name="" id="basepass" value="" />
<input type="button" name="" id="" value="base加密" onclick="basefunc(1)"/>
<input type="button" name="" id="" value="base解密" onclick="basefunc(2)"/>
</body>
<script src="js/funcbase64.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function basefunc(e){
var password = $("#basepass").val();
var base = new Base64();
var encypass = base.encode(password);
var decryptpass = base.decode(encypass);
if(e == 1){
console.log("加密之前的密碼是:"+password);
console.log("加密之后的結(jié)果是:"+encypass);
}
else if(e == 2){
console.log("解密之前的結(jié)果是:"+encypass);
console.log("解密之后的結(jié)果是:"+decryptpass);
}
}
</script>
</html>
第二種寫法
H5源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>base64加密</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<input type="password" name="" id="basepass" value="" />
<input type="button" name="" id="" value="base加密" onclick="basefunc(1)"/>
<input type="button" name="" id="" value="base解密" onclick="basefunc(2)"/>
</body>
<script src="js/base64.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function basefunc(e){
var password = $("#basepass").val();
var encypass = Base64.encode(password);
var decryptpass = Base64.decode(encypass);
if(e == 1){
console.log("加密之前的密碼是:"+password);
console.log("加密之后的結(jié)果是:"+encypass);
}
else if(e == 2){
console.log("解密之前的結(jié)果是:"+encypass);
console.log("解密之后的結(jié)果是:"+decryptpass);
}
}
</script>
</html>
sha1加密
H5源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>shal加密</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/shal.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<input type="password" name="" id="md5" value="" />
<input type="button" name="" id="" value="hex_sha1加密" onclick="testshal(1)" />
<input type="button" name="" id="" value="b64_sha1加密" onclick="testshal(2)" />
<input type="button" name="" id="" value="str_sha1加密" onclick="testshal(3)" />
</body>
<script type="text/javascript">
function testshal(e){
var password = $("#md5").val();
console.log("沒有加密之前的是:"+password);
if(e == 1){
var shalpassword = hex_sha1(password);
console.log("hex_sha1方式加密后是:"+shalpassword);
}
else if(e == 2){
var shalpassword = b64_sha1(password);
console.log("b64_sha1方式加密后是:"+shalpassword);
}
else if(e == 3){
var shalpassword = str_sha1(password);
console.log("str_sha1方式加密后是:"+shalpassword);
}
}
</script>
</html>