暂无描述

Authentication.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. namespace WpfApplication19
  8. {
  9. class Authentication
  10. {
  11. static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
  12. {
  13. // Check arguments.
  14. if (plainText == null || plainText.Length <= 0)
  15. throw new ArgumentNullException("plainText");
  16. if (Key == null || Key.Length <= 0)
  17. throw new ArgumentNullException("Key");
  18. if (IV == null || IV.Length <= 0)
  19. throw new ArgumentNullException("IV");
  20. // Declare the stream used to encrypt to an in memory
  21. // array of bytes.
  22. MemoryStream msEncrypt = null;
  23. // Declare the RijndaelManaged object
  24. // used to encrypt the data.
  25. RijndaelManaged aesAlg = null;
  26. try
  27. {
  28. // Create a RijndaelManaged object
  29. // with the specified key and IV.
  30. aesAlg = new RijndaelManaged();
  31. aesAlg.Key = Key;
  32. aesAlg.IV = IV;
  33. // Create an encrypto to perform the stream transform.
  34. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  35. // Create the streams used for encryption.
  36. msEncrypt = new MemoryStream();
  37. using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  38. {
  39. using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  40. {
  41. //Write all data to the stream.
  42. swEncrypt.Write(plainText);
  43. }
  44. }
  45. }
  46. finally
  47. {
  48. // Clear the RijndaelManaged object.
  49. if (aesAlg != null)
  50. aesAlg.Clear();
  51. }
  52. // Return the encrypted bytes from the memory stream.
  53. return msEncrypt.ToArray();
  54. }
  55. static string decryptStringFromBytes_AES(byte[] cipherText, byte[] Key, byte[] IV)
  56. {
  57. // Check arguments.
  58. if (cipherText == null || cipherText.Length <= 0)
  59. throw new ArgumentNullException("cipherText");
  60. if (Key == null || Key.Length <= 0)
  61. throw new ArgumentNullException("Key");
  62. if (IV == null || IV.Length <= 0)
  63. throw new ArgumentNullException("IV");
  64. // Declare the RijndaelManaged object
  65. // used to decrypt the data.
  66. RijndaelManaged aesAlg = null;
  67. // Declare the string used to hold
  68. // the decrypted text.
  69. string plaintext = null;
  70. try
  71. {
  72. // Create a RijndaelManaged object
  73. // with the specified key and IV.
  74. aesAlg = new RijndaelManaged();
  75. aesAlg.Key = Key;
  76. aesAlg.IV = IV;
  77. // Create a decrytor to perform the stream transform.
  78. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  79. // Create the streams used for decryption.
  80. using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  81. {
  82. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  83. {
  84. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  85. // Read the decrypted bytes from the decrypting stream
  86. // and place them in a string.
  87. plaintext = srDecrypt.ReadToEnd();
  88. }
  89. }
  90. }
  91. finally
  92. {
  93. // Clear the RijndaelManaged object.
  94. if (aesAlg != null)
  95. aesAlg.Clear();
  96. }
  97. return plaintext;
  98. }
  99. }
  100. }