Dear All,

can some one share me a code with one simple cipher encryption, as i'm a noob i in this thing....

Thanks all.

public string Encrypt(string input, string key = "012345678901234567890123")
        {
            byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            tripleDES.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

http://stackoverflow.com/questions/5632211/3des-key-size-matter-in-c-net

There are multiple types of encryption such as AES and RSA, and depending on which you use, there have different key structures.

Quick side track lesson about encryption. Make the algorithms public, but keep the keys private (the only cipher that is the exception being Caesar Cipher, but that's back when cryptology was taking off).

Here's an AES Encryption library I wrote up for myself some time back (I haven't used it in awhile, but it should work). It's a nice wrapper, just remember, the Key and IV are how you Encrypt and Decrypt a message

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace Temp.Namespace
{
    public class AESEncryption
    {
        /// <summary>
        /// Initializes a new AESEncryption, with auto generated Key and IV
        /// </summary>
        public AESEncryption ()
        {
            this.AESProvider = new AesCryptoServiceProvider();
            this.AESProvider.GenerateKey();
            this.AESProvider.GenerateIV();
        }

        /// <summary>
        /// Initializes a new AESEncryption, using the provided Key and IV
        /// </summary>
        public AESEncryption (byte [] Key, byte [] IV)
        {
            this.AESProvider = new AesCryptoServiceProvider();
            this.AESProvider.Key = Key;
            this.AESProvider.IV = IV;
        }

        public SymmetricAlgorithm AESProvider
        {
            get;
            private set;
        }

        public byte [] Key //16-32 bytes
        {
            get
            {
                return this.AESProvider.Key;
            }
            set
            {
                this.AESProvider.Key = value;
            }
        }

        public byte [] IV //16 bytes
        {
            get
            {
                return this.AESProvider.IV;
            }
            set
            {
                this.AESProvider.IV = value;
            }
        }

        public string Encrypt (string PlainText) //used to encrypt a message
        {
            ICryptoTransform Encryption = AESProvider.CreateEncryptor(AESProvider.Key, AESProvider.IV);
            byte [] EncodedMessage = null;

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, Encryption, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(PlainText); //write all the data to the stream
                    }

                    EncodedMessage = msEncrypt.ToArray();
                }
            }

            return Encoding.Unicode.GetString(EncodedMessage); //returns the encrypted text as a string
        }

        public string Decrypt (string EncryptedText) //used to decrypt a message
        {
            byte [] CipherText = Encoding.Unicode.GetBytes(EncryptedText);

            ICryptoTransform Decryption = AESProvider.CreateDecryptor(AESProvider.Key, AESProvider.IV);
            string PlainText = null;

            using (MemoryStream msDecrypt = new MemoryStream(CipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, Decryption, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        PlainText = srDecrypt.ReadToEnd(); //Read the decrypted bytes from the decrypting stream and place them in a string.
                    }
                }
            }

            return PlainText;
        }

    }
}

I also have code to do RSA as well as a Windows built in encryption if you are interested in them as well. Also, let me know if you have any questions about the code above.

(I love Encryption, it's a part of my degree)

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.