Home > Web Front-end > JS Tutorial > JavaScript implements base64 md5 sha1 password encryption_javascript skills

JavaScript implements base64 md5 sha1 password encryption_javascript skills

WBOY
Release: 2016-05-16 15:39:53
Original
1617 people have browsed it

1. base64 encryption

Introduce the base64.js file into the page, and the calling method is:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">

<title>base64加密</title>

<script type="text/javascript" src="base64.js"></script>

<script type="text/javascript">

    var b = new Base64();

    var str = b.encode("admin:admin");

    alert("base64 encode:" + str);

     //解密

    str = b.decode(str);

    alert("base64 decode:" + str);

</script>

</head>

<body>

</body>

</html>

Copy after login

2. md5 encryption

To reference the md5.js file in the page, the calling method is

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">

<title>md5加密</title>

<script type="text/ecmascript" src="md5.js"></script>

<script type="text/javascript">

 var hash = hex_md5("123dafd");

  alert(hash)

</script>

</head>

<body>

</body>

</html>

Copy after login

3. sha1 encryption

It is said that this is the most secure encryption

Introducing sha1.js into the page, the calling method is

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">

<title>sha1加密</title>

<script type="text/ecmascript" src="sha1.js"></script>

<script type="text/javascript">

 var sha = hex_sha1('mima123465')

  alert(sha) 

</script>

</head>

<body>

</body>

</html>

Copy after login

Very popular javascript md5 encryption

The MD5 encryption of javascript I saw on the Internet is relatively good, so I excerpted it for reference

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

<HTML>

 <HEAD>

 <META http-equiv='Content-Type' content='text/html; charset=gb'>

 <TITLE>非常流行的JS的md加密办法</TITLE>

 </HEAD>

 <BODY >

 <input id=test value=webasp>

 <input type=button value=md onclick="alert(hex_md(test.value))">

 <script>

 var hexcase = ; /* hex output format. - lowercase; - uppercase    */

 var bpad = ""; /* base- pad character. "=" for strict RFC compliance  */

 var chrsz  = ; /* bits per input character. - ASCII; - Unicode   */

 /*

 * These are the functions you'll usually want to call

 * They take string arguments and return either hex or base- encoded strings

 */

 function hex_md(s){ return binlhex(core_md(strbinl(s), s.length * chrsz));}

 function b_md(s){ return binlb(core_md(strbinl(s), s.length * chrsz));}

 function hex_hmac_md(key, data) { return binlhex(core_hmac_md(key, data)); }

 function b_hmac_md(key, data) { return binlb(core_hmac_md(key, data)); }

 /* Backwards compatibility - same as hex_md() */

 function calcMD(s){ return binlhex(core_md(strbinl(s), s.length * chrsz));}

 /*

 * Perform a simple self-test to see if the VM is working

 */

 function md_vm_test()

 {

  return hex_md("abc") == "cdfbdfdef";

 }

 /*

 * Calculate the MD of an array of little-endian words, and a bit length

 */

 function core_md(x, len)

 {

  /* append padding */

  x[len >> ] |= x << ((len) % );

  x[(((len + ) >>> ) << ) + ] = len;

  var a = ;

  var b = -;

  var c = -;

  var d = ;

  for(var i = ; i < x.length; i += )

 {

  var olda = a;

  var oldb = b;

  var oldc = c;

  var oldd = d;

  a = md_ff(a, b, c, d, x[i+ ], , -);

  d = md_ff(d, a, b, c, x[i+ ], , -);

  c = md_ff(c, d, a, b, x[i+ ], , );

  b = md_ff(b, c, d, a, x[i+ ], , -);

  a = md_ff(a, b, c, d, x[i+ ], , -);

  d = md_ff(d, a, b, c, x[i+ ], , );

  c = md_ff(c, d, a, b, x[i+ ], , -);

  b = md_ff(b, c, d, a, x[i+ ], , -);

  a = md_ff(a, b, c, d, x[i+ ], , );

  d = md_ff(d, a, b, c, x[i+ ], , -);

  c = md_ff(c, d, a, b, x[i+], , -);

  b = md_ff(b, c, d, a, x[i+], , -);

  a = md_ff(a, b, c, d, x[i+], , );

  d = md_ff(d, a, b, c, x[i+], , -);

  c = md_ff(c, d, a, b, x[i+], , -);

  b = md_ff(b, c, d, a, x[i+], , );

  a = md_gg(a, b, c, d, x[i+ ], , -);

  d = md_gg(d, a, b, c, x[i+ ], , -);

  c = md_gg(c, d, a, b, x[i+], , );

  b = md_gg(b, c, d, a, x[i+ ], , -);

  a = md_gg(a, b, c, d, x[i+ ], , -);

  d = md_gg(d, a, b, c, x[i+], , );

  c = md_gg(c, d, a, b, x[i+], , -);

  b = md_gg(b, c, d, a, x[i+ ], , -);

  a = md_gg(a, b, c, d, x[i+ ], , );

  d = md_gg(d, a, b, c, x[i+], , -);

  c = md_gg(c, d, a, b, x[i+ ], , -);

  b = md_gg(b, c, d, a, x[i+ ], , );

  a = md_gg(a, b, c, d, x[i+], , -);

  d = md_gg(d, a, b, c, x[i+ ], , -);

  c = md_gg(c, d, a, b, x[i+ ], , );

  b = md_gg(b, c, d, a, x[i+], , -);

  a = md_hh(a, b, c, d, x[i+ ], , -);

  d = md_hh(d, a, b, c, x[i+ ], , -);

  c = md_hh(c, d, a, b, x[i+], , );

  b = md_hh(b, c, d, a, x[i+], , -);

  a = md_hh(a, b, c, d, x[i+ ], , -);

  d = md_hh(d, a, b, c, x[i+ ], , );

  c = md_hh(c, d, a, b, x[i+ ], , -);

  b = md_hh(b, c, d, a, x[i+], , -);

  a = md_hh(a, b, c, d, x[i+], , );

  d = md_hh(d, a, b, c, x[i+ ], , -);

  c = md_hh(c, d, a, b, x[i+ ], , -);

  b = md_hh(b, c, d, a, x[i+ ], , );

  a = md_hh(a, b, c, d, x[i+ ], , -);

  d = md_hh(d, a, b, c, x[i+], , -);

  c = md_hh(c, d, a, b, x[i+], , );

  b = md_hh(b, c, d, a, x[i+ ], , -);

  a = md_ii(a, b, c, d, x[i+ ], , -);

  d = md_ii(d, a, b, c, x[i+ ], , );

  c = md_ii(c, d, a, b, x[i+], , -);

  b = md_ii(b, c, d, a, x[i+ ], , -);

  a = md_ii(a, b, c, d, x[i+], , );

  d = md_ii(d, a, b, c, x[i+ ], , -);

  c = md_ii(c, d, a, b, x[i+], , -);

  b = md_ii(b, c, d, a, x[i+ ], , -);

  a = md_ii(a, b, c, d, x[i+ ], , );

  d = md_ii(d, a, b, c, x[i+], , -);

  c = md_ii(c, d, a, b, x[i+ ], , -);

  b = md_ii(b, c, d, a, x[i+], , );

  a = md_ii(a, b, c, d, x[i+ ], , -);

  d = md_ii(d, a, b, c, x[i+], , -);

  c = md_ii(c, d, a, b, x[i+ ], , );

  b = md_ii(b, c, d, a, x[i+ ], , -);

  a = safe_add(a, olda);

  b = safe_add(b, oldb);

  c = safe_add(c, oldc);

  d = safe_add(d, oldd);

 }

 return Array(a, b, c, d);

}

