在微信小程序开发中,使用到MD5加密(比如登录加密password)。
具体实现方法:
首先在项目中找到utils文件,在该文件下新建一个js文件 md5.js
然后复制下面的代码到你的文件中

/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. * Code also contributed by Greg Holt * See http://pajhome.org.uk/site/legal.html for details. */  /* * Add integers, wrapping at 2^32. This uses 16-bit operations internally * to work around bugs in some JS interpreters. */
function safe_add(x, y)
{  var lsw = (x & 0xFFFF) + (y & 0xFFFF)  var msw = (x >> 16) + (y >> 16) + (lsw >> 16)  return (msw << 16) | (lsw & 0xFFFF)
}  /* * Bitwise rotate a 32-bit number to the left. */
function rol(num, cnt)
{  return (num << cnt) | (num >>> (32 - cnt))
}  /* * These functions implement the four basic operations the algorithm uses. */
function cmn(q, a, b, x, s, t)
{  return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
}
function ff(a, b, c, d, x, s, t)
{  return cmn((b & c) | ((~b) & d), a, b, x, s, t)
}
function gg(a, b, c, d, x, s, t)
{  return cmn((b & d) | (c & (~d)), a, b, x, s, t)
}
function hh(a, b, c, d, x, s, t)
{  return cmn(b ^ c ^ d, a, b, x, s, t)
}
function ii(a, b, c, d, x, s, t)
{  return cmn(c ^ (b | (~d)), a, b, x, s, t)
}  /* * Calculate the MD5 of an array of little-endian words, producing an array * of little-endian words. */
function coreMD5(x)
{  var a = 1732584193  var b = -271733879  var c = -1732584194  var d = 271733878  for(var i = 0; i < x.length; i += 16)  {  var olda = a  var oldb = b  var oldc = c  var oldd = d  a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)  d = ff(d, a, b, c, x[i+ 1], 12, -389564586)  c = ff(c, d, a, b, x[i+ 2], 17, 606105819)  b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)  a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)  d = ff(d, a, b, c, x[i+ 5], 12, 1200080426)  c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)  b = ff(b, c, d, a, x[i+ 7], 22, -45705983)  a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416)  d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)  c = ff(c, d, a, b, x[i+10], 17, -42063)  b = ff(b, c, d, a, x[i+11], 22, -1990404162)  a = ff(a, b, c, d, x[i+12], 7 , 1804603682)  d = ff(d, a, b, c, x[i+13], 12, -40341101)  c = ff(c, d, a, b, x[i+14], 17, -1502002290)  b = ff(b, c, d, a, x[i+15], 22, 1236535329)  a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)  d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)  c = gg(c, d, a, b, x[i+11], 14, 643717713)  b = gg(b, c, d, a, x[i+ 0], 20, -373897302)  a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)  d = gg(d, a, b, c, x[i+10], 9 , 38016083)  c = gg(c, d, a, b, x[i+15], 14, -660478335)  b = gg(b, c, d, a, x[i+ 4], 20, -405537848)  a = gg(a, b, c, d, x[i+ 9], 5 , 568446438)  d = gg(d, a, b, c, x[i+14], 9 , -1019803690)  c = gg(c, d, a, b, x[i+ 3], 14, -187363961)  b = gg(b, c, d, a, x[i+ 8], 20, 1163531501)  a = gg(a, b, c, d, x[i+13], 5 , -1444681467)  d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)  c = gg(c, d, a, b, x[i+ 7], 14, 1735328473)  b = gg(b, c, d, a, x[i+12], 20, -1926607734)  a = hh(a, b, c, d, x[i+ 5], 4 , -378558)  d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)  c = hh(c, d, a, b, x[i+11], 16, 1839030562)  b = hh(b, c, d, a, x[i+14], 23, -35309556)  a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)  d = hh(d, a, b, c, x[i+ 4], 11, 1272893353)  c = hh(c, d, a, b, x[i+ 7], 16, -155497632)  b = hh(b, c, d, a, x[i+10], 23, -1094730640)  a = hh(a, b, c, d, x[i+13], 4 , 681279174)  d = hh(d, a, b, c, x[i+ 0], 11, -358537222)  c = hh(c, d, a, b, x[i+ 3], 16, -722521979)  b = hh(b, c, d, a, x[i+ 6], 23, 76029189)  a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)  d = hh(d, a, b, c, x[i+12], 11, -421815835)  c = hh(c, d, a, b, x[i+15], 16, 530742520)  b = hh(b, c, d, a, x[i+ 2], 23, -995338651)  a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)  d = ii(d, a, b, c, x[i+ 7], 10, 1126891415)  c = ii(c, d, a, b, x[i+14], 15, -1416354905)  b = ii(b, c, d, a, x[i+ 5], 21, -57434055)  a = ii(a, b, c, d, x[i+12], 6 , 1700485571)  d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)  c = ii(c, d, a, b, x[i+10], 15, -1051523)  b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)  a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359)  d = ii(d, a, b, c, x[i+15], 10, -30611744)  c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)  b = ii(b, c, d, a, x[i+13], 21, 1309151649)  a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)  d = ii(d, a, b, c, x[i+11], 10, -1120210379)  c = ii(c, d, a, b, x[i+ 2], 15, 718787259)  b = ii(b, c, d, a, x[i+ 9], 21, -343485551)  a = safe_add(a, olda)  b = safe_add(b, oldb)  c = safe_add(c, oldc)  d = safe_add(d, oldd)  }  return [a, b, c, d]
}  /* * Convert an array of little-endian words to a hex string. */
function binl2hex(binarray)
{  var hex_tab = "0123456789abcdef"  var str = ""  for(var i = 0; i < binarray.length * 4; i++)  {  str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +  hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF)  }  return str
}  /* * Convert an array of little-endian words to a base64 encoded string. */
function binl2b64(binarray)
{  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"  var str = ""  for(var i = 0; i < binarray.length * 32; i += 6)  {  str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |  ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))  }  return str
}  /* * Convert an 8-bit character string to a sequence of 16-word blocks, stored * as an array, and append appropriate padding for MD4/5 calculation. * If any of the characters are >255, the high byte is silently ignored. */
function str2binl(str)
{  var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks  var blks = new Array(nblk * 16)  for(var i = 0; i < nblk * 16; i++) blks[i] = 0  for(var i = 0; i < str.length; i++)  blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)  blks[i>>2] |= 0x80 << ((i%4) * 8)  blks[nblk*16-2] = str.length * 8  return blks
}  /* * Convert a wide-character string to a sequence of 16-word blocks, stored as * an array, and append appropriate padding for MD4/5 calculation. */
function strw2binl(str)
{  var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks  var blks = new Array(nblk * 16)  for(var i = 0; i < nblk * 16; i++) blks[i] = 0  for(var i = 0; i < str.length; i++)  blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)  blks[i>>1] |= 0x80 << ((i%2) * 16)  blks[nblk*16-2] = str.length * 16  return blks
}  /* * External interface */
function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }
function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }
function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }
/* Backward compatibility */
function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }
module.exports = {  hexMD5: hexMD5
}  

