Js的Aes加密如何转换为C#代码?
我这里用了下面代码,可是结果却不一样:
public static string AESEncryptedString(this string encrypteStr,
string key) {
byte[] aesBytes = Encoding.UTF8.GetBytes(encrypteStr);
byte[] aesKey = Encoding.UTF8.GetBytes(key);
using MemoryStream memoryStream = new();
Rijndael Aes = Rijndael.Create();
Aes.Mode = CipherMode.ECB;
Aes.Padding = PaddingMode.PKCS7;
Aes.KeySize = 128;
Aes.Key = aesKey;
using CryptoStream cryptoStream = new(memoryStream,
Aes.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(aesBytes, 0, aesBytes.Length);
cryptoStream.FlushFinalBlock();
Aes.Clear();
//return Convert.ToBase64String(memoryStream.ToArray());
return Convert.ToHexString(memoryStream.ToArray());
}
请高手帮帮!!!
使用.NET框架中的System.Security.Cryptography命名空间中的AesManaged类来实现,下面这个是我以前写过的可以参考:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class AesExample
{
static void Main()
{
string plainText = "Hello, world!";
byte[] key = new byte[32];
byte[] iv = new byte[16];
// Generate a random key and IV
using (AesManaged aes = new AesManaged())
{
aes.GenerateKey();
aes.GenerateIV();
key = aes.Key;
iv = aes.IV;
}
// Encrypt the string using the key and IV
byte[] encrypted = EncryptStringToBytes_Aes(plainText, key, iv);
// Decrypt the encrypted string using the key and IV
string decrypted = DecryptStringFromBytes_Aes(encrypted, key, iv);
// Display the original data and the decrypted data
Console.WriteLine("Original: {0}", plainText);
Console.WriteLine("Encrypted: {0}", Convert.ToBase64String(encrypted));
Console.WriteLine("Decrypted: {0}", decrypted);
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] key, byte[] iv)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] key, byte[] iv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
$("a").bind('click', function() {
var hh = $(this).attr("href");
if (typeof (hh) == 'undefined' || hh == '#') {
hh = $(this).attr("url");
if (typeof (hh) == 'undefined' || hh == '#') {
return
}
}
var aa = hh.split("/");
var aaa = aa.length;
var bbb = aa[aaa - 1].split('.');
var ccc = bbb[0];
var cccc = bbb[1];
var r = /^\+?[1-9][0-9]*$/;
var ee = $(this).attr('target');
if (r.test(ccc) && cccc.indexOf('jhtml') != -1) {
var srcs = CryptoJS.enc.Utf8.parse(ccc);
var k = CryptoJS.enc.Utf8.parse(s);
var en = CryptoJS.AES.encrypt(srcs, k, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
var ddd = en.toString();
ddd = ddd.replace(/\//g, "^");
ddd = ddd.substring(0, ddd.length - 2);
var bbbb = ddd + '.' + bbb[1];
aa[aaa - 1] = bbbb;
var uuu = '';
for (i = 0; i < aaa; i++) {
uuu += aa[i] + '/'
}
uuu = uuu.substring(0, uuu.length - 1);
if (typeof (ee) == 'undefined') {
window.location = uuu
} else {
window.open(uuu)
}
} else {
if (typeof (ee) == 'undefined') {
window.location = hh
} else {
window.open(hh)
}
}
return false
});
上面代码需要修改,没有CryptoJS的需要安装:npm install crypto-js
这里已知 a = i.a.enc.Hex.parse(o); 是对 Key进行 字节化处理 ,是否可以对应C# 的 byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key)?或者是对应别的C#代码?
我想接着问一下
key="1d2d3d4d5d6d7d8d9d8d7d6d5d4d3d2d" 经过 .enc.Hex.parse 后变为 下面的内容
sigBytes: 16
words: (4) [489504077, 1567456653, -1651671699, 1565343021]
这个JS的变量 , 是多大的 改用C# 的 byte[] 应该是 byte[16]?
这里有这一段代码
parse: function(e) {
for (var t = e.length, n = [], r = 0; r < t; r += 2)
n[r >>> 3] |= parseInt(e.substr(r, 2), 16) << 24 - r % 8 * 4;
return new u.init(n,t / 2)
请问如何把上面的一段代码转 成C#?