经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue使用MD5对前后端进行加密的实现
来源:jb51  时间:2022/4/7 12:49:28  对本文有异议

前后端分离的项目,遇到了对密码进行加密的情况,在前端或者是在后端加密都是可以的。但是从用户数据的安全性来讲,前后端是都需要进行加密的。后端不加密的话,数据库中存储明文密码,就可以从数据库窃取用户密码。前端不加密的话,在异步传输的过程中,就可以获取传输的明文密码,就会导致密码泄露。当然,加密算法有很多,这里我就简单介绍一下使用MD5进行加密吧。

前端

1、在public下面新建一个MD5.js工具类
MD5 的内容:

  1. var KEY = "!@#QWERT";
  2. /*
  3. * Configurable variables. You may need to tweak these to be compatible with
  4. * the server-side, but the defaults work in most cases.
  5. */
  6. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  7. var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  8. var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  9.  
  10. /*
  11. * These are the functions you'll usually want to call
  12. * They take string arguments and return either hex or base-64 encoded strings
  13. */
  14. function hexMd5(s) {
  15. return hex_md5(s);
  16. }
  17. function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
  18. function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
  19. function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
  20. function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
  21. function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
  22. function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }
  23.  
  24. /*
  25. * Perform a simple self-test to see if the VM is working
  26. */
  27. function md5_vm_test()
  28. {
  29. return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
  30. }
  31.  
  32. /*
  33. * Calculate the MD5 of an array of little-endian words, and a bit length
  34. */
  35. function core_md5(x, len)
  36. {
  37. /* append padding */
  38. x[len >> 5] |= 0x80 << ((len) % 32);
  39. x[(((len + 64) >>> 9) << 4) + 14] = len;
  40.  
  41. var a = 1732584193;
  42. var b = -271733879;
  43. var c = -1732584194;
  44. var d = 271733878;
  45.  
  46. for(var i = 0; i < x.length; i += 16)
  47. {
  48. var olda = a;
  49. var oldb = b;
  50. var oldc = c;
  51. var oldd = d;
  52.  
  53. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  54. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  55. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  56. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  57. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  58. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  59. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  60. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  61. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  62. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  63. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  64. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  65. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  66. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  67. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  68. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  69.  
  70. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  71. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  72. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  73. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  74. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  75. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  76. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  77. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  78. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  79. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  80. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  81. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  82. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  83. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  84. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  85. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  86.  
  87. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  88. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  89. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  90. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  91. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  92. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  93. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  94. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  95. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  96. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  97. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  98. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  99. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  100. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  101. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  102. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  103.  
  104. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  105. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  106. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  107. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  108. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  109. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  110. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  111. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  112. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  113. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  114. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  115. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  116. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  117. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  118. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  119. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  120.  
  121. a = safe_add(a, olda);
  122. b = safe_add(b, oldb);
  123. c = safe_add(c, oldc);
  124. d = safe_add(d, oldd);
  125. }
  126. return Array(a, b, c, d);
  127.  
  128. }
  129.  
  130. /*
  131. * These functions implement the four basic operations the algorithm uses.
  132. */
  133. function md5_cmn(q, a, b, x, s, t)
  134. {
  135. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  136. }
  137. function md5_ff(a, b, c, d, x, s, t)
  138. {
  139. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  140. }
  141. function md5_gg(a, b, c, d, x, s, t)
  142. {
  143. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  144. }
  145. function md5_hh(a, b, c, d, x, s, t)
  146. {
  147. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  148. }
  149. function md5_ii(a, b, c, d, x, s, t)
  150. {
  151. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  152. }
  153.  
  154. /*
  155. * Calculate the HMAC-MD5, of a key and some data
  156. */
  157. function core_hmac_md5(key, data)
  158. {
  159. var bkey = str2binl(key);
  160. if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
  161.  
  162. var ipad = Array(16), opad = Array(16);
  163. for(var i = 0; i < 16; i++)
  164. {
  165. ipad[i] = bkey[i] ^ 0x36363636;
  166. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  167. }
  168.  
  169. var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  170. return core_md5(opad.concat(hash), 512 + 128);
  171. }
  172.  
  173. /*
  174. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  175. * to work around bugs in some JS interpreters.
  176. */
  177. function safe_add(x, y)
  178. {
  179. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  180. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  181. return (msw << 16) | (lsw & 0xFFFF);
  182. }
  183.  
  184. /*
  185. * Bitwise rotate a 32-bit number to the left.
  186. */
  187. function bit_rol(num, cnt)
  188. {
  189. return (num << cnt) | (num >>> (32 - cnt));
  190. }
  191.  
  192. /*
  193. * Convert a string to an array of little-endian words
  194. * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
  195. */
  196. function str2binl(str)
  197. {
  198. var bin = Array();
  199. var mask = (1 << chrsz) - 1;
  200. for(var i = 0; i < str.length * chrsz; i += chrsz)
  201. bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  202. return bin;
  203. }
  204.  
  205. /*
  206. * Convert an array of little-endian words to a string
  207. */
  208. function binl2str(bin)
  209. {
  210. var str = "";
  211. var mask = (1 << chrsz) - 1;
  212. for(var i = 0; i < bin.length * 32; i += chrsz)
  213. str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  214. return str;
  215. }
  216.  
  217. /*
  218. * Convert an array of little-endian words to a hex string.
  219. */
  220. function binl2hex(binarray)
  221. {
  222. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  223. var str = "";
  224. for(var i = 0; i < binarray.length * 4; i++)
  225. {
  226. str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
  227. hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
  228. }
  229. return str;
  230. }
  231.  
  232. /*
  233. * Convert an array of little-endian words to a base-64 string
  234. */
  235. function binl2b64(binarray)
  236. {
  237. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  238. var str = "";
  239. for(var i = 0; i < binarray.length * 4; i += 3)
  240. {
  241. var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)
  242. | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
  243. | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
  244. for(var j = 0; j < 4; j++)
  245. {
  246. if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
  247. else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
  248. }
  249. }
  250. return str;
  251. }
  252.  

