javascript和c#aes加密方法互解

Posted 游游淡淡.net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript和c#aes加密方法互解相关的知识,希望对你有一定的参考价值。

关键信息如下。

javascript

function Encrypt() {
var key = CryptoJS.enc.Utf8.parse(‘8080808080808080‘);
var iv = CryptoJS.enc.Utf8.parse(‘8080808080808080‘);
var varIn = document.getElementById("txtIn").value;
var varOut = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(varIn), key,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
document.getElementById("txtOut").value = varOut;

}

function decrypt() {
var key = CryptoJS.enc.Utf8.parse(‘8080808080808080‘);
var iv = CryptoJS.enc.Utf8.parse(‘8080808080808080‘);
var varIn = document.getElementById("txtOut").value;
var varOut = CryptoJS.AES.decrypt(varIn, key,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
document.getElementById("txtIn").value = varOut;

}

c#(AES.cs)

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace AESTest
{
public class AES
{
public static string DecryptStringAES(string cipherText)
{
var keybytes = Encoding.UTF8.GetBytes("8080808080808080");
var iv = Encoding.UTF8.GetBytes("8080808080808080");

var encrypted = Convert.FromBase64String(cipherText);
var decriptedFromJavascript = DecryptStringFromBytes(encrypted, keybytes, iv);
return string.Format(decriptedFromJavascript);
}

private static string DecryptStringFromBytes(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("key");
}

// Declare the string used to hold
// the decrypted text.
string plaintext = null;

// Create an RijndaelManaged object
// with the specified key and IV.
using (var rijAlg = new RijndaelManaged())
{
//Settings
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.FeedbackSize = 128;

rijAlg.Key = key;
rijAlg.IV = iv;

// Create a decrytor to perform the stream transform.
var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

try
{
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{

using (var srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();

}

}
}
}
catch
{
plaintext = "keyError";
}
}

return plaintext;
}


public static string EncryptStringAES(string cipherText)
{
var keybytes = Encoding.UTF8.GetBytes("8080808080808080"); //自行設定
var iv = Encoding.UTF8.GetBytes("8080808080808080"); //自行設定
var EncryptString = EncryptStringToBytes(cipherText, keybytes, iv);
return Convert.ToBase64String(EncryptString);
}

private static byte[] EncryptStringToBytes(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("key");
}
byte[] encrypted;
// Create a RijndaelManaged object
// with the specified key and IV.
using (var rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.FeedbackSize = 128;

rijAlg.Key = key;
rijAlg.IV = iv;

// Create a decrytor to perform the stream transform.
var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var 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;
}
}
}

 

以上是关于javascript和c#aes加密方法互解的主要内容,如果未能解决你的问题,请参考以下文章

针对C#和javascript的兼容AES加密和解密

AES加密算法

javascript 与 PHP 通信加密,使用AES 128 CBC no padding,以及ios,java,c#文章例子

php中的AES加密,然后用Javascript(cryptojs)解密

使用 PHP mcrypt 加密后使用 Javascript CryptoJS 解密 AES

JS实现AES加密并与PHP互通的方法分析