retesere20

encrypt-descrypt c#

Jul 1st, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1.  
  2. #region Encrypt/Decrypt
  3. public static class EncryptDecrypt
  4. {
  5. // encryption
  6. public static string EncryptString(string messageToEncrypt, string key_ )
  7. {
  8. CryptoStream cryptoStream; MemoryStream memoryStream;
  9. helper__encrypt_decrypt_stream(out memoryStream, out cryptoStream, secterKey);
  10. string encryptedText = String.Empty;
  11. try
  12. {
  13. byte[] plainBytes = Encoding.ASCII.GetBytes(plainText); // Convert the plainText string into a byte array
  14.  
  15. cryptoStream.Write(plainBytes, 0, plainBytes.Length); // Encrypt the input plaintext string
  16.  
  17. cryptoStream.FlushFinalBlock(); // Complete the encryption process
  18.  
  19. byte[] cipherBytes = memoryStream.ToArray(); // Convert the encrypted data from a MemoryStream to a byte array
  20.  
  21.  
  22.  
  23.  
  24. encryptedText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length); // Convert the encrypted byte array to a base64 encoded string
  25. }
  26. catch (Exception e)
  27. {
  28. return e.Message;
  29. }
  30. finally
  31. {
  32. memoryStream.Close();
  33. cryptoStream.Close();
  34. }
  35. return encryptedText;
  36. }
  37.  
  38.  
  39. public static string DecryptString(string encryptedText, string secterKey)
  40. {
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. CryptoStream cryptoStream; MemoryStream memoryStream;
  56. helper__encrypt_decrypt_stream(out memoryStream, out cryptoStream, secterKey);
  57.  
  58.  
  59.  
  60.  
  61.  
  62. string plainText = String.Empty;
  63. try
  64. byte[] cipherBytes = Convert.FromBase64String(encryptedText);// Convert the encryptedText string into a byte array
  65. cryptoStream.Write(cipherBytes, 0, cipherBytes.Length); // Decrypt the input encryptedText string
  66. cryptoStream.FlushFinalBlock(); // Complete the decryption process
  67. byte[] plainBytes = memoryStream.ToArray(); // Convert the decrypted data from a MemoryStream to a byte array
  68.  
  69. plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length); // Convert the decrypted byte array to string
  70. }
  71. catch (Exception e)
  72. {
  73. return e.Message;
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. }
  86. finally
  87. {
  88.  
  89. memoryStream.Close();
  90. cryptoStream.Close();
  91. }
  92.  
  93. return plainText;
  94. }
  95.  
  96. public static void helper__encrypt_decrypt_stream(out MemoryStream memoryStream, out CryptoStream cryptoStream, string secterKey)
  97. {
  98. SHA256 mySHA256 = SHA256Managed.Create();
  99. byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(secterKey));
  100. byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
  101. // string symmetric encryption
  102. Aes encryptor = Aes.Create();
  103. encryptor.Mode = CipherMode.CBC;
  104. //encryptor.KeySize = 256; encryptor.BlockSize = 128; encryptor.Padding = PaddingMode.Zeros;
  105. encryptor.Key = key;
  106. encryptor.IV = iv;
  107.  
  108. memoryStream = new MemoryStream();
  109. cryptoStream = new CryptoStream(memoryStream, encryptor.CreateEncryptor(), CryptoStreamMode.Write); // write to memory stream
  110. }
  111. }
Add Comment
Please, Sign In to add comment