2 Replies - 3256 Views - Last Post: 11 September 2012 - 05:18 AM

#1 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Encrypt\Decrypt Byte Arrays

Posted 30 August 2012 - 05:31 PM

Description:
using System.Security.Cryptography;
using System.IO;

namespace Cryptography
{
    class Cryptography
    {
        private static byte[] SALT = { 34, 65, 11, 12, 16, 0, 65, 128, 92, 72, 65, 23, 87, 11, 10, 8 };
        private enum CryptProc {ENCRYPT, DECRYPT};

        /// 
        /// Performs either an encryption or decrytion
        /// 
        /// Unencrypted byte array
        /// Password to be used
        /// Number of iterations hash algorithm uses
        /// Process to be performed
        /// Results of process in the form of a byte array
        private static byte[] CryptBytes(byte[] plain, string password, int iterations, CryptProc cryptproc)
        {
            //Create our key from the password provided
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, SALT, "SHA512", iterations);

            //We'll be using 3DES
            TripleDES des = TripleDES.Create();
            des.Key = pdb.GetBytes(24); 
            des.IV = pdb.GetBytes(8);

            MemoryStream memstream = new MemoryStream();

            ICryptoTransform cryptor = (cryptproc == CryptProc.ENCRYPT) ? des.CreateEncryptor() : des.CreateDecryptor();

            CryptoStream cryptostream = new CryptoStream(memstream, cryptor, CryptoStreamMode.Write);
            cryptostream.Write(plain, 0, plain.Length); //write finished product to our MemoryStream

            cryptostream.Close();

            return memstream.ToArray();
        }

        /// 
        /// Encrypts byte arrays
        /// 
        /// Unencrypted byte array
        /// Password to be used
        /// Number of iterations hash algorithm uses
        /// Encypted byte array
        public static byte[] EncryptBytes(byte[] plain, string password, int iterations)
        {
            return CryptBytes(plain, password, 2, CryptProc.ENCRYPT);
        }

        /// 
        /// Decrypts byte arrays
        /// 
        /// Unencrypted byte array
        /// Password to be used
        /// Number of iterations hash algorithm uses
        /// Decrypted byte array
        public static byte[] DecryptBytes(byte[] plain, string password, int iterations)
        {
            return CryptBytes(plain, password, 2, CryptProc.DECRYPT);
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Encrypt\Decrypt Byte Arrays

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5828
  • View blog
  • Posts: 19,865
  • Joined: 05-May 12

Re: Encrypt\Decrypt Byte Arrays

Posted 10 September 2012 - 05:26 PM

You should use SecureString for the password parameter.
Was This Post Helpful? 0
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Encrypt\Decrypt Byte Arrays

Posted 11 September 2012 - 05:18 AM

Thanks for the feedback. Didn't know about that class but it looks pretty good from what I'm seeing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1