C # Programming AES encryption (three)

In this chapter we will further improve the content of the previous chapter, entered by the user needs to be encrypted serial number, is encrypted:

Because all of us to read in the form of character input, so the first step you want to read characters into the array:  char [] = inputBuf str.ToCharArray (); 

After the array is converted to hexadecimal, e.g. two A5 input characters, we want to convert it to hexadecimal, that is, 0xA5, and loaded into the byte array.

                            if (inputBuf[j] >= '0' && '9' >= inputBuf[j]) outBuf[i] = (byte)(inputBuf[j++] - '0');
                            else if (('f' >= inputBuf[j]) && (inputBuf[j] >= 'a')) outBuf[i] = (byte)(inputBuf[j++] - 0x57);
                            else if (('F' >= inputBuf[j]) && (inputBuf[j] >= 'A')) outBuf[i] = (byte)(inputBuf[j++] - 0x37);
                            else throw new Exception("ERROR: Format incorrect.");
                            outBuf[i] <<= 4;
                            if (inputBuf[j] >= '0' && '9' >= inputBuf[j]) outBuf[i] |= (byte)(inputBuf[j++] - '0');
                            else if (('f' >= inputBuf[j]) && (inputBuf[j] >= 'a')) outBuf[i] |= (byte)(inputBuf[j++] - 0x57);
                            else if (('F' >= inputBuf[j]) && (inputBuf[j] >= 'A')) outBuf[i] |= (byte)(inputBuf[j++] - 0x37);
                            else throw new Exception("ERROR: Format incorrect.");

Finally, we have internally defined keys to encrypt this byte array elements to complete our encryption algorithms:

                        aes.IV = new byte[16];
                        aes.Key = new byte[] { 0x1F, 0x54, 0x52, 0x6A, 0x73, 0x93, 0x58, 0x9E, 0x4B, 0xCF, 0xFB, 0xAE, 0xFC, 0x97, 0x59, 0x3E };
                        //aes.Mode = CipherMode.CBC;
                        aes.Padding = PaddingMode.None;
                        var cryptoTransform = aes.CreateEncryptor();
                        byte[] resultBuff = cryptoTransform.TransformFinalBlock(outBuf, 0, outBuf.Length);

For aesthetics, we can modify the font color:  Console.ForegroundColor = ConsoleColor.yourColorCode; 

Complete code:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Security.Cryptography;
 7 
 8 namespace aesDemo
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Console.WriteLine("========================================================================");
15             Console.WriteLine(" Copyright:   GALAXY .ltd in China");
16             Console.WriteLine(" Designed by: Milo lu");
17             Console.WriteLine(" Date:        Des 05  2019");
18             Console.WriteLine(" Version:     12052019");
19             Console.WriteLine("e.g.");
20             Console.WriteLine(" enter:FD5D9BE05cdb8376a1861896fd5d9be0");
21             Console.WriteLine(" print: 05F14C987E2E65FC6AB5A41CA7B5429D");
22             Console.WriteLine("=========================================================================");
23             int color = 2;
24             while (true)
25             {
26                 try
27                 {
28                     using (var aes = new RijndaelManaged())
29                     {
30                         Console.ForegroundColor = (System.ConsoleColor)color++;
31                         if (color >= 16) color = 2;
32                         Console.WriteLine("-------------------------------------------");
33                         Console.WriteLine("Enter 16 hex:");
34                         string str = Console.ReadLine();
35 
36                         if(str.Length!=32)
37                         {
38                             if (str == "exit" || str == "EXIT") 
39                             {
40                                 break;
41                             }
42                             throw new Exception("ERROR: Enter 16 hex.");
43                         }
44                         char[] inputBuf = str.ToCharArray();
45                         byte[] outBuf=new byte[16];
46                         for (int i = 0, j = 0; i < 16; i++)
47                         {
48                             if (inputBuf[j] >= '0' && '9' >= inputBuf[j]) outBuf[i] = (byte)(inputBuf[j++] - '0');
49                             else if (('f' >= inputBuf[j]) && (inputBuf[j] >= 'a')) outBuf[i] = (byte)(inputBuf[j++] - 0x57);
50                             else if (('F' >= inputBuf[j]) && (inputBuf[j] >= 'A')) outBuf[i] = (byte)(inputBuf[j++] - 0x37);
51                             else throw new Exception("ERROR: Format incorrect.");
52                             outBuf[i] <<= 4;
53                             if (inputBuf[j] >= '0' && '9' >= inputBuf[j]) outBuf[i] |= (byte)(inputBuf[j++] - '0');
54                             else if (('f' >= inputBuf[j]) && (inputBuf[j] >= 'a')) outBuf[i] |= (byte)(inputBuf[j++] - 0x57);
55                             else if (('F' >= inputBuf[j]) && (inputBuf[j] >= 'A')) outBuf[i] |= (byte)(inputBuf[j++] - 0x37);
56                             else throw new Exception("ERROR: Format incorrect.");
57                         }
58                         /// aes cryption
59                         aes.IV = new byte[16];
60                         aes.Key = new byte[] { 0x1F, 0x54, 0x52, 0x6A, 0x73, 0x93, 0x58, 0x9E, 0x4B, 0xCF, 0xFB, 0xAE, 0xFC, 0x97, 0x59, 0x3E };
61                         //aes.Mode = CipherMode.CBC;
62                         aes.Padding = PaddingMode.None;
63                         var cryptoTransform = aes.CreateEncryptor();
64                         byte[] resultBuff = cryptoTransform.TransformFinalBlock(outBuf, 0, outBuf.Length);
65                         Console.WriteLine("Encoding: ");
66                         foreach (byte i in resultBuff)
67                             Console.Write("{0:X2}", i);
68                         Console.WriteLine();
69                         Console.WriteLine("Decoding:");
70                         cryptoTransform = aes.CreateDecryptor();
71                         resultBuff = cryptoTransform.TransformFinalBlock(resultBuff, 0, resultBuff.Length);
72                         foreach (byte i in resultBuff)
73                             Console.Write("{0:X2}", i);
74                         Console.WriteLine();
75                     }
76                 }
77                 catch (Exception ex)
78                 {
79                     Console.WriteLine(ex.Message);
80                 }
81             }
82         }
83     }
84 }
View Code

Compile and run:

 

So far, we have basically mastered the AES encryption algorithm. From the beginning of the next chapter, we will begin to design a serial number outside the receiving device through the serial port to send over, and the serial number is encrypted, and finally sent to the external device through the serial port.

End.

Thank you.

Guess you like

Origin www.cnblogs.com/lumao1122-Milolu/p/11989333.html