In few post in stackoverflow I found a basic Encryption and Decryption with C#. I build one of the post as win form and it worked correctly.
I use a special key as: ZWT451+Route!Ydgp$8524*Aksdsvgh+
I use a string as: My name is overflow.
And this is what I get in return: MEPtmZFj3YaToiTT5V3xO/LwYo1U9YAt
I use this code and buil my Web API 2. Than I after run my web api 2 I use below link to send Encrypt data to web api 2 as shown below.
http://localhost:49407/api/MyCntRDS/MyCntRoute?fromacreli= MEPtmZFj3YaToiTT5V3xO/LwYo1U9YAt
Here is the error I get:
{"Message":"An error has occurred.","ExceptionMessage":"Invalid length for a Base-64 char array or string.","ExceptionType":"System.FormatException","StackTrace":" at System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)\r\n at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)\r\n at System.Convert.FromBase64String(String s)\r\n at ELITACR890LTKB.Controllers.Acr89Ltkb1939Controller.DecryptionACR890String
Later I remove my Encryption and Decryption in web api and send basic data as:
My#name#is#overflow
and worked perfectly.
Here is the code that it worked in C# WinForm and not in Web API 2.
Any help please.
public Form1()
{
InitializeComponent();
}
// BTN ENCRYPT +++++++++++++++++++++++++++++++++++++++++++++++
private void Encrypt_Click(object sender, EventArgs e)
{
// Text String to be Encrypt
byte[] buffer = Encryption(textBox1.Text, txtKey.Text);
string b = Convert.ToBase64String(buffer);
// Password
textBox2.Text = b;
}
// BTN DECRYPT +++++++++++++++++++++++++++++++++++++++++++++++
private void Decrypt_Click(object sender, EventArgs e)
{
// Decrypt Text String
textBox3.Text = Decryption(textBox2.Text, txtKey.Text);
}
// ENCRYPTION
public static byte[] Encryption(string PlainText, string key)
{
TripleDES des = CreateDES(key);
ICryptoTransform ct = des.CreateEncryptor();
byte[] input = Encoding.UTF8.GetBytes(PlainText);
return ct.TransformFinalBlock(input, 0, input.Length);
}
// DECRYPTION
public static string Decryption(string CypherText, string key)
{
byte[] b = Convert.FromBase64String(CypherText);
TripleDES des = CreateDES(key);
ICryptoTransform ct = des.CreateDecryptor();
byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
return Encoding.UTF8.GetString(output);
}
// TRIPLEDES / 3DES
static TripleDES CreateDES(string key)
{
MD5 md5 = new MD5CryptoServiceProvider();
TripleDES des = new TripleDESCryptoServiceProvider();
des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
des.IV = new byte[des.BlockSize / 8];
return des;
}
}
And this is the web api code that I get an error.
[RoutePrefix("api/MyCntRoute")]
public class MyCntRDS: ApiController
{
string _SpecialKey = "ZWT451+Route!Ydgp$8524*Aksdsvgh+// 32 Character
[Route("MyCntRoute")]
[HttpGet]
public List<ACR1939ELITE> Get(string fromterminal)
{
getDataFromACR890 = DecryptionACR890String(fromterminal, _SpecialKey);
..............
..............
..............
}
}
// DECRYPTION
private static string DecryptionACR890String (string fromterminal, string _SpecialKey)
{
byte[] b = Convert.FromBase64String(fromterminal);
TripleDES des = CreateDes(_SpecialKey);
ICryptoTransform ct = des.CreateDecryptor();
byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
return Encoding.UTF8.GetString(output);
}