-
-
Save NullArray/7f4d73a73b63760354984dc2626b952e to your computer and use it in GitHub Desktop.
Encryption & Decryption method for C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string Encrypt(string data, string key) | |
{ | |
RijndaelManaged rijndaelCipher = new RijndaelManaged(); | |
rijndaelCipher.Mode = CipherMode.CBC; | |
rijndaelCipher.Padding = PaddingMode.PKCS7; | |
rijndaelCipher.KeySize = 0x80; | |
rijndaelCipher.BlockSize = 0x80; | |
byte[] pwdBytes = Encoding.UTF8.GetBytes(key); | |
byte[] keyBytes = new byte[0x10]; | |
int len = pwdBytes.Length; | |
if (len > keyBytes.Length) | |
{ | |
len = keyBytes.Length; | |
} | |
Array.Copy(pwdBytes, keyBytes, len); | |
rijndaelCipher.Key = keyBytes; | |
rijndaelCipher.IV = keyBytes; | |
ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); | |
byte[] plainText = Encoding.UTF8.GetBytes(data); | |
return Convert.ToBase64String | |
(transform.TransformFinalBlock(plainText, 0, plainText.Length)); | |
} | |
public static string Decrypt(string data, string key) | |
{ | |
RijndaelManaged rijndaelCipher = new RijndaelManaged(); | |
rijndaelCipher.Mode = CipherMode.CBC; | |
rijndaelCipher.Padding = PaddingMode.PKCS7; | |
rijndaelCipher.KeySize = 0x80; | |
rijndaelCipher.BlockSize = 0x80; | |
byte[] encryptedData = Convert.FromBase64String(data); | |
byte[] pwdBytes = Encoding.UTF8.GetBytes(key); | |
byte[] keyBytes = new byte[0x10]; | |
int len = pwdBytes.Length; | |
if (len > keyBytes.Length) | |
{ | |
len = keyBytes.Length; | |
} | |
Array.Copy(pwdBytes, keyBytes, len); | |
rijndaelCipher.Key = keyBytes; | |
rijndaelCipher.IV = keyBytes; | |
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock | |
(encryptedData, 0, encryptedData.Length); | |
return Encoding.UTF8.GetString(plainText); | |
} |
RijndaelManaged
byte has to be using System
System.byte or using
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interestingly PowerShell has the ability to run C++ and C# codes, as far as i understand it through special, scriptlets/cmdlets. This allows one to basically set up a windows service with PowerShell and write the service logic in C#/C++
At least that's to the best of my understanding. I am sure about C++ but i might be mistaken on C#, been experimenting with it a bit though.