Created
March 15, 2019 09:01
-
-
Save PurwantoGZ/2369f68fa7ad8d9aad458887549f9a16 to your computer and use it in GitHub Desktop.
Encryp Decrypt C#
This file contains 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
//Rextester.Program.Main is the entry point for your code. Don't change it. | |
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 | |
using System; | |
using System.IO; | |
using System.Text; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using System.Security.Cryptography; | |
namespace Rextester | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
//Your code goes here | |
Console.WriteLine(Encrypt.EncryptString("SUTEST0000001","purwanto")); | |
Console.WriteLine(Encrypt.DecryptString("dMLDTbV0jMGivLUqcoxGtg==","purwanto")); | |
Console.WriteLine("Hello, world!"); | |
} | |
} | |
public static class Encrypt | |
{ | |
// This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be | |
// 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array. | |
private const string initVector = "pemgail9uzpgzl88"; | |
// This constant is used to determine the keysize of the encryption algorithm | |
private const int keysize = 256; | |
//Encrypt | |
public static string EncryptString(string plainText, string passPhrase) | |
{ | |
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector); | |
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); | |
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null); | |
byte[] keyBytes = password.GetBytes(keysize / 8); | |
RijndaelManaged symmetricKey = new RijndaelManaged(); | |
symmetricKey.Mode = CipherMode.CBC; | |
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); | |
MemoryStream memoryStream = new MemoryStream(); | |
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); | |
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); | |
cryptoStream.FlushFinalBlock(); | |
byte[] cipherTextBytes = memoryStream.ToArray(); | |
memoryStream.Close(); | |
cryptoStream.Close(); | |
return Convert.ToBase64String(cipherTextBytes); | |
} | |
//Decrypt | |
public static string DecryptString(string cipherText, string passPhrase) | |
{ | |
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector); | |
byte[] cipherTextBytes = Convert.FromBase64String(cipherText); | |
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null); | |
byte[] keyBytes = password.GetBytes(keysize / 8); | |
RijndaelManaged symmetricKey = new RijndaelManaged(); | |
symmetricKey.Mode = CipherMode.CBC; | |
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes); | |
MemoryStream memoryStream = new MemoryStream(cipherTextBytes); | |
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); | |
byte[] plainTextBytes = new byte[cipherTextBytes.Length]; | |
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); | |
memoryStream.Close(); | |
cryptoStream.Close(); | |
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment