2018년 1월 18일 목요일
C# - MSSQL CLR (AES256 암호화)
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
public partial class CryptoHelper
{
/// <summary>
/// AesManaged 생성
/// </summary>
/// <returns></returns>
private static AesManaged GetAesManaged()
{
// 암호화에 사용할 32bytes 의 키 값.
const string DEFAULT_KEY_ASE256 = "32자리 KEY 입력";
return new AesManaged()
{
KeySize = 256,
BlockSize = 128,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
Key = System.Text.Encoding.UTF8.GetBytes(DEFAULT_KEY_ASE256),
IV = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
}
/// <summary>
/// 암호화 AES256
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static string EncryptAES256(string inputText)
{
if (string.IsNullOrWhiteSpace(inputText))
return null;
byte[] encrypted;
AesManaged aesAlg = GetAesManaged();
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(inputText);
}
encrypted = msEncrypt.ToArray();
}
}
return Convert.ToBase64String(encrypted);
}
/// <summary>
/// 복호화 AES256
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static string DecryptAES256(string inputText)
{
if (string.IsNullOrWhiteSpace(inputText))
return null;
string plaintext = null;
byte[] cipherText = Convert.FromBase64String(inputText);
AesManaged aesAlg = GetAesManaged();
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
return plaintext;
}
/// <summary>
/// 단방향 암호화 (SHA256)
/// </summary>
/// <param name="Data"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static string SHA256Hash(string inputText)
{
StringBuilder sb = new StringBuilder();
SHA256 sha = new SHA256Managed();
byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(inputText));
foreach (byte b in hash)
sb.AppendFormat("{0:x2}", b);
return sb.ToString();
}
}
/*
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO
*/
/*
-- DROP ASSEMBLY myEncrypt
CREATE ASSEMBLY myEncrypt FROM 'D:\CryptoProject\CryptoSQLProject.dll'
WITH PERMISSION_SET = SAFE
*/
/*
-- DROP FUNCTION dbo.fn_Encrypt
-- DROP FUNCTION dbo.fn_Decrypt
CREATE FUNCTION fn_Encrypt(@value NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
EXTERNAL NAME myEncrypt.CryptoHelper.EncryptAES256
GO
CREATE FUNCTION fn_Decrypt(@value NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
EXTERNAL NAME myEncrypt.CryptoHelper.DecryptAES256
GO
*/
/*
SELECT *
FROM sys.assemblies
SELECT dbo.fn_Encrypt('123-12-123456')
SELECT dbo.fn_Decrypt('Jsh3QXSLqiv2U5q1wF+gEw==')
*/
피드 구독하기:
댓글 (Atom)
Flutter #0
[Flutter 교육] Dart vs JavaScript 타입 시스템 비교 1. 기본 타입 차이 숫자 타입 // Dart int integerNumber = 42; // 정수 double floatingPoint = 3.14; // 부...
-
11 SQL Client for Productive Database Administration & Development Working as a web developer or database administrator, often n...
-
JsonUtility 유니티 5.3 이상부터 JsonUtility를 지원한다. 외부 JSON 라이브러리가 필요 없다. < JsonUtility 멤버 함수 > FromJson : JSON을 오브젝트로 변환한다. 오브젝트를 생성...
댓글 없음:
댓글 쓰기