C# Facing issue to encrypt and decrypt text by Base64

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
The text ZIRT which i encrypt by base64 this way


public static string encrypt(string ToEncrypt)
{
return Convert.ToBase64String(Encoding.ASCII.GetBytes(ToEncrypt));
}


after encrypt the text becomes V2tsU1ZB and when i try to decrypt using the below function

public static string decrypt(string cypherString)
{
//return Encoding.ASCII.GetString(Convert.FromBase64String(cypherString));

byte[] data = Convert.FromBase64String(cypherString);
string decodedString = Encoding.UTF8.GetString(data);
return decodedString;
}


then i am getting this text WklSVA but it suppose to be ZIRT

Please tell me what is wrong in my code ?

I am giving more code which fail to decrypt text

private void button1_Click(object sender, EventArgs e)
{
string strTxt = "ZIRT";

string ss = EnryptString(strTxt);

string ss1 = EnryptString(ss);
}

public string DecryptString(string encrString)
{
byte[] b;
string decrypted;
try
{
b = Convert.FromBase64String(encrString);
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(b);
}
catch (FormatException fe)
{
decrypted = "";
}
return decrypted;
}

public string EnryptString(string strEncrypted)
{
byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted);
string encrypted = Convert.ToBase64String(b);
return encrypted;
}
please give me right code which can properly encrypt and decrypt text using base64 logic. thanks

Continue reading...
 
Back
Top