C#编程之AES加密(三)

这一章我们将上一章的内容做进一步完善,由用户输入需要加密的序列号,进行加密:

因为我们输入的都是以字符的形式读取,所以第一步要将读取到的字符存入到数组中: char[] inputBuf = str.ToCharArray(); 

之后对这个数组进行转换成16进制,例如输入A5两个字符,我们要将其转成16进制数,即为0xA5,并将其装入byte数组内。

                            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.");

最后有我们内部定义的秘钥对这个byte数组元素进行加密,完成我们的加密算法:

                        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);

为了美观,我们可以对字体进行颜色修改: Console.ForegroundColor = ConsoleColor.yourColorCode; 

完整代码:

 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

编译运行:

至此,我们已经基本掌握了AES加密算法。从下一章开始,我们将开始着手设计由接收有外设备通过串口发送过来的序列号,并对这些序列号进行加密,最后再通过串口发送给外设备。

End.

谢谢.

猜你喜欢

转载自www.cnblogs.com/lumao1122-Milolu/p/11989333.html