When you encode something you have to decode it with the same username and password again, otherwise it will give you a very strange character text.
It's called symmetric key cryptography, and I don't recommend inventing your own algorithm as it's more likely to be woefully insecure than decent in any way. As one example, your key isn't unique (actually, it'sVERY not unique) due to the chance of unsigned integer wrap around with arbitrarily long text strings.
We're going to use this for our website, and I thought it might be nice to share this with others!
If you're going to show it off on your website, fine. If you're doing this for fun and education, fine. If you're planning on using this algorithm for your website's security, I strongly suggest that you reconsider and instead use .NET's built-in cryptography algorithms. The following ismuch better (not to mention more concise):
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class Program {
public static void Main() {
// Test driver for the Encryption class
//
var crypt = new Encryption("QNHMKh4HTJnTxzDsorGvL5IZxfPgvagA", "21Z8CmgIDQEB9Khm7fs8aw==");
string cipherText = crypt.Encrypt("this is a test");
string plainText = crypt.Decrypt(cipherText);
Console.WriteLine("{0}\n{1}", cipherText, plainText);
}
}
public sealed class Encryption {
private byte[] _key; // Symmetric key used for both encryption and decryption
private byte[] _iv; // Initialization vector for the encryption algorithm
/// <summary>
/// Initialize an object of the Encryption class with specified key and IV.
/// </summary>
/// <param name="key">Base 64 representation of the symmetric key.</param>
/// <param name="iv">Base 64 representation of the initialization vector.</param>
/// <remarks>
/// Only default key and IV sizes are supported by this class. 256 and 128 bits, respectively.
/// </remarks>
public Encryption(string key, string iv) {
_key = Convert.FromBase64String(key);
_iv = Convert.FromBase64String(iv);
}
/// <summary>
/// Encrypt a plain text string.
/// </summary>
/// <param name="data">The plain text.</param>
/// <returns>A base 64 string representation of the encrypted bytes.</returns>
public string Encrypt(string data) {
using (var crypt = new RijndaelManaged()) {
using (var ms = new MemoryStream()) {
using (var encryptor = crypt.CreateEncryptor(_key, _iv)) {
byte[] input = Encoding.UTF8.GetBytes(data);
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
cs.Write(input, 0, input.Length);
return Convert.ToBase64String(ms.ToArray());
}
}
}
}
/// <summary>
/// Decrypt a cipher text string.
/// </summary>
/// <param name="data">The cipher text.</param>
/// <returns>The plain text representation of the decrypted bytes.</returns>
/// <remarks>
/// The cipher text string *must* have been created by the Encrypt() method using the same key and IV.
/// </remarks>
public string Decrypt(string data) {
using (var crypt = new RijndaelManaged()) {
using (var ms = new MemoryStream()) {
using (var decryptor = crypt.CreateDecryptor(_key, _iv)) {
byte[] input = Convert.FromBase64String(data);
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
cs.Write(input, 0, input.Length);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
} Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401