在C#中
class AES256CBC
{
public static string HexToString(string hexString)
{
// Remove any spaces or other non-hex characters from the input string
hexString = Regex.Replace(hexString, "[^0-9a-fA-F]", "");
// Convert each pair of hex digits into a byte and add it to a byte array
byte[] byteArray = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length; i += 2)
{
byteArray[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
}
// Convert the byte array to a string using the default encoding
string result = Encoding.Default.GetString(byteArray);
return result;
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public static byte[] GetByteArrayFromStr(string str, int count)
{
byte[] byteArray = new byte[count];
// Copy the first 32 characters of the string into the byte array
for (int i = 0; i < count && i < str.Length; i++)
{
byteArray[i] = (byte)str[i];
}
return byteArray;
}
static string ByteArrayToHexViaByteManipulation2(byte[] bytes)
{
char[] c = new char[bytes.Length * 2];
int b;
for (int i = 0; i < bytes.Length; i++)
{
b = bytes[i] >> 4;
c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7));
b = bytes[i] & 0xF;
c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7));
}
return new string(c);
}
public string Decrypt(string Key, string EncryptedStr)
{
var aes = CreateAES(Key);
string hexiv = EncryptedStr.Substring(0, 32);
string hexencstr = EncryptedStr.Substring(32);
aes.IV = StringToByteArray(hexiv);
ICryptoTransform ct = aes.CreateDecryptor();
byte[] clearBytes = StringToByteArray(hexencstr);
byte[] output = ct.TransformFinalBlock(clearBytes, 0, clearBytes.Length);
ct.Dispose();
string unencr = Encoding.UTF8.GetString(output);
return unencr;
}
public string Encrypt(string Key, string StringToEncrypt)
{
var aes = CreateAES(Key);
aes.GenerateIV();
var ct = aes.CreateEncryptor();
byte[] clearBytes = Encoding.UTF8.GetBytes(StringToEncrypt);
var output = ct.TransformFinalBlock(clearBytes, 0, clearBytes.Length);
string unencr = ByteArrayToHexViaByteManipulation2(aes.IV) + ByteArrayToHexViaByteManipulation2(output);
return unencr;
}
private static Aes CreateAES(string key)
{
Aes algorithm = new AesCryptoServiceProvider();
//set the mode, padding and block size
algorithm.Padding = PaddingMode.Zeros;
algorithm.Mode = CipherMode.CBC;
algorithm.KeySize = 256;
algorithm.BlockSize = 128;
algorithm.GenerateIV();
algorithm.Key = GetByteArrayFromStr(key, 32);
return algorithm;
}
}
我想要 Apex 中的类似代码。我尝试使用下面的顶点代码解密从 C# 接收到的字符串,但它不起作用。它抱怨有一个错误的密钥。有人可以帮忙吗?
Blob secretKey = Blob.valueOf('78A52519043F9C61CEDDECAFC0FFEE39');
Blob encryptedData = Blob.valueOf('896F4DE1B142A76E846855A74475939E56697327FC41CB8FAA328766EF7A8B897AEE86070462B621792B8E6B569952C5715BD9484601C397554E25BCE52BF2468364E927168609785FB09469F5C06C9C');
Blob vector = Blob.valueOf('011C7B86231CE243');
Blob decryptedData = System.Crypto.decryptWithManagedIV('AES256', secretKey, encryptedData);