Skip to content

Instantly share code, notes, and snippets.

@NullArray
Forked from heiswayi/EncryptDecrypt.cs
Created January 22, 2019 20:37
Show Gist options
  • Save NullArray/7f4d73a73b63760354984dc2626b952e to your computer and use it in GitHub Desktop.
Save NullArray/7f4d73a73b63760354984dc2626b952e to your computer and use it in GitHub Desktop.
Encryption & Decryption method for C#
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);
}
@NullArray
Copy link
Author

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.

@NullArray
Copy link
Author

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