1 uni-app操作

(1) 打开HBuilderX的视图->显示终端

  cd 切换到你项目的根目录

  执行命令 npm install crypto-js

安装成功后你的项目根目录会生成node_modules文件夹,里面有crypto-js就说明成功了

uniapp 与C# 加解密_uniapp 与C#

 

import Vue from 'vue'

import CryptoJS from '@/components/crypto-js/crypto-js.js'

const keys ='12345678900000000000000987654321';

export default {

AES_encrypt(word) {

var key = CryptoJS.enc.Utf8.parse(keys);

var srcs = CryptoJS.enc.Utf8.parse(word);

var encrypted = CryptoJS.AES.encrypt(srcs, key, {


mode: CryptoJS.mode.ECB,

padding: CryptoJS.pad.Pkcs7

});

return encrypted.toString();

},

AES_decrypt(word) {

var key = CryptoJS.enc.Utf8.parse(keys);

var decrypt = CryptoJS.AES.decrypt(word, key, {

mode: CryptoJS.mode.ECB,

padding: CryptoJS.pad.Pkcs7

});

return CryptoJS.enc.Utf8.stringify(decrypt).toString();

}

}

 

 

C#

 

const string key = "12345678900000000000000987654321";

/// <summary>
/// AES 加密
/// </summary>
/// <param name="str">明文(待加密)</param>
/// <param name="key">密文</param>
/// <returns></returns>
public string AesEncrypt(string str)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);

RijndaelManaged rm = new RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};

ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// AES 解密
/// </summary>
/// <param name="str">明文(待解密)</param>
/// <param name="key">密文</param>
/// <returns></returns>
public string AesDecrypt(string str)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(str);

RijndaelManaged rm = new RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};

ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

return Encoding.UTF8.GetString(resultArray);
}