然后在你需要使用加密的页面进行引入就可以了
例如:
我在我的登录页面引入并使用
(根据自己的项目导入,注意路径别错了)

import { hexMD5 } from "../../utils/md5.js"Page({onLoad: function (options) {console.log(hexMD5(123456))}
})

加密完成。

微信小程序使用MD5加密相关推荐

  1. 微信小程序:MD5 加密

    微信小程序是基于 js 进行封装的,所以,本质上是 js 的 MD5 加密 一个问题是,我后台服务器用的是 Python,js 和 Python 会有 MD5 密文不一致的情况 具体参考:https: ...

  2. 微信小程序接口实现加密

    微信小程序接口实现加密教程: 场景 小程序请求的所有接口参数必须加密,后台返回数据也需要加密,并且增加Token验证 一.小程序端功能编写 1.下载一份Js版的aesUtil.js源码.[注:文章末尾 ...

  3. java写微信小程序答辩问题_java微信小程序开发中加密解密算法总结

    详解java微信小程序开发中加密解密算法 一.概述 微信推出了小程序,很多公司的客户端应用不仅具有了APP.H5.还接入了小程序开发.但是,小程序中竟然没有提供Java版本的加密数据解密算法.这着实让 ...

  4. 京喜拼拼微信小程序-signStr参数加密

    调试api: https://api.m.jd.com/api?functionId=jxpp.category.catePageRpc.cateSkuFetch&appid=jxpp_min ...

  5. 微信小程序RSA非对称加密。

    因公司做的产品为金融项目,所以对数据安全性有很高要求,因为项目中的数据都会通过3DES 对称加密,和RSA非对称加密进行数据传输. 在这里先简单介绍一下什么是对称加密和非对称加密 对称加密:对称加密采 ...

  6. 微信小程序 javascript MD5 支持汉字

    今天白天搞完RSA,晚上回来没啥 事,这破疫情真的把人快木乱了,又要封三天..特奶的,这样下去 ,不是我要被封而是我要快疯~~~~!!!!! 回到家,看着我的小棉袄在那里认真的学习,又看到我置满IT书 ...

  7. 微信小程序--实现密码加密

    微信小程序开发--实现密码加密具体步骤: 在utils中的util.js 文件中增加 函数 实现 字符串转换为16进制加密后的字符串 function encodeUTF8(s) {var i, r ...

  8. 微信小程序——crypto-js参数加密、解密问题

    前言: 在很多项目中涉及到信息敏感问题,为防止http信息传输时参数被劫持进行二次传输的尴尬局面,最好使用前端加密参数请求,后端解密,返回数据时后端加密,前端解密.防止信息被盗取.目前主流的加密方式有 ...

  9. 微信小程序安卓密码加密的小黑点太大

    最近在写一个小程序,身份证和密码需要用小眼睛来控制加密与展示,但是安卓手机的加码黑点太大,输入身份证太长,输入框长度不够,也查了官方资料,没有找到,就自己简略的想了一个比较笨的办法,如果大家有更好的方 ...

最新文章

  1. monkey如何获取app包名
  2. Python使用shape计算矩阵的行和列
  3. Android视频播放之VideoView
  4. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--L-用来作弊的药水
  5. Android窗口管理服务WindowManagerService的简要介绍和学习计划
  6. Hills And Valleys CodeForces - 1467B 思维
  7. 二分查找---查找区间
  8. 实现xml和json接口(第一篇)
  9. 搭建高性能日志服务器,rsyslog日志服务器搭建
  10. PHP的implode函数运用,PHP implode()函数用法讲解
  11. C/C++ 16bit转8bit
  12. Complementary Trilateral Decoder for Fast and Accurate Salient Object Detection(速读啊)内含与u-shape的对比
  13. 端午安康,用python给你画盘粽子~啾啾
  14. 网络工程师备考6章(续3)
  15. InfluxDB入门系列教程④ InfluxDB Studio可视化数据库管理工具
  16. 国内小程序生态服务平台即速应用完成5000万元A+轮融资...
  17. NTC,PT100热电阻转4-20mA温度信号转换器
  18. 计算机内存不足 c盘快满了怎么办,电脑C盘内存快满了怎么清理垃圾
  19. 数据库是什么,它是做什么用的?
  20. 综合布线系统中直接与用户终端设备相连的子系统是什么呢?

热门文章

  1. 第四代计算机网络是高速互联的什么网,宽带接入网竞赛试题(选择题300道,判断题150道,填空题250道)...
  2. Android Studio更换眼睛保护背景色
  3. macOS - 手动修改本地app的icon
  4. 深入理解JVM运行原理(一)
  5. 计算机应用基础及ms office应用,计算机应用MS Office基础练习题
  6. 八张图表示八大排序算法
  7. 微型计算机实验报告温度控制,温度控制实验报告.doc
  8. Java编程珠玑(202205)
  9. 选择 Go 还是 Rust?CloudWeGo-Volo 基于 Rust 语言的探索实践
  10. Matlab符号计算求导与化简