Sharrok G
Sharrok G

Reputation: 457

how to decrypt a string in c#

I know how to encrypt the test but I don't know how to decrypt it.

Can any one please say me how can I do it.

The code I am using to encrypt the string is

string encoded = Convert.ToBase64String(Encoding.Unicode.GetBytes("USERNAME"));
string decript = Convert.ToString(encoded);
Decrypt(encoded);

I don't know how to decrypt.

Upvotes: 0

Views: 649

Answers (4)

CodeCaster
CodeCaster

Reputation: 151588

Have you tried Convert.FromBase64String()?

Upvotes: 2

Vitaliy
Vitaliy

Reputation: 2804

This code is used to encode/decode string to/from Base64:

string inputText = "This is some text.";
byte [] bytesToEncode = Encoding.Unicode.GetBytes (inputText);

string encodedText = Convert.ToBase64String (bytesToEncode);

byte [] decodedBytes = Convert.FromBase64String (encodedText);
string decodedText = Encoding.Unicode.GetString (decodedBytes);

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532445

First, you're not encrypting but encoding. Encryption typically uses a secret key (or public/private key pair) so that only the person holding the key can decrypt the encrypted message. Encoding is reversible if you know the algorithm that's used. Encoding should not be used as a substitute for encryption; it is not secure.

Second, you simply need to reverse the process using the twin of the Convert method you're using to do the encoding.

 string decoded = Encoding.Unicode.GetString( Convert.FromBase64String( encoded ) );

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1499760

Base64 isn't "encryption" - it's just a way of representing arbitrary binary data as an entirely ASCII string.

The reverse of Convert.ToBase64String is simply Convert.FromBase64String, but it's important that you understand that this is not an encryption technique. (It's also worth thinking about why you've got that middle line - what do you expect Convert.ToString to do when it's passed a string?)

Upvotes: 5

Related Questions