原文: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("加密之后的结果是:"+encypass);
}
else if(e == 2){
console.log("解密之前的结果是:"+encypass);
console.log("解密之后的结果是:"+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("加密之后的结果是:"+encypass);
}
else if(e == 2){
console.log("解密之前的结果是:"+encypass);
console.log("解密之后的结果是:"+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>