Simple String Encryption and Decryption in .net(C# and VB.NET)

Hi All,


Some times we need to encrypt a string or password for some reason.For that we can use the below encryption and decryption technique. it can be used for simple and less secured strings.


C#
private string EnryptString(string strEncrypted)
{
try
{
byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted);
string encryptedConnectionString = Convert.ToBase64String(b);
return encryptedConnectionString;
}
catch
{
throw;
}
}

private string DecryptString(string encrString)
{
try
{
byte[] b = Convert.FromBase64String(encrString);
string decryptedConnectionString = System.Text.ASCIIEncoding.ASCII.GetString(b);
return decryptedConnectionString;
}
catch
{
throw;
}
}

VB.NET



Private Function EnryptString(ByVal strEncrypted As String) As String
Try
Dim b As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted)
Dim encryptedConnectionString As String = Convert.ToBase64String(b)
Return encryptedConnectionString
Catch
Throw
End Try
End Function

Private Function DecryptString(ByVal encrString As String) As String
Try
Dim b As Byte() = Convert.FromBase64String(encrString)
Dim decryptedConnectionString As String = System.Text.ASCIIEncoding.ASCII.GetString(b)
Return decryptedConnectionString
Catch
Throw
End Try
End Function




Regards
JK


Comments

Author: Arivazhagan Sekar05 Oct 2010 Member Level: Gold   Points : 0

Hi friend, i have error while using your Decryption function...

Author: Mrinmay Paul12 Oct 2010 Member Level: Gold   Points : 1

Hi D.Jeya kumar,

I don't know what idea you have about encryption and decryption. But the codes that you have posted are not at all encryption/decryption leave alone simple.
What is posted here is simply text conversions like what you have done here is converted to Base64 format, similarly there are UTF32,UTF7,UTF8, and many more.

Encryption/decryption always comes with the terms "passwords"/"keys". That means you have a data(it can be file, simple text,etc ) and you encrypt/decrypt that data with a password or key.

Paul

Author: D.Jeya kumar(JK)16 Oct 2010 Member Level: Gold   Points : 1

Hi,

Encryption is a technique and we can use any technique to encrypt or decrypt the given text. It is not specific that you need to have a key to encrypt and decrypt the string. If you want to you can use your own method to encrypt or decrypt.

Read the topic again. It is a simple encryption or decryption technique.

Author: Mrinmay Paul22 Oct 2010 Member Level: Gold   Points : 1

Hi D.Jeya kumar,
I do not want to upset you.

But the definition that you have written is not complete,
If you are talking about encryption "key" has to be mentioned.

I will show you examples too.
check this link in wikipedia: http://en.wikipedia.org/wiki/Encryption

Where itself in first line its mentioned that without a special knowledge otherwise refered as key you wont be able to encrypt or decrypt a plain text.

check this link also: http://www.howstuffworks.com/encryption.htm
where the exact defination mentioned is " encryption, the process of encoding information in such a way that only the person with the key can decode it."

All the definition or references of encryption/decryption contains the term "key" which is very important part of the encryption/decryption process, if the concept of key is removed its not encryption/decryption.
It will only be "text conversion" with out key or password.

You should correct this out.

Paul

Guest Author: Thanks27 Mar 2013

string decryptedConnectionString = System.Text.UnicodeEncoding.Unicode.GetString(b);



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: