1.npm下载

npm install crypto-js

2.在methods里面引入以下代码,注意sKey可自定义与后端商量

//ase加密解密
		enCode:function(plaintText){
			        var that=this
					var CryptoJS = require("crypto-js");
					var sKey = "1111111111111111"
					sKey = CryptoJS.enc.Utf8.parse(sKey)
					var ciphertext = CryptoJS.AES.encrypt(plaintText, sKey,{
						iv: sKey,
						mode: CryptoJS.mode.CBC, // CBC算法
						 padding: CryptoJS.pad.ZeroPadding, //使用pkcs7 进行padding 后端需要注意
					}).toString();
					return ciphertext;
				},
				deCode:function(ciphertext){
					var sKey = "1111111111111111"
					var CryptoJS = require("crypto-js");
					sKey = CryptoJS.enc.Utf8.parse(sKey)
					return CryptoJS.AES.decrypt(ciphertext, sKey, {
						 iv: sKey,
						 mode: CryptoJS.mode.CBC,
						padding: CryptoJS.pad.Pkcs7
					}).toString(CryptoJS.enc.Utf8);
				},

3.在onLoad里面使用示例

onLoad() {
		var str = '6667';
		var enStr = this.enCode(str);
		console.log('加密数据:', enStr);		
		var deStr = this.deCode(enStr);	
		console.log('解密数据', deStr);	
	},

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