/*

 * These functions implement the four basic operations the algorithm uses.

 */

function md_cmn(q, a, b, x, s, t)

{

 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);

}

function md_ff(a, b, c, d, x, s, t)

{

 return md_cmn((b & c) | ((~b) & d), a, b, x, s, t);

}

function md_gg(a, b, c, d, x, s, t)

{

 return md_cmn((b & d) | (c & (~d)), a, b, x, s, t);

}

function md_hh(a, b, c, d, x, s, t)

{

 return md_cmn(b ^ c ^ d, a, b, x, s, t);

}

function md_ii(a, b, c, d, x, s, t)

{

 return md_cmn(c ^ (b | (~d)), a, b, x, s, t);

}

/*

 * Calculate the HMAC-MD, of a key and some data

 */

function core_hmac_md(key, data)

{

 var bkey = strbinl(key);

 if(bkey.length > ) bkey = core_md(bkey, key.length * chrsz);

 var ipad = Array(), opad = Array();

 for(var i = ; i < ; i++)

 {

  ipad[i] = bkey[i] ^ x;

  opad[i] = bkey[i] ^ xCCCC;

 }

 var hash = core_md(ipad.concat(strbinl(data)), + data.length * chrsz);

 return core_md(opad.concat(hash), + );

}

/*

 * Add integers, wrapping at ^. This uses -bit operations internally

 * to work around bugs in some JS interpreters.

 */

function safe_add(x, y)

{

 var lsw = (x & xFFFF) + (y & xFFFF);

 var msw = (x >> ) + (y >> ) + (lsw >> );

 return (msw << ) | (lsw & xFFFF);

}

/*

 * Bitwise rotate a -bit number to the left.

 */

function bit_rol(num, cnt)

{

 return (num << cnt) | (num >>> ( - cnt));

}

/*

 * Convert a string to an array of little-endian words

 * If chrsz is ASCII, characters > have their hi-byte silently ignored.

 */

function strbinl(str)

{

 var bin = Array();

 var mask = ( << chrsz) - ;

 for(var i = ; i < str.length * chrsz; i += chrsz)

  bin[i>>] |= (str.charCodeAt(i / chrsz) & mask) << (i%);

 return bin;

}

/*

 * Convert an array of little-endian words to a hex string.

 */

function binlhex(binarray)

{

 var hex_tab = hexcase &#63; "ABCDEF" : "abcdef";

 var str = "";

 for(var i = ; i < binarray.length * ; i++)

 {

  str += hex_tab.charAt((binarray[i>>] >> ((i%)*+)) & xF) +

      hex_tab.charAt((binarray[i>>] >> ((i%)* )) & xF);

 }

 return str;

}

/*

 * Convert an array of little-endian words to a base- string

 */

function binlb(binarray)

{

 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";

 var str = "";

 for(var i = ; i < binarray.length * ; i += )

 {

  var triplet = (((binarray[i  >> ] >> * ( i  %)) & xFF) << )

        | (((binarray[i+ >> ] >> * ((i+)%)) & xFF) << )

        | ((binarray[i+ >> ] >> * ((i+)%)) & xFF);

  for(var j = ; j < ; j++)

  {

   if(i * + j * > binarray.length * ) str += bpad;

   else str += tab.charAt((triplet >> *(-j)) & xF);

  }

 }

 return str;

}

</script>

</BODY></HTML>

Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template