uniapp 与C# 加解密

五陵年少金市东,银鞍白马渡春风。这篇文章主要讲述uniapp 与C# 加解密相关的知识,希望能为你提供帮助。
【uniapp 与C# 加解密】1 uni-app操作
(1) 打开HBuilderX的视图-> 显示终端
cd 切换到你项目的根目录
执行命令 npm install crypto-js
安装成功后你的项目根目录会生成node_modules文件夹,里面有crypto-js就说明成功了

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>
publicstring 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>
publicstring 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);
}
 

    推荐阅读