CryptoJS與C#AES加解密互轉
轉載連線:https://www.cnblogs.com/wz122889488/p/6899615.html
頁面js引用:
<script type="text/javascript" src="/content/plugin/CryptoJSv3.1.2/components/core-min.js"></script>
<script type="text/javascript" src="/content/plugin/CryptoJSv3.1.2/rollups/aes.js"></script>
JS端AES加密解密:
/**************************************************************
*字串加密
* str:需要加密的字串
****************************************************************/
function Encrypt(str) {
var KEY = "12345678900000001234567890000000";//32位
var IV = "1234567890000000";//16位
var key = CryptoJS.enc.Utf8.parse(KEY);
var iv = CryptoJS.enc.Utf8.parse(IV);
var encrypted = '';
var srcs = CryptoJS.enc.Utf8.parse(str);
encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.ciphertext.toString();
}
/**************************************************************
*字串解密
* str:需要解密的字串
****************************************************************/
function Decrypt(str) {
var KEY = "12345678900000001234567890000000";//32位
var IV = "1234567890000000";//16位
var key = CryptoJS.enc.Utf8.parse(KEY);
var iv = CryptoJS.enc.Utf8.parse(IV);
var encryptedHexStr = CryptoJS.enc.Hex.parse(str);
var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
var decrypt = CryptoJS.AES.decrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
C# AES加密解密:
const string AES_IV = "1234567890000000";//16位
/// <summary>
/// AES加密演算法
/// </summary>
/// <param name="input">明文字串</param>
/// <param name="key">金鑰(32位)</param>
/// <returns>字串</returns>
public static string EncryptByAES(string input, string key)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = keyBytes;
aesAlg.IV = Encoding.UTF8.GetBytes(AES_IV.Substring(0, 16));
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(input);
}
byte[] bytes = msEncrypt.ToArray();
return ByteArrayToHexString(bytes);
}
}
}
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="input">密文位元組陣列</param>
/// <param name="key">金鑰(32位)</param>
/// <returns>返回解密後的字串</returns>
public static string DecryptByAES(string input, string key)
{
byte[] inputBytes = HexStringToByteArray(input);
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = keyBytes;
aesAlg.IV = Encoding.UTF8.GetBytes(AES_IV.Substring(0, 16));
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream(inputBytes))
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srEncrypt = new StreamReader(csEncrypt))
{
return srEncrypt.ReadToEnd();
}
}
}
}
}
/// <summary>
/// 將指定的16進位制字串轉換為byte陣列
/// </summary>
/// <param name="s">16進位制字串(如:“7F 2C 4A”或“7F2C4A”都可以)</param>
/// <returns>16進位制字串對應的byte陣列</returns>
public static byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
/// <summary>
/// 將一個byte陣列轉換成一個格式化的16進位制字串
/// </summary>
/// <param name="data">byte陣列</param>
/// <returns>格式化的16進位制字串</returns>
public static string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
{
//16進位制數字
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
//16進位制數字之間以空格隔開
//sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
}
return sb.ToString().ToUpper();
}
用法:
Encrypt("2017-05")//結果:68f4a7903a9fe6085d2301ac68cc039c Decrypt("68f4a7903a9fe6085d2301ac68cc039c")//結果:2017-05
//加密 string str1 = Encrypt.EncryptByAES("2017-05", "12345678900000001234567890000000"); //解密 string str2 = Encrypt.DecryptByAES("68f4a7903a9fe6085d2301ac68cc039c", "12345678900000001234567890000000");
相關文章
- CryptoJs 使用 AES CBC 加解密資料JS解密
- CryptoJSJS
- cryptojs的使用JS
- Canvas與Image互轉Canvas
- Buffer 與 Mat 互轉
- QImage 與 Mat 互轉
- Java中List與陣列互轉Java陣列
- Json物件與Json字串互轉JSON物件字串
- List ,Set,Map集合與陣列互轉陣列
- linux與unix時間戳互轉Linux時間戳
- JN專案-String與Timestamp互轉
- Java中集合與陣列互轉總結Java陣列
- 阿拉伯與羅馬數字互轉程式
- iOS 音訊壓縮(caf與amr互轉)iOS音訊
- Python/Java AES 加解密方法互相轉換PythonJava解密
- Linux軟體包轉換-deb與rpm互轉Linux
- 記一次前端CryptoJS AES解密前端JS解密
- 【Java加解密系列】- SM2加解密Java解密
- 凱撒密碼加解密過程與破解原理密碼解密
- ios/oc中的結構體 與字串互轉iOS結構體字串
- C++中UNIX時間戳與日期互轉C++時間戳
- Egg.js+MongoDB開發小程式介面 (三) 與微信伺服器互動及使用者資訊加解密JSMongoDB伺服器解密
- 前端加解密前端解密
- C++ string互轉wstring/Unicode互轉ANSI/Unicode互轉UTF8C++Unicode
- JavaScript 字串與json物件互轉的幾種方法JavaScript字串JSON物件
- java 圖片與base64相互轉化Java
- json字串與Object、List、Map的互轉工具類JSON字串Object
- 使用xstream實現對JavaBean與xml字串的互轉JavaBeanXML字串
- 談asp 與SQL server互操作的時間處理 (轉)SQLServer
- 用vb實現DES加解密演算法(三)--解密 (轉)解密演算法
- 用vb實現DES加解密演算法(二)--加密 (轉)解密演算法加密
- HarmonyOS Next 金鑰轉換技巧:提升加解密靈活性解密
- Java Unicode互轉JavaUnicode
- js 與WKWebView 互動JSWebView
- 互動與關係
- MySQL 與OS互動MySql
- WKWebView與JS互動WebViewJS
- JAVA中 XML與資料庫互轉 學習筆記三JavaXML資料庫筆記