AESEncrypt.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ErgometerLibrary
  9. {
  10. static class AESEncrypt
  11. {
  12. // Change these keys
  13. private static byte[] Key = { 47, 20, 192, 222, 214, 221, 193, 151, 182, 182, 20, 246, 80, 235, 182, 216, 116, 141, 188, 191, 13, 159, 1, 137, 151, 150, 29, 171, 240, 21, 251, 252 };
  14. private static byte[] Vector = { 147, 16, 78, 223, 97, 189, 43, 197, 244, 102, 242, 123, 184, 101, 75, 203 };
  15. private static ICryptoTransform EncryptorTransform, DecryptorTransform;
  16. private static System.Text.UTF8Encoding UTFEncoder;
  17. static AESEncrypt()
  18. {
  19. //This is our encryption method
  20. RijndaelManaged rm = new RijndaelManaged();
  21. //Create an encryptor and a decryptor using our encryption method, key, and vector.
  22. EncryptorTransform = rm.CreateEncryptor(Key, Vector);
  23. DecryptorTransform = rm.CreateDecryptor(Key, Vector);
  24. //Used to translate bytes to text and vice versa
  25. UTFEncoder = new System.Text.UTF8Encoding();
  26. }
  27. /// -------------- Two Utility Methods (not used but may be useful) -----------
  28. /// Generates an encryption key.
  29. static public byte[] GenerateEncryptionKey()
  30. {
  31. //Generate a Key.
  32. RijndaelManaged rm = new RijndaelManaged();
  33. rm.GenerateKey();
  34. return rm.Key;
  35. }
  36. /// Generates a unique encryption vector
  37. static public byte[] GenerateEncryptionVector()
  38. {
  39. //Generate a Vector
  40. RijndaelManaged rm = new RijndaelManaged();
  41. rm.GenerateIV();
  42. return rm.IV;
  43. }
  44. /// ----------- The commonly used methods ------------------------------
  45. /// Encrypt some text and return a string suitable for passing in a URL.
  46. public static string EncryptToString(string TextValue)
  47. {
  48. return ByteArrToString(Encrypt(TextValue));
  49. }
  50. /// Encrypt some text and return an encrypted byte array.
  51. public static byte[] Encrypt(string TextValue)
  52. {
  53. //Translates our text value into a byte array.
  54. Byte[] bytes = UTFEncoder.GetBytes(TextValue);
  55. //Used to stream the data in and out of the CryptoStream.
  56. MemoryStream memoryStream = new MemoryStream();
  57. /*
  58. * We will have to write the unencrypted bytes to the stream,
  59. * then read the encrypted result back from the stream.
  60. */
  61. #region Write the decrypted value to the encryption stream
  62. CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write);
  63. cs.Write(bytes, 0, bytes.Length);
  64. cs.FlushFinalBlock();
  65. #endregion
  66. #region Read encrypted value back out of the stream
  67. memoryStream.Position = 0;
  68. byte[] encrypted = new byte[memoryStream.Length];
  69. memoryStream.Read(encrypted, 0, encrypted.Length);
  70. #endregion
  71. //Clean up.
  72. cs.Close();
  73. memoryStream.Close();
  74. return encrypted;
  75. }
  76. /// The other side: Decryption methods
  77. public static string DecryptString(string EncryptedString)
  78. {
  79. return Decrypt(StrToByteArray(EncryptedString));
  80. }
  81. /// Decryption when working with byte arrays.
  82. public static string Decrypt(byte[] EncryptedValue)
  83. {
  84. #region Write the encrypted value to the decryption stream
  85. MemoryStream encryptedStream = new MemoryStream();
  86. CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write);
  87. decryptStream.Write(EncryptedValue, 0, EncryptedValue.Length);
  88. decryptStream.FlushFinalBlock();
  89. #endregion
  90. #region Read the decrypted value from the stream.
  91. encryptedStream.Position = 0;
  92. Byte[] decryptedBytes = new Byte[encryptedStream.Length];
  93. encryptedStream.Read(decryptedBytes, 0, decryptedBytes.Length);
  94. encryptedStream.Close();
  95. #endregion
  96. return UTFEncoder.GetString(decryptedBytes);
  97. }
  98. /// Convert a string to a byte array. NOTE: Normally we'd create a Byte Array from a string using an ASCII encoding (like so).
  99. // System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  100. // return encoding.GetBytes(str);
  101. // However, this results in character values that cannot be passed in a URL. So, instead, I just
  102. // lay out all of the byte values in a long string of numbers (three per - must pad numbers less than 100).
  103. public static byte[] StrToByteArray(string str)
  104. {
  105. if (str.Length == 0)
  106. throw new Exception("Invalid string value in StrToByteArray");
  107. byte val;
  108. byte[] byteArr = new byte[str.Length / 3];
  109. int i = 0;
  110. int j = 0;
  111. do
  112. {
  113. val = byte.Parse(str.Substring(i, 3));
  114. byteArr[j++] = val;
  115. i += 3;
  116. }
  117. while (i < str.Length);
  118. return byteArr;
  119. }
  120. // Same comment as above. Normally the conversion would use an ASCII encoding in the other direction:
  121. // System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  122. // return enc.GetString(byteArr);
  123. public static string ByteArrToString(byte[] byteArr)
  124. {
  125. byte val;
  126. string tempStr = "";
  127. for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
  128. {
  129. val = byteArr[i];
  130. if (val < (byte)10)
  131. tempStr += "00" + val.ToString();
  132. else if (val < (byte)100)
  133. tempStr += "0" + val.ToString();
  134. else
  135. tempStr += val.ToString();
  136. }
  137. return tempStr;
  138. }
  139. }
  140. }