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. var tempData = "This is the text to encrypt. It's not much, but it's all there is.";
  11. byte[] iv;
  12. byte[] key;
  13. byte[] encryptedData;
  14. using (var rijCrypto = new RijndaelManaged())
  15. {
  16. rijCrypto.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
  17. rijCrypto.KeySize = 256;
  18. rijCrypto.GenerateIV();
  19. rijCrypto.GenerateKey();
  20. iv = new byte[rijCrypto.IV.Length];
  21. rijCrypto.IV.CopyTo(iv, 0);
  22. key = new byte[rijCrypto.Key.Length];
  23. rijCrypto.Key.CopyTo(key, 0);
  24.  
  25. using (var input = new MemoryStream(Encoding.Unicode.GetBytes(tempData)))
  26. using (var output = new MemoryStream())
  27. {
  28. var encryptor = rijCrypto.CreateEncryptor();
  29.  
  30. using (var cryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
  31. {
  32. var buffer = new byte[1024];
  33. var read = input.Read(buffer, 0, buffer.Length);
  34. while (read > 0)
  35. {
  36. cryptStream.Write(buffer, 0, read);
  37. read = input.Read(buffer, 0, buffer.Length);
  38. }
  39. cryptStream.FlushFinalBlock();
  40. encryptedData = output.ToArray();
  41. }
  42. }
  43. }
  44.  
  45. using (var rijCrypto = new RijndaelManaged())
  46. {
  47. rijCrypto.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
  48. rijCrypto.KeySize = 256;
  49. rijCrypto.IV = iv;
  50. rijCrypto.Key = key;
  51.  
  52. using (var input = new MemoryStream(encryptedData))
  53. using (var output = new MemoryStream())
  54. {
  55. var decryptor = rijCrypto.CreateDecryptor();
  56. using (var cryptStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read))
  57. {
  58. var buffer = new byte[1024];
  59. var read = cryptStream.Read(buffer, 0, buffer.Length);
  60. while (read > 0)
  61. {
  62. output.Write(buffer, 0, read);
  63. read = cryptStream.Read(buffer, 0, buffer.Length);
  64. }
  65. cryptStream.Flush();
  66. var result = Encoding.Unicode.GetString(output.ToArray());
  67. Console.WriteLine(result);
  68. }
  69. }
  70. }
  71. }
  72. }
Success #stdin #stdout 0.03s 15468KB
stdin
Standard input is empty
stdout
This is the text to encrypt. It's not much, but it's all there is.