2、在index.html中,引入MD5.js

  1. <script href="<%= BASE_URL %>js/md5.js" rel="external nofollow" ></script>

3、使用方法

  1. /**
  2. * 前端调用 MD5 加密算法
  3. * KEY 是盐值,让密码更为复杂
  4. */
  5. user.value.password = hexMd5(user.value.password + KEY);

后端

spring自带MD5加密,直接引用工具类DigestUtils就可以

在这里插入图片描述

  1. user1.setPassword(DigestUtils.md5DigestAsHex(user.getUserCode().getBytes()));

以下是工具类DigestUtils的源码,不需要的可以直接忽略。

  1. /*
  2. * Copyright 2002-2020 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package org.springframework.util;
  18.  
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.security.MessageDigest;
  22. import java.security.NoSuchAlgorithmException;
  23.  
  24. /**
  25. * Miscellaneous methods for calculating digests.
  26. *
  27. * <p>Mainly for internal use within the framework; consider
  28. * <a href="https://commons.apache.org/codec/" rel="external nofollow" >Apache Commons Codec</a>
  29. * for a more comprehensive suite of digest utilities.
  30. *
  31. * @author Arjen Poutsma
  32. * @author Juergen Hoeller
  33. * @author Craig Andrews
  34. * @since 3.0
  35. */
  36. public abstract class DigestUtils {
  37.  
  38. private static final String MD5_ALGORITHM_NAME = "MD5";
  39.  
  40. private static final char[] HEX_CHARS =
  41. {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  42.  
  43.  
  44. /**
  45. * Calculate the MD5 digest of the given bytes.
  46. * @param bytes the bytes to calculate the digest over
  47. * @return the digest
  48. */
  49. public static byte[] md5Digest(byte[] bytes) {
  50. return digest(MD5_ALGORITHM_NAME, bytes);
  51. }
  52.  
  53. /**
  54. * Calculate the MD5 digest of the given stream.
  55. * <p>This method does <strong>not</strong> close the input stream.
  56. * @param inputStream the InputStream to calculate the digest over
  57. * @return the digest
  58. * @since 4.2
  59. */
  60. public static byte[] md5Digest(InputStream inputStream) throws IOException {
  61. return digest(MD5_ALGORITHM_NAME, inputStream);
  62. }
  63.  
  64. /**
  65. * Return a hexadecimal string representation of the MD5 digest of the given bytes.
  66. * @param bytes the bytes to calculate the digest over
  67. * @return a hexadecimal digest string
  68. */
  69. public static String md5DigestAsHex(byte[] bytes) {
  70. return digestAsHexString(MD5_ALGORITHM_NAME, bytes);
  71. }
  72.  
  73. /**
  74. * Return a hexadecimal string representation of the MD5 digest of the given stream.
  75. * <p>This method does <strong>not</strong> close the input stream.
  76. * @param inputStream the InputStream to calculate the digest over
  77. * @return a hexadecimal digest string
  78. * @since 4.2
  79. */
  80. public static String md5DigestAsHex(InputStream inputStream) throws IOException {
  81. return digestAsHexString(MD5_ALGORITHM_NAME, inputStream);
  82. }
  83.  
  84. /**
  85. * Append a hexadecimal string representation of the MD5 digest of the given
  86. * bytes to the given {@link StringBuilder}.
  87. * @param bytes the bytes to calculate the digest over
  88. * @param builder the string builder to append the digest to
  89. * @return the given string builder
  90. */
  91. public static StringBuilder appendMd5DigestAsHex(byte[] bytes, StringBuilder builder) {
  92. return appendDigestAsHex(MD5_ALGORITHM_NAME, bytes, builder);
  93. }
  94.  
  95. /**
  96. * Append a hexadecimal string representation of the MD5 digest of the given
  97. * inputStream to the given {@link StringBuilder}.
  98. * <p>This method does <strong>not</strong> close the input stream.
  99. * @param inputStream the inputStream to calculate the digest over
  100. * @param builder the string builder to append the digest to
  101. * @return the given string builder
  102. * @since 4.2
  103. */
  104. public static StringBuilder appendMd5DigestAsHex(InputStream inputStream, StringBuilder builder) throws IOException {
  105. return appendDigestAsHex(MD5_ALGORITHM_NAME, inputStream, builder);
  106. }
  107.  
  108.  
  109. /**
  110. * Create a new {@link MessageDigest} with the given algorithm.
  111. * <p>Necessary because {@code MessageDigest} is not thread-safe.
  112. */
  113. private static MessageDigest getDigest(String algorithm) {
  114. try {
  115. return MessageDigest.getInstance(algorithm);
  116. }
  117. catch (NoSuchAlgorithmException ex) {
  118. throw new IllegalStateException("Could not find MessageDigest with algorithm \"" + algorithm + "\"", ex);
  119. }
  120. }
  121.  
  122. private static byte[] digest(String algorithm, byte[] bytes) {
  123. return getDigest(algorithm).digest(bytes);
  124. }
  125.  
  126. private static byte[] digest(String algorithm, InputStream inputStream) throws IOException {
  127. MessageDigest messageDigest = getDigest(algorithm);
  128. if (inputStream instanceof UpdateMessageDigestInputStream){
  129. ((UpdateMessageDigestInputStream) inputStream).updateMessageDigest(messageDigest);
  130. return messageDigest.digest();
  131. }
  132. else {
  133. final byte[] buffer = new byte[StreamUtils.BUFFER_SIZE];
  134. int bytesRead = -1;
  135. while ((bytesRead = inputStream.read(buffer)) != -1) {
  136. messageDigest.update(buffer, 0, bytesRead);
  137. }
  138. return messageDigest.digest();
  139. }
  140. }
  141.  
  142. private static String digestAsHexString(String algorithm, byte[] bytes) {
  143. char[] hexDigest = digestAsHexChars(algorithm, bytes);
  144. return new String(hexDigest);
  145. }
  146.  
  147. private static String digestAsHexString(String algorithm, InputStream inputStream) throws IOException {
  148. char[] hexDigest = digestAsHexChars(algorithm, inputStream);
  149. return new String(hexDigest);
  150. }
  151.  
  152. private static StringBuilder appendDigestAsHex(String algorithm, byte[] bytes, StringBuilder builder) {
  153. char[] hexDigest = digestAsHexChars(algorithm, bytes);
  154. return builder.append(hexDigest);
  155. }
  156.  
  157. private static StringBuilder appendDigestAsHex(String algorithm, InputStream inputStream, StringBuilder builder)
  158. throws IOException {
  159.  
  160. char[] hexDigest = digestAsHexChars(algorithm, inputStream);
  161. return builder.append(hexDigest);
  162. }
  163.  
  164. private static char[] digestAsHexChars(String algorithm, byte[] bytes) {
  165. byte[] digest = digest(algorithm, bytes);
  166. return encodeHex(digest);
  167. }
  168.  
  169. private static char[] digestAsHexChars(String algorithm, InputStream inputStream) throws IOException {
  170. byte[] digest = digest(algorithm, inputStream);
  171. return encodeHex(digest);
  172. }
  173.  
  174. private static char[] encodeHex(byte[] bytes) {
  175. char[] chars = new char[32];
  176. for (int i = 0; i < chars.length; i = i + 2) {
  177. byte b = bytes[i / 2];
  178. chars[i] = HEX_CHARS[(b >>> 0x4) & 0xf];
  179. chars[i + 1] = HEX_CHARS[b & 0xf];
  180. }
  181. return chars;
  182. }
  183.  
  184. }
  185.  

到此这篇关于Vue使用MD5对前后端进行加密的实现的文章就介绍到这了,更多相关Vue MD5前后端加密内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号