
Вот пример функций шифрования и дешифрования AES (режим ECB) с использованием .NET Core и JS с использованием CryptoJS.
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System;
public class Program
{
public static void Main(string[] args)
{
string secret = "abcdefghijklmnop";
byte[] key = Encoding.UTF8.GetBytes(secret);
string plainText = "Text to encode";
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
byte[] cipherBytes = Encrypt(plainBytes, key);
string cipherText = Convert.ToBase64String(cipherBytes);
Console.WriteLine("Cipher Text: " + cipherText);
byte[] decryptedBytes = Decrypt(cipherBytes, key);
string decryptedText = Encoding.UTF8.GetString(decryptedBytes);
Console.WriteLine("Decrypted Text: " + decryptedText);
Console.ReadLine();
}
static byte[] Encrypt(byte[] plainBytes, byte[] key)
{
byte[] encryptedBytes = null;
// Set up the encryption objects
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
// Encrypt the input plaintext using the AES algorithm
using (ICryptoTransform encryptor = aes.CreateEncryptor())
{
encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}
}
return encryptedBytes;
}
static byte[] Decrypt(byte[] cipherBytes, byte[] key)
{
byte[] decryptedBytes = null;
// Set up the encryption objects
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
// Decrypt the input ciphertext using the AES algorithm
using (ICryptoTransform decryptor = aes.CreateDecryptor())
{
decryptedBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
}
return decryptedBytes;
}
}
const crypto = require('cryptojs').Crypto;
let plainText = "Text to encrypt";
let cipherText = "<cipher text from encryption function>";
let secret = "abcdefghijklmnop";
let key = CryptoJS.enc.Utf8.parse(secret);
let encryptedBytes = CryptoJS.AES.encrypt(plainText, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
let encryptedString = encryptedBytes.toString();
console.log(encryptedString);
let decryptedBytes = CryptoJS.AES.decrypt(cipherText, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
let decryptedText = decryptedBytes.toString(CryptoJS.enc.Utf8);
console.log(decryptedText);