Hi, all
I have a C# code to decrypt data as following
public static string test(string input, string key)
{
if (((input == null) || string.IsNullOrEmpty(input.Trim())) || ((input == "false") || (input == "null")))
{
return string.Empty;
}
RijndaelManaged managed = new RijndaelManaged {
KeySize = 0x100,
BlockSize = 0x100,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
Key = Encoding.ASCII.GetBytes(key),
IV = Encoding.ASCII.GetBytes(key)
};
try
{
byte[] buffer = Convert.FromBase64String(input);
ICryptoTransform transform = managed.CreateDecryptor();
byte[] bytes = null;
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
{
stream2.Write(buffer, 0, buffer.Length);
}
bytes = stream.ToArray();
}
return Encoding.ASCII.GetString(bytes);
}
catch (Exception exception)
{
Console.Write (exception.ToString ());
return string.Empty;
}
}
And I have write a golang code as following:
func test1(data, key string) {
raw, err := base64.StdEncoding.DecodeString(data)
if err != nil {
println(err.Error())
return
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
println(err.Error())
return
}
mode := cipher.NewCBCDecrypter(block, []byte(key)[:aes.BlockSize])
mode.CryptBlocks(raw, raw)
println(string(raw))
}
Besides the PKCS7 padding problem, I can’t get the right answer. Can someone help me with this? Where do I get this IV parameter?
I Can’t set IV as C# does, because golang just complains about block size problem.