⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des.txt

📁 这是问同学要的
💻 TXT
📖 第 1 页 / 共 2 页
字号:
这个加密算法应该是.NET   类库中有提供的,可以自己查查MSDN   ,System.Security.Cryptography     空间中的   DES   类     
    
  private   static   void   EncryptData(String   inName,   String   outName,   byte[]   desKey,   byte[]   desIV)   
    {           
            //Create   the   file   streams   to   handle   the   input   and   output   files.   
            FileStream   fin   =   new   FileStream(inName,   FileMode.Open,   FileAccess.Read);   
            FileStream   fout   =   new   FileStream(outName,   FileMode.OpenOrCreate,   FileAccess.Write);   
            fout.SetLength(0);   
                  
            //Create   variables   to   help   with   read   and   write.   
            byte[]   bin   =   new   byte[100];   //This   is   intermediate   storage   for   the   encryption.   
            long   rdlen   =   0;                             //This   is   the   total   number   of   bytes   written.   
            long   totlen   =   fin.Length;         //This   is   the   total   length   of   the   input   file.   
            int   len;                                           //This   is   the   number   of   bytes   to   be   written   at   a   time.   
      
            DES   des   =   new   DESCryptoServiceProvider();                       
            CryptoStream   encStream   =   new   CryptoStream(fout,   des.CreateEncryptor(desKey,   desIV),   CryptoStreamMode.Write);   
                                    
            Console.WriteLine("Encrypting...");   
      
            //Read   from   the   input   file,   then   encrypt   and   write   to   the   output   file.   
            while(rdlen   <   totlen)   
            {   
                    len   =   fin.Read(bin,   0,   100);   
                    encStream.Write(bin,   0,   len);   
                    rdlen   =   rdlen   +   len;   
                    Console.WriteLine("{0}   bytes   processed",   rdlen);   
            }   
      
            encStream.Close();       
            fout.Close();   
            fin.Close();                                         
    }   
      
  可以去看看     http://msdn.microsoft.com/library/chs/


1.    处理密钥: 
1.1  从用户处获得64位密钥.(每第8位为校验位,为使密钥有正确的奇偶校验,每 
个密钥要有奇 
     数个”1”位.(本文如未特指,均指二进制位) 
1.2    具体过程: 
1.2.1    对密钥实施变换,使得变换以后的密钥的各个位与原密钥位对应关系如 
下表所示: 
         表一为忽略校验位以后情况 

1      2     3     4     5     6     7     8     9    10    11    12   
  13    14    15    16    17    18    19    20    21    22    23    24 
    25    26    27    28 
57    49    41    33    25    17     9     1    58    50    42    34   
  26    18    10     2    59    51    43    35    27    19    11     3 
    60    52    44    36 
29    30    31    32    33    34    35    36    37    38    39    40   
  41    42    43    44    45    46    47    48    49    50    51    52 
    53    54    55    56 
63    55    47    39    31    23    15     7    62    54    46    38   
  30    22    14     6    61    53    45    37    29    21    13     5 
    28    20    12     4 

1.2.2  把变换后的密钥等分成两部分,前28位记为C[0], 后28位记为D[0]. 
1.2.3  计算子密钥(共16个), 从i=1开始。 
1.2.3.1 分别对C[i-1],D[i-1]作循环左移来生成C[i],D[i].(共16次)。每次循环 
左移位数 
        如下表所示: 

循环次数    1    2    3    4    5    6    7    8    9    10    11    1 
2    13    14    15    16 
左移位数    1    1    2    2    2    2    2    2    1     2     2       
2     2     2     2     1 

1.2.3.2 串联C[i],D[i],得到一个56位数,然后对此数作如下变换以产生48位子 
密钥K[i]。 
        变换过程如下: 

1      2     3     4     5     6     7     8     9    10    11    12   
  13    14    15    16    17    18    19    20    21    22    23    24 

14    17    11    24     1     5     3    28    15     6    21    10   
  23    19    12     4    26     8    16     7    27    20    13     2 

25    26    27    28    29    30    31    32    33    34    35    36   
  37    38    39    40    41    42    43    44    45    46    47    48 

41    52    31    37    47    55    30    40    51    45    33    48   
  44    49    39    56    34    53    46    42    50    36    29    32 


1.2.3.3    按以上方法计算出16个子密钥。 


2.对64位数据块的处理: 
2.1 把数据分成64位的数据块,不够64位的以适当的方式填补。 
2.2对数据块作变换。 

bit    goes to bit    bit    goes to bit 
58      1             57     33 
50      2             49     34 
42      3             41     35 
34      4             33     36 
26      5             25     37 
18      6             17     38 
10      7              9     39 
2       8              1     40 
60      9             59     41 
52     10             51     42 
44     11             43     43 
36     12             35     44 
28     13             27     45 
20     14             19     46 
12     15             11     47 
4      16              3     48 
62     17             61     49 
54     18             53     50 
46     19             45     51 
38     20             37     52 
30     21             29     53 
22     22             21     54 
14     23             13     55 
6      24              5     56 
64     25             63     57 
56     26             55     58 
48     27             47     59 
40     28             39     60 
32     29             31     61 
24     30             23     62 
16     31             15     63 
8      32              7     64   

2.3 将变换后的数据块等分成前后两部分,前32位记为L[0],后32位记为R[0]。 

2.4 用16个子密钥对数据加密。 
2.4.1 根据下面的扩冲函数E,扩展32位的成48位 

bit    goes to bit    bit    goes to bit    bit    goes to bit    bit   
   goes to bit 
32      1              8     13             16     25             24   
   37 
1       2              9     14             17     26             25   
   38 
2       3             10     15             18     27             26   
   39 
3       4             11     16             19     28             27   
   40 
4       5             12     17             20     29             28   
   41 
5       6             13     18             21     30             29   
   42 
4       7             12     19             20     31             28   
   43 
5       8             13     20             21     32             29   
   44 
6       9             14     21             22     33             30   
   45 
7      10             15     22             23     34             31   
   46 
8      11             16     23             24     35             32   
   47 
9      12             17     24             25     36              1   
   48 

2.4.2 用E{R[i-1]}与K[i]作异或运算。 
2.4.3 把所得的48位数分成8个6位数。1-6位为B[1],7-12位为B[2],……43-4 
8位为B[8]。 
2.4.4 用S密箱里的值替换B[j]。从j=1开始。S密箱里的值为4位数,共8个S密 
2.4.4.1 取出B[j]的第1和第6位串联起来成一个2位数,记为m.。m即是S密箱 
里用来替换 
           B[j]的数所在的列数。 
2.4.4.2 取出B[j]的第2至第5位串联起来成一个4位数,记为n。n即是S密箱里 
用来替换 
           B[j]的数所在的行数。 
2.4.4.3 用S密箱里的值S[j][ m][ n]替换B[j]。8个S密箱如下所示: 

-------- 
S-BOXES1 

Binary    d1d6 =>    00    01    10    11 
/ d2..d5 /    Dec   0     1     2     3 
0000       0         14     0     4    15 
0001       1          4    15     1    12 
0010       2         13     7    14     8 
0011       3          1     4     8     2 
0100       4          2    14    13     4 
0101       5         15     2     6     9 
0110       6         11    13     2     1 
0111       7          8     1    11     7 
1000       8          3    10    15     5 
1001       9         10     6    12    11 
1010      10          6    12     9     3 
1011      11         12    11     7    14 
1100      12          5     9     3    10 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -