Processing data. Please wait a moment...
포인트: 100 / 트랜잭션(건)
using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.OpenSsl; using Org.BouncyCastle.Security; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net.Http; using System.Security.Cryptography; using System.Text; namespace UnitTest { [TestClass] public class TestCase { string apiHost = "https://api24.tilko.net"; string apiKey = "__API_KEY__"; // AES 암호화 함수 public string aesEncrypt(byte[] key, byte[] iv, byte[] plainText) { byte[] ret = new byte[0]; using (RijndaelManaged aes = new RijndaelManaged()) { aes.Key = key; aes.IV = iv; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; using (ICryptoTransform enc = aes.CreateEncryptor(aes.Key, aes.IV)) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write)) { cs.Write(plainText, 0, plainText.Length); cs.FlushFinalBlock(); ret = ms.ToArray(); } } } aes.Clear(); } return Convert.ToBase64String(ret); } // AES 암호화 함수 public string aesEncrypt(byte[] key, byte[] iv, string plainText) { byte[] ret = new byte[0]; using (RijndaelManaged aes = new RijndaelManaged()) { aes.Key = key; aes.IV = iv; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; using (ICryptoTransform enc = aes.CreateEncryptor(aes.Key, aes.IV)) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write)) { cs.Write(Encoding.UTF8.GetBytes(plainText), 0, Encoding.UTF8.GetBytes(plainText).Length); cs.FlushFinalBlock(); ret = ms.ToArray(); } } } aes.Clear(); } return Convert.ToBase64String(ret); } // RSA 암호화 함수 public string rsaEncrypt(string publicKey, byte[] aesKey) { string encryptedData = ""; using (RSACryptoServiceProvider rsaCSP = importPublicKey(publicKey)) { byte[] byteEncryptedData = rsaCSP.Encrypt(aesKey, false); encryptedData = Convert.ToBase64String(byteEncryptedData); rsaCSP.Dispose(); } return encryptedData; } public static RSACryptoServiceProvider importPublicKey(string pem) { string PUBLIC_HEADER = "-----BEGIN PUBLIC KEY-----"; string PUBLIC_FOOTER = "-----END PUBLIC KEY-----"; if (!pem.Contains(PUBLIC_HEADER)) { pem = PUBLIC_HEADER + Environment.NewLine + pem + Environment.NewLine + PUBLIC_FOOTER; } PemReader pr = new PemReader(new StringReader(pem)); AsymmetricKeyParameter publicKey = (AsymmetricKeyParameter)pr.ReadObject(); RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaKeyParameters)publicKey); RSACryptoServiceProvider csp = new RSACryptoServiceProvider(); csp.ImportParameters(rsaParams); return csp; } // RSA 공개키(Public Key) 조회 함수 public string getPublicKey() { string rsaPublicKey = ""; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); // 틸코 인증 서버에 RSA 공개키 요청 string url = string.Format("{0}/api/Auth/GetPublicKey?APIkey={1}", apiHost, apiKey); using (var response = httpClient.GetAsync(url).Result) { var resContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); JObject resJson = JObject.Parse(resContent); rsaPublicKey = (string)resJson["PublicKey"].ToString(); } } return rsaPublicKey; } public class Auth { public string UserId { get; set; } public string UserPassword { get; set; } } [TestMethod] public void main() { // RSA Public Key 조회 string rsaPublicKey = getPublicKey(); Debug.WriteLine("rsaPublicKey: " + rsaPublicKey); // AES Secret Key 및 IV 생성 byte[] aesKey = new byte[16]; new Random().NextBytes(aesKey); byte[] aesIv = new byte[16] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // AES Key를 RSA Public Key로 암호화 string aesCipherKey = rsaEncrypt(rsaPublicKey, aesKey); Debug.WriteLine("aesCipherKey: " + aesCipherKey); // API URL 설정 string url = apiHost + "/api/v2.0/KcomwelGeneral/SelectBosuJeopsuList_GT"; // API 요청 파라미터 설정 var paramObj = new { Auth = new { CertFile = aesEncrypt(aesKey, aesIv, File.ReadAllBytes(@"C:\Users\__USER_NAME__\AppData\LocalLow\NPKI\yessign\USER\__CERT_NAME__\signCert.der")), KeyFile = aesEncrypt(aesKey, aesIv, File.ReadAllBytes(@"C:\Users\__USER_NAME__\AppData\LocalLow\NPKI\yessign\USER\__CERT_NAME__\signPri.key")), CertPassword = aesEncrypt(aesKey, aesIv, "__VALUE__"), AgentId = aesEncrypt(aesKey, aesIv, "__VALUE__"), AgentPassword = aesEncrypt(aesKey, aesIv, "__VALUE__"), DeptUserId = aesEncrypt(aesKey, aesIv, "__VALUE__"), DeptUserPassword = aesEncrypt(aesKey, aesIv, "__VALUE__"), UserName = aesEncrypt(aesKey, aesIv, "__VALUE__"), IdentityNumber = aesEncrypt(aesKey, aesIv, "__VALUE__") }, BusinessNumber = "", UserGroupFlag = "", IndividualFlag = "", BoheomYear = "", GwanriNo = "" }; string bodies = JsonConvert.SerializeObject(paramObj); // API 호출 using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Add("API-KEY", apiKey); httpClient.DefaultRequestHeaders.Add("ENC-KEY", aesCipherKey); // 틸코 데이터 서버에 데이터 요청 var reqContent = new StringContent(bodies, Encoding.UTF8, "application/json"); using (var response = httpClient.PostAsync(url, reqContent).Result) { var resContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); Debug.WriteLine("resContent: " + resContent); // 바이너리 파일 저장(해당되는 경우에만) /* JObject resJson = JObject.Parse(resContent); File.WriteAllBytes("D:\\result.bin", Convert.FromBase64String((string)resJson["Result"]["BinaryData"])); */ } } } } }