Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Created May 25, 2020 02:37
Show Gist options
  • Save DongguemYoo/2546a2067dc065297765c5b7d55b05cf to your computer and use it in GitHub Desktop.
Save DongguemYoo/2546a2067dc065297765c5b7d55b05cf to your computer and use it in GitHub Desktop.
c# 데이터 암호화 (Class DataEncryptDecrypt.cs)
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
public class DataEncryptDecrypt
{
public static string encryData(string filepath)
{
///RijndaelManaged supports 128, 192 and 256 bit keys.
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("AAECAwQFBgcICQoLDA0ODw==");
//byte[] toEmcryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
//byte[] toEmcryptArray = Convert.FromBase64String(toEncrypt);
byte[] toEmcryptArray = File.ReadAllBytes(filepath);
//byte[] toEmcryptArray = Convert.FromBase64String(toEncrypt.Replace(" ", "+"));
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
//rDel.Padding = PaddingMode.PKCS7;
//rDel.Padding = PaddingMode.None;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEmcryptArray, 0, toEmcryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(string toDecrypt)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes("AAECAwQFBgcICQoLDA0ODw==");
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
//rDel.Padding = PaddingMode.PKCS7;
rDel.Padding = PaddingMode.None;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
//string rs = System.Convert.ToBase64String(resultArray);
//return Convert.ToBase64String(resultArray);
//return Convert.ToBase64String(resultArray,0,resultArray.Length);
string rs = UTF8Encoding.UTF8.GetString(resultArray);
//string rs = Encoding.ASCII.GetString(resultArray);
//s = s.Replace("\x01", @"&#01"); ;
return rs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment