none
How to use TripleDESCryptoServiceProvider to encrypt / decrypt any text RRS feed

  • Question

  • please show me how to use below code to encrypt and decrypt any text.

    public class Encryptor
    {
        private static SymmetricAlgorithm _cryptoService = new TripleDESCryptoServiceProvider(); 
        // maybe use AesCryptoServiceProvider instead?
    
        // vector and key have to match between encryption and decryption
        public static string Encrypt(string text, byte[] key, byte[] vector)
        {
            return Transform(text, _cryptoService.CreateEncryptor(key, vector));
        }
    
        // vector and key have to match between encryption and decryption
        public static string Decrypt(string text, byte[] key, byte[] vector)
        {
            return Transform(text, _cryptoService.CreateDecryptor(key, vector));
        }
    
        private static string Transform(string text, ICryptoTransform cryptoTransform)
        {
            MemoryStream stream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(stream, cryptoTransform, CryptoStreamMode.Write);
    
            byte[] input = Encoding.Default.GetBytes(text);
    
            cryptoStream.Write(input, 0, input.Length);
            cryptoStream.FlushFinalBlock();
    
            return Encoding.Default.GetString(stream.ToArray());
        }
    }

    what is byte[] key and byte[] vector ?

    i guess byte[] key is probably saltkey but what is vector and its usage ?
    guide me please about the vector usage.

    thanks

    Wednesday, March 14, 2018 8:54 AM

Answers

  • Hello Sudip_inn,

    >>what is byte[] key and byte[] vector ?

    They represent Key property and IV property respectively. There are detailed info about them as below.

    Key: The secret key is used both for encryption and for decryption. For a symmetric algorithm to be successful, the secret key must be known only to the sender and the receiver.

    IV: In order to solve problem that origin text has the same block, information from the previous block is mixed into the process of encrypting the next block. Thus, the output of two identical plain text blocks is different. Because this technique uses the previous block to encrypt the next block, an initialization vector is needed to encrypt the first block of data.

    As for how to generate Key and IV, you could define a custom personal these but they need to meet the require rules. Also you could use GenerateKey to generate a random key, what you do is to save it for next decrypt. Below is my testing code.

    class Program
        {
            static void Main(string[] args)
            {
                string Text = "The secret";
    
                SymmetricAlgorithm _cryptoService = new TripleDESCryptoServiceProvider();
    
                _cryptoService.GenerateKey();
                byte[] key = _cryptoService.Key;
    
                _cryptoService.GenerateIV();
                byte[] vector = _cryptoService.IV;
    
                string EncryptString= Encryptor.Encrypt(Text,key,vector);
    
                var result=Encryptor.Decrypt(EncryptString, key, vector);
            }
        }
    
        public class Encryptor
        {
            private static SymmetricAlgorithm _cryptoService = new TripleDESCryptoServiceProvider();
            // maybe use AesCryptoServiceProvider instead?
    
            // vector and key have to match between encryption and decryption
            public static string Encrypt(string text, byte[] key, byte[] vector)
            {
                return Transform(text, _cryptoService.CreateEncryptor(key, vector));
            }
    
            // vector and key have to match between encryption and decryption
            public static string Decrypt(string text, byte[] key, byte[] vector)
            {
                return Transform(text, _cryptoService.CreateDecryptor(key, vector));
            }
    
            private static string Transform(string text, ICryptoTransform cryptoTransform)
            {
                MemoryStream stream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(stream, cryptoTransform, CryptoStreamMode.Write);
    
                byte[] input = Encoding.Default.GetBytes(text);
    
                cryptoStream.Write(input, 0, input.Length);
                cryptoStream.FlushFinalBlock();
    
                return Encoding.Default.GetString(stream.ToArray());
            }
        }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, March 15, 2018 11:34 AM
    Thursday, March 15, 2018 6:24 AM
    Moderator

All replies

  • The documentation describes this.  The "vector" value is an initialization vector, which the TripleDESCryptoServiceProvider will generate randomly each time you create an instance.  You can fetch the value by using _cryptoService.IV.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, March 15, 2018 5:16 AM
  • Hello Sudip_inn,

    >>what is byte[] key and byte[] vector ?

    They represent Key property and IV property respectively. There are detailed info about them as below.

    Key: The secret key is used both for encryption and for decryption. For a symmetric algorithm to be successful, the secret key must be known only to the sender and the receiver.

    IV: In order to solve problem that origin text has the same block, information from the previous block is mixed into the process of encrypting the next block. Thus, the output of two identical plain text blocks is different. Because this technique uses the previous block to encrypt the next block, an initialization vector is needed to encrypt the first block of data.

    As for how to generate Key and IV, you could define a custom personal these but they need to meet the require rules. Also you could use GenerateKey to generate a random key, what you do is to save it for next decrypt. Below is my testing code.

    class Program
        {
            static void Main(string[] args)
            {
                string Text = "The secret";
    
                SymmetricAlgorithm _cryptoService = new TripleDESCryptoServiceProvider();
    
                _cryptoService.GenerateKey();
                byte[] key = _cryptoService.Key;
    
                _cryptoService.GenerateIV();
                byte[] vector = _cryptoService.IV;
    
                string EncryptString= Encryptor.Encrypt(Text,key,vector);
    
                var result=Encryptor.Decrypt(EncryptString, key, vector);
            }
        }
    
        public class Encryptor
        {
            private static SymmetricAlgorithm _cryptoService = new TripleDESCryptoServiceProvider();
            // maybe use AesCryptoServiceProvider instead?
    
            // vector and key have to match between encryption and decryption
            public static string Encrypt(string text, byte[] key, byte[] vector)
            {
                return Transform(text, _cryptoService.CreateEncryptor(key, vector));
            }
    
            // vector and key have to match between encryption and decryption
            public static string Decrypt(string text, byte[] key, byte[] vector)
            {
                return Transform(text, _cryptoService.CreateDecryptor(key, vector));
            }
    
            private static string Transform(string text, ICryptoTransform cryptoTransform)
            {
                MemoryStream stream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(stream, cryptoTransform, CryptoStreamMode.Write);
    
                byte[] input = Encoding.Default.GetBytes(text);
    
                cryptoStream.Write(input, 0, input.Length);
                cryptoStream.FlushFinalBlock();
    
                return Encoding.Default.GetString(stream.ToArray());
            }
        }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, March 15, 2018 11:34 AM
    Thursday, March 15, 2018 6:24 AM
    Moderator
  • @Fei thanks for answer.

    can we ignore vector when encrypt and decrypt ?

    if possible what to change in routine for  encrypt and decrypt ?

    what things vector does and if we do not use vector then what worse may happen ?

    share the knowledge please. thanks

    Thursday, March 15, 2018 11:36 AM
  • I don't think you read his answer at all.  You can't "not use vector".  That isn't an option.  It is a required parameter.  The code sample he provided (which came straight from the documentation page, by the way) shows you how to fetch it from the crypto provider.  You are making it way harder than it is.  You just fetch the value from the provider, and pass it to the CreateEncryptor and CreateDecryptor calls.  Straight passthrough.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, March 15, 2018 5:53 PM