fork(1) download
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. string mensaje = "Hola Mundo! Este es un mensaje secreto";
  11. Console.WriteLine("Original: " + mensaje);
  12. /* Emisor */
  13. Seguridad rijndael1 = new Seguridad();
  14. string cifrado = rijndael1.EncryptToString(mensaje);
  15. Console.WriteLine("Cifrado: " + cifrado);
  16.  
  17. /* Receptor, instancia de seguridad separada por que es otra aplicacion la que recibe el texto encriptado */
  18. Seguridad rijndael2 = new Seguridad();
  19. string descifrado = rijndael2.DecryptToString(cifrado);
  20. Console.WriteLine("Descifrado: " + descifrado);
  21.  
  22. Console.ReadKey();
  23. }
  24.  
  25. public class Seguridad
  26. {
  27. private const string _claveSecreta = "esUnaClaveSecretaDe32Caracteres_"; /* 32 caracteres.*/
  28. private const string _IVScreta = "1234567890abcdef"; /*16 caracteres, pues debe ser la mitad de la clave*/
  29. private ICryptoTransform EncryptorTransform, DecryptorTransform;
  30. private System.Text.ASCIIEncoding TextEncoder;
  31.  
  32. /// <summary>
  33. /// Constructor
  34. /// </summary>
  35. public Seguridad()
  36. {
  37. TextEncoder = new System.Text.ASCIIEncoding();
  38.  
  39. using (RijndaelManaged raindoll = new RijndaelManaged())
  40. {
  41. raindoll.BlockSize = 128; // bloques de 128 bits los hace compatible con AES.
  42. raindoll.KeySize = 256; // es el size por defecto.
  43. raindoll.Padding = PaddingMode.PKCS7;
  44. raindoll.Key = UTF8Encoding.UTF8.GetBytes(_claveSecreta); /* Longitud de la clave es de 32 bytes. 32*8=256 que es el keysize*/
  45. raindoll.IV = UTF8Encoding.UTF8.GetBytes(_IVScreta);
  46. EncryptorTransform = raindoll.CreateEncryptor();
  47. DecryptorTransform = raindoll.CreateDecryptor();
  48.  
  49. }
  50. }
  51.  
  52. public string EncryptToString(string texto)
  53. {
  54. return Convert.ToBase64String(Encrypt(texto));
  55. }
  56.  
  57. public string DecryptToString(string textoCifrado)
  58. {
  59. return Decrypt(Convert.FromBase64String(textoCifrado));
  60. }
  61.  
  62. /// <summary>
  63. /// Encriptador
  64. /// </summary>
  65. /// <param name="texto">Texto a cifrar</param>
  66. /// <returns>cadena de bytes</returns>
  67. private byte[] Encrypt(string texto)
  68. {
  69. Byte[] encrypted = null;
  70. Byte[] bytesText = TextEncoder.GetBytes(texto);
  71.  
  72. using (MemoryStream memStream = new MemoryStream())
  73. {
  74. using (CryptoStream encryptStream = new CryptoStream(memStream, this.EncryptorTransform, CryptoStreamMode.Write))
  75. {
  76. encryptStream.Write(bytesText, 0, bytesText.Length);
  77. encryptStream.FlushFinalBlock();
  78. }
  79. encrypted = memStream.ToArray();
  80. }
  81.  
  82. return encrypted;
  83. }
  84.  
  85. /// <summary>
  86. /// Desencriptador
  87. /// </summary>
  88. /// <param name="encryptado">cadena de bytes cifrados</param>
  89. /// <returns>texto descifrado.</returns>
  90. private string Decrypt(byte[] encryptado)
  91. {
  92. Byte[] decryptedBytes = null;
  93.  
  94. using (MemoryStream memStream = new MemoryStream())
  95. {
  96. using (CryptoStream decryptStream = new CryptoStream(memStream, this.DecryptorTransform, CryptoStreamMode.Write))
  97. {
  98. decryptStream.Write(encryptado, 0, encryptado.Length);
  99. }
  100.  
  101. decryptedBytes = memStream.ToArray();
  102. }
  103.  
  104. return this.TextEncoder.GetString(decryptedBytes);
  105. }
  106. }
  107. }
Success #stdin #stdout 0s 131648KB
stdin
Standard input is empty
stdout
Original:  Hola Mundo! Este es un mensaje secreto
Cifrado: nCAnOzMWYDQZfI0VEG48gwGxH9NV7AhmTN6PEkN92/67tYAQzYZs9T9LzRjgQJzV
Descifrado: Hola Mundo! Este es un mensaje secreto