Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. string json = JsonConvert.SerializeObject(credentials);
  2. using (AesManaged myAes = new AesManaged())
  3. {
  4.  
  5. byte[] encrypted = ControlHelperscs.EncryptStringToBytes_Aes(json, myAes.Key, myAes.IV);
  6. File.WriteAllBytes(subPath, encrypted);
  7. }
  8.  
  9. using (AesManaged myAes = new AesManaged())
  10. {
  11.  
  12. byte[] file = File.ReadAllBytes(subPath);
  13.  
  14. string decrypt = ControlHelperscs.DecryptStringFromBytes_Aes(file, myAes.Key, myAes.IV);
  15. credentials = JsonConvert.DeserializeObject<LoginModel>(decrypt);
  16.  
  17. }
  18.  
  19. public static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
  20. {
  21. // Check arguments.
  22. if (plainText == null || plainText.Length <= 0)
  23. throw new ArgumentNullException("plainText");
  24. if (Key == null || Key.Length <= 0)
  25. throw new ArgumentNullException("Key");
  26. if (IV == null || IV.Length <= 0)
  27. throw new ArgumentNullException("IV");
  28. byte[] encrypted;
  29.  
  30. // Create an AesManaged object
  31. // with the specified key and IV.
  32. using (AesManaged aesAlg = new AesManaged())
  33. {
  34. aesAlg.Key = Key;
  35. aesAlg.IV = IV;
  36.  
  37. // Create an encryptor to perform the stream transform.
  38. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  39.  
  40. // Create the streams used for encryption.
  41. using (MemoryStream msEncrypt = new MemoryStream())
  42. {
  43. using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  44. {
  45. using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  46. {
  47. //Write all data to the stream.
  48. swEncrypt.Write(plainText);
  49. }
  50. encrypted = msEncrypt.ToArray();
  51. }
  52. }
  53. }
  54.  
  55.  
  56. // Return the encrypted bytes from the memory stream.
  57. return encrypted;
  58.  
  59. }
  60.  
  61. public static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
  62. {
  63. // Check arguments.
  64. if (cipherText == null || cipherText.Length <= 0)
  65. throw new ArgumentNullException("cipherText");
  66. if (Key == null || Key.Length <= 0)
  67. throw new ArgumentNullException("Key");
  68. if (IV == null || IV.Length <= 0)
  69. throw new ArgumentNullException("IV");
  70.  
  71. // Declare the string used to hold
  72. // the decrypted text.
  73. string plaintext = null;
  74.  
  75. // Create an AesManaged object
  76. // with the specified key and IV.
  77. using (AesManaged aesAlg = new AesManaged())
  78. {
  79. aesAlg.Key = Key;
  80. aesAlg.IV = IV;
  81.  
  82. // Create a decryptor to perform the stream transform.
  83. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  84.  
  85. // Create the streams used for decryption.
  86. using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  87. {
  88. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  89. {
  90. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  91. {
  92.  
  93. // Read the decrypted bytes from the decrypting stream
  94. // and place them in a string.
  95. plaintext = srDecrypt.ReadToEnd();
  96. }
  97. }
  98. }
  99.  
  100. }
  101.  
  102. return plaintext;
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement