Acerca del cifrado de Rijndael

Propiedades y métodos relacionados de la clase MSDN  https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rijndaelmanaged(v=vs.110).aspx .

Aquí pongo el uso dado en MSDN aquí para uso directo en el futuro

  1  utilizando el sistema;
  2  usando System.IO;
  3  utilizando System.Security.Cryptography;
  4  
  5  espacio de nombres RijndaelManaged_Example
   6  {
   7      class RijndaelExample
   8      {
   9          public  static  void Main()
 10          {
 11              Start( Aquí hay algunos datos para cifrar! " );
12  
13              Consola.ReadKey();
14          }
 15          privado  estático void Start( string original)
 16          {
 17              try 
18              {
 19                  using (RijndaelManaged myRijndael = new RijndaelManaged())
 20                  {
 21                      myRijndael.GenerateKey(); // genera aleatoriamente la clave 
22                      myRijndael.GenerateIV(); // genera aleatoriamente iv
 23                      // encripta la cadena y devuelve a por te[] 
24                      byte [] encriptado = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
 25                      // Decodifica la matriz de bytes obtenida previamente
ida y vuelta de 26                      cadenas = DecryptStringFromBytes (cifrado, myRijndael.Key, myRijndael.IV);
27  
28                      // Mostrar los datos originales y los datos descifrados. 
29                      Console.WriteLine( " Original: {0} " , original);
30                      Console.WriteLine( " Ida y vuelta: {0} " , ida y vuelta);
31                  }
 32              }
 33              catch (Excepción e)
 34              {
 35                  Console.WriteLine( " Error: {0} ", e.Mensaje);
36              }
 37          }
 38  
39          byte estático  [] EncryptStringToBytes( string texto sin formato, byte [] Clave, byte [] IV)
 40         {
 41 // Comprobar argumentos. 42 if (texto sin formato == nulo || texto sin formato.Longitud <= 0 )
 43 throw new ArgumentNullException ( " texto sin formato " );
44 si (Clave == nulo || Clave.Longitud <= 0 )
              
                                            45                  throw  new ArgumentNullException( " Clave " );
46              if (IV == null || IV.Length <= 0 )
 47                  throw  new ArgumentNullException( " IV " );
48              bytes [] encriptados;
49              // Cree un objeto RijndaelManaged
 50              // con la clave especificada (规定的) y IV. 
51              usando (RijndaelManaged rijAlg = new RijndaelManaged())
 52              {
 53                 rijAlg.Clave = Clave;
54                  rijAlg.IV = IV;
55  
56                  // Crear un descifrador para realizar la transformación de flujo. 
57                  Cifrador ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
58  
59                  // Crear los flujos utilizados para el cifrado. 
60                  usando (MemoryStream msEncrypt = new MemoryStream())
 61                  {
 62                      usando (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
 63                     {
 64                          usando (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
 65                          {
 66  
67                              // Escribe todos los datos en la transmisión. 
68                              swEncrypt.Write(texto sin formato);
69                          }
 70                          encriptado = msEncrypt.ToArray();
71                      }
 72                  }
 73              }
 74              retorno encriptado;
75          }
 76  
77          cadena estática DecryptStringFromBytes( byte [] cipherText, byte [] Clave, byte [] IV)
 78          {
 79              // Comprobar argumentos. 
80              if (cipherText == null || cipherText.Length <= 0 )
 81                  throw  new ArgumentNullException( " cipherText " );
82              if (Key == null || Key.Length <= 0 )
 83                  throw  new ArgumentNullException( " Key " );
84             if (IV == null || IV.Length <= 0 )
 85                  throw  new ArgumentNullException( " IV " );
86  
87              // Declara la cadena utilizada para contener
 88              // el texto descifrado. 
89              cadena de texto sin formato = nulo ;
90  
91              // Crear un objeto RijndaelManaged
 92              // con la clave especificada y IV. 
93              usando (RijndaelManaged rijAlg = new RijndaelManaged())
 94              {
 95                 rijAlg.Clave = Clave;
96                  rijAlg.IV = IV;
97  
98                  // Crear un descifrador para realizar la transformación de flujo. 
99                  Descifrador ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
100  
101                  // Crea las secuencias utilizadas para el descifrado. 
102                  usando (MemoryStream msDecrypt = new MemoryStream(cipherText))
 103                  {
 104                      usando (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
 105                     {
 106                          usando (StreamReader srDecrypt = new StreamReader(csDecrypt))
 107                          {
 108  
109                              // Lea los bytes descifrados del flujo de descifrado
 110                              // y colóquelos en una cadena. 
111                              texto sin formato = srDecrypt.ReadToEnd();
112                          }
 113                      }
 114                  }
 115  
116              }
 117  
118              devuelve texto sin formato;
119  
120          }
121      }
 122 }

 

Reimpreso en: https://www.cnblogs.com/Yukisora/p/8283517.html

Supongo que te gusta

Origin blog.csdn.net/a1808508751/article/details/101353091
Recomendado
Clasificación