C-language monolithic and C # language server-side des and 3DES encryption implementation

Source: Internet
Author: User
Tags decrypt

The company has recently been doing a microcontroller and C # language communication. Socket communication is used. The transmitted data is plaintext and later discussed at the meeting is prepared with DES encryption (symmetric encryption) to do.

Both parties agree on the corresponding "key".

Previously encrypted encryption is generally used in C # encryption and C # decryption. Always thought it was a simple problem, and now with the C-write MCU communication when there is a problem.

What is the problem?

I found a few online encryption and decryption of the site, but also under a few encryption and decryption tools. Results in the same data, but with the same key, the results are different.

And now on-line C language implementation of the DES data is basically not reliable, many are wrong, are you copy me, I copied you, copied to copy, copied to the end without a full implementation of DES several modes of encryption.

No way, hurriedly to find des principle of view, a good study now finally resolved to complete. Ensures that the code you write has the same results as several tools. Now put the code on, hoping to help everyone's busy.

One, ECB mode
The ECB (Electronic Code Book Code) mode, relatively simple, the data in accordance with every 8 bytes of des plus decryption (the first decryption operation must be 8 bytes, which is determined by the algorithm), if the last paragraph less than 8 bytes, The calculation is done by adding 0x00 or 0xFF as needed. Then all the data is concatenated together in the order of data.

This model said so much, I do not understand what it means, but now the online spread of C language implementation of the DES encryption algorithm is basically this mode.

With this mode, it is not available to set the encryption vector.

Note that I have implemented this pattern here, so the text of the encryption vector related to the statement, you can ignore it, you delete it, you will get the same result.
Second, CBC mode
CBC (Cipher-block chaining cipher Group link) mode, this mode makes the data there are some links, the implementation of the principle is more complex, I do not understand, but in the past when writing C # code, there is a cryptographic vector, like the following code, such as a byte array

  Private Static byte 0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF };

I never understood what it was for, and now I understand that the default for C # is this pattern.

Now on the implementation Code of C #:

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication3{classProgram {Static voidMain (string[] args) {            stringstr ="ABCDEFGH"; stringSTR1 ="1234567887654321"; stringSTR2 =des3encrypt (str, str1);            Console.WriteLine (STR2);        Console.WriteLine (Des3decrypt (STR2, str1)); }        #regionDes plus decryptionPrivate Static byte[] IV = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};///This piece of code can be deleted//default key Vector//private static byte[] IV = {0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01};        /// <summary>        ///des encryption/// </summary>        /// <param name= "Input" >string to encrypt</param>        /// <param name= "key" >encryption Key</param>        /// <returns></returns>         Public Static stringEncrypt (stringPtoencrypt,stringSKey) {DESCryptoServiceProvider provider=NewDESCryptoServiceProvider (); Provider. Mode=CIPHERMODE.CBC; //provider. Padding = Paddingmode.none;            byte[] bytes =Encoding.Default.GetBytes (Ptoencrypt); Provider. Key=Encoding.ASCII.GetBytes (SKey); Provider.iv=Encoding.ASCII.GetBytes (SKey); /*Create a memory stream object*/MemoryStream Stream=NewMemoryStream (); /*Create an encrypted stream object*/CryptoStream stream2=NewCryptoStream (stream, provider.            CreateEncryptor (), cryptostreammode.write); /*write the encrypted text to the encrypted stream*/stream2. Write (Bytes,0, Bytes.            Length); /*Update buffering*/stream2.            FlushFinalBlock (); /*Get encrypted text*/StringBuilder Builder=NewStringBuilder (); foreach(byteNuminchStream. ToArray ()) {Builder. AppendFormat ("{0:x2}", num); } stream2.            Close (); Stream.            Close (); returnBuilder.            ToString (); //return convert.tobase64string (stream.            ToArray ()); //byte[] Bytes4 = stream.            ToArray (); //string str=encoding.default.getstring (BYTES4); //return str;        }        /// <summary>        ///des decryption/// </summary>        /// <param name= "Input" >string to decrypt</param>        /// <param name= "key" >Decryption key, required to be 8-bit, same as encryption key</param>        /// <returns>decryption succeeded in returning the decrypted string, failed to return the source string</returns>         Public Static stringDecrypt (stringDecryptstring,stringKey) {            Try            {                //byte[] Inputbytearray = convert.frombase64string (decryptstring);                /** * * Convert a string into a 16 byte array **/                byte[] Inputbytearray =New byte[Decryptstring.length/2];  for(intx =0; X < Decryptstring.length/2; X + +)                {                    inti = (Convert.ToInt32 (decryptstring.substring (x *2,2), -)); INPUTBYTEARRAY[X]= (byte) I; }                //byte[] Inputbytearray = Encoding.UTF8.GetBytes (decryptstring);DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); Des. Mode=CIPHERMODE.CBC; Des. Padding=Paddingmode.none; Des. Key=Encoding.ASCII.GetBytes (Key); Des.iv=Encoding.ASCII.GetBytes (Key); MemoryStream Mstream=NewMemoryStream (); CryptoStream Cstream=NewCryptoStream (Mstream, Des.                CreateDecryptor (), cryptostreammode.write); Cstream.write (Inputbytearray,0, inputbytearray.length);                Cstream.flushfinalblock ();                Mstream.close ();                Cstream.close (); returnEncoding.Default.GetString (Mstream.toarray ()); }            Catch            {                return ""; }        }        #endregion        #region3DES Encryption Decryption Public Static stringDes3encrypt (stringDatastringkey) {TripleDESCryptoServiceProvider DES=NewTripleDESCryptoServiceProvider (); //DES.            GenerateKey (); //byte[] Ckey = DES. Key;DES. Key=ASCIIEncoding.ASCII.GetBytes (key); Des. Mode=CIPHERMODE.ECB; Des. Padding=Paddingmode.none; ICryptoTransform desencrypt=DES.            CreateEncryptor (); byte[] Buffer =ASCIIEncoding.ASCII.GetBytes (data); byte[] result = Desencrypt.transformfinalblock (Buffer,0, buffer.length); StringBuilder Builder=NewStringBuilder (); foreach(byteNuminchresult) {Builder. AppendFormat ("{0:x2}", num); }            returnBuilder.            ToString (); //return convert.tobase64string (Desencrypt.transformfinalblock (Buffer, 0, Buffer.length));        }         Public Static stringDes3decrypt (stringDecryptstring,stringkey) {TripleDESCryptoServiceProvider DES=NewTripleDESCryptoServiceProvider (); //DES. GenerateKey ();Des. Key =ASCIIEncoding.ASCII.GetBytes (key); Des. Mode=CIPHERMODE.ECB; Des. Padding=Paddingmode.none; ICryptoTransform Desdecrypt=DES.            CreateDecryptor (); stringresult =""; Try            {                //byte[] Buffer = convert.frombase64string (data);                /*string to 16 binary byte array*/                byte[] Inputbytearray =New byte[Decryptstring.length/2];  for(intx =0; X < Decryptstring.length/2; X + +)                {                    inti = (Convert.ToInt32 (decryptstring.substring (x *2,2), -)); INPUTBYTEARRAY[X]= (byte) I; }                //byte[] Byteresult = Desdecrypt.transformfinalblock (inputbytearray, 0, inputbytearray.length); //StringBuilder builder = new StringBuilder (); //foreach (Byte num in byteresult)//{                //Builder.                AppendFormat ("{0:x2}", num); //}                //return builder. ToString ();result = ASCIIEncoding.ASCII.GetString (Desdecrypt.transformfinalblock (Inputbytearray,0, inputbytearray.length)); }            Catch(Exception e) {}returnresult; }        #endregion    }}

Then we compare the effect:

This is a tool after the effect of encryption, using DES.

The string to encrypt is: ABCDEFG

The encryption key is: 12345678

This is the cryptographic decryption of C #:

OK the same effect.

Then the microcontroller that side of the implementation of the effect to show:

Of course C language version is I download someone else's code, link here

Http://files.cnblogs.com/erwin/yxyDES2_C_Edition.rar

C-language monolithic and C # language server-side des and 3DES encryption implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.