C# text encoder / decoder

Please note that I'm not a professional developer and I might make simple mistakes because of that, I'm also dutch, and 14 years old, so please keep that in mind before you reply. thank you C:

My brother came up with an idea to encode and decode text with a username and a password...
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.
We're going to use this for our website, and I thought it might be nice to share this with others!

Here's the DLL file: http://data.bemacizedgaming.com/CSharp_Pauls_CodeSystem.dll


Encode:

public static UInt64[] Encode(string text, string username, string password)
        {
            // set values for use in method
            UInt64 usrnpas = 0;
            // convert the values to bytes
            Byte[] t = Encoding.ASCII.GetBytes(text);
            Byte[] u = Encoding.ASCII.GetBytes(username);
            Byte[] p = Encoding.ASCII.GetBytes(password);
            // create list for numbers to be added later
            List<UInt64> tl = new List<UInt64>();
            // convert username and password to UInt64 and * them into usrnpas
            UInt64 ui = 0; 
            UInt64 pi = 0;
            foreach (Byte bu in u)
            {
                ui += Convert.ToUInt64(bu);
            }
            foreach (Byte bp in p)
            {
                pi += Convert.ToUInt64(bp);
            }
            usrnpas = ui * pi;
            // multiply all text data by that number and return them all
            foreach (Byte tb in t)
            {
                tl.Add((Convert.ToUInt64(tb) * usrnpas));
            }
            return tl.ToArray();
        }

Decoding:

public static string Decode(string[] textarray, string username, string password)
        {
            List<ulong> ul = new List<ulong>();
            foreach (string s in textarray)
            {
                ul.Add(Convert.ToUInt64(s));
            }
            return Decode(ul.ToArray(), username, password);
        }
        public static string Decode(UInt64[] textarray, string username, string password)
        {
            // set values for use in method
            UInt64 usrnpas = 0;
            string endstring = "";
            // convert the values to bytes
            Byte[] u = Encoding.ASCII.GetBytes(username);
            Byte[] p = Encoding.ASCII.GetBytes(password);
            // convert username and password to UInt64 and * them into usrnpas
            UInt64 ui = 0;
            UInt64 pi = 0;
            foreach (Byte bu in u)
            {
                ui += Convert.ToUInt64(bu);
            }
            foreach (Byte bp in p)
            {
                pi += Convert.ToUInt64(bp);
            }
            usrnpas = ui * pi;
            // devide everything by the usrnpass and get the string with ASCII
            List<byte> bytes = new List<byte>();
            foreach (UInt64 i in textarray)
            {
                try
                {
                    byte s = (byte)(i / usrnpas);
                    bytes.Add(s);
                }
                catch
                {
                    return "ERROR";
                }
            }
            endstring = Encoding.ASCII.GetString(bytes.ToArray());
            return endstring;
        }

If you feel like improving this please reply with your suggestion (:

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's VERY 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 is much 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());
                }
            }
        }
    }
}
commented: Consice answer with great coding example +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.