반응형
암호화 코드
using System.IO;
using System.Security.Cryptography;
public static class CryptoFunc
{
private static byte[] _key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
private static byte[] _iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
private static Aes _aes = null;
private static void InitAES()
{
if (_aes == null)
_aes = Aes.Create();
}
public static byte[] DecryptorBytes(byte[] bytes)
{
InitAES();
using (var decryptedStream = new MemoryStream())
{
using (var encryptedStream = new MemoryStream(bytes))
{
using (var decryptor = _aes.CreateDecryptor(_key, _iv))
{
using (var cryptoStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read))
{
int b;
while ((b = cryptoStream.ReadByte()) != -1)
decryptedStream.WriteByte((byte)b);
}
}
}
decryptedStream.Position = 0;
return decryptedStream.ToArray();
}
}
public static byte[] EncryptorBytes(byte[] bytes)
{
InitAES();
using (MemoryStream encryptedStream = new MemoryStream(bytes.Length))
{
using (var encryptor = _aes.CreateEncryptor(_key, _iv))
{
using (var cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write))
{
using (var originalStream = new MemoryStream(bytes))
{
int b;
while ((b = originalStream.ReadByte()) != -1)
cryptoStream.WriteByte((byte)b);
}
}
}
var encryptedBytes = encryptedStream.ToArray();
return encryptedBytes;
}
}
}
암호화 Integer
using System;
public struct CryptoInteger
{
private byte[] _bytes;
public CryptoInteger(int i)
{
_bytes = CryptoFunc.EncryptorBytes(BitConverter.GetBytes(i));
}
public static CryptoInteger operator +(CryptoInteger a) => a;
public static CryptoInteger operator -(CryptoInteger a) => new CryptoInteger(-a);
public static CryptoInteger operator +(CryptoInteger a, CryptoInteger b)
{
int ia = a;
int ib = b;
return new CryptoInteger(ia + ib);
}
public static CryptoInteger operator -(CryptoInteger a, CryptoInteger b)
{
int ia = a;
int ib = b;
return new CryptoInteger(ia - ib);
}
public static CryptoInteger operator *(CryptoInteger a, CryptoInteger b)
{
int ia = a;
int ib = b;
return new CryptoInteger(ia * ib);
}
public static CryptoInteger operator /(CryptoInteger a, CryptoInteger b)
{
int ia = a;
int ib = b;
return new CryptoInteger(ia / ib);
}
public static implicit operator int(CryptoInteger a) => BitConverter.ToInt32(CryptoFunc.DecryptorBytes(a._bytes), 0);
public static implicit operator float(CryptoInteger a) => BitConverter.ToSingle(CryptoFunc.DecryptorBytes(a._bytes), 0);
public static implicit operator long(CryptoInteger a) => BitConverter.ToInt64(CryptoFunc.DecryptorBytes(a._bytes), 0);
public static implicit operator CryptoInteger(int a) => new CryptoInteger(a);
public override string ToString() => $"{(int)this}";
}
테스트 코드
private void IntegerTester()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
int a = 0, b = 0, c = 0, d = 0;
int ci = 5;
for (int i = 0; i < 100000; i++)
{
a = ci + 2;
b = ci - 2;
c = ci * 2;
d = ci / 2;
}
watch.Stop();
Debug.Log($"INTEGER A:{a} B:{b} C:{c} D:{d} I:{ci.ToString()} TIME:{watch.ElapsedMilliseconds}");
}
private void CryptoIntegerTester()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
int a = 0, b = 0, c = 0, d = 0;
CryptoInteger ci = new CryptoInteger(5);
for (int i = 0; i < 100000; i++)
{
a = ci + 2;
b = ci - 2;
c = ci * 2;
d = ci / 2;
}
watch.Stop();
Debug.Log($"CRYPTO INTEGER A:{a} B:{b} C:{c} D:{d} I:{ci.ToString()} TIME:{watch.ElapsedMilliseconds}");
}
더보기
INTEGER A:7 B:3 C:10 D:2 I:5 TIME:0
CRYPTO INTEGER A:7 B:3 C:10 D:2 I:5 TIME:22793
반응형
'Works > C#' 카테고리의 다른 글
Covariance and Contravariance (0) | 2022.01.19 |
---|---|
[.NET] Windows Registry 등록/삭제 (0) | 2019.09.08 |
[.NET] Java Bridge (2) | 2019.03.22 |
For vs Foreach (0) | 2018.12.04 |
댓글