간단한 클라이언트/서버 설정이 있습니다. 클라이언트와 서버는 모두 개인 키를 가지고 있습니다.Fast Lightweight .NET 클라이언트 암호화 -> 서버 암호 해독
.NET
ClientData-> ClientEncrypt와 KEY-> 전송에 서버 -> ServerDecrypt와 KEY-> ClientData
사람이 어떤 빠르고 간단 라이브러리를 제안 할 수 읽기의 방법으로 저를 제공하는 일 위로?
감사
간단한 클라이언트/서버 설정이 있습니다. 클라이언트와 서버는 모두 개인 키를 가지고 있습니다.Fast Lightweight .NET 클라이언트 암호화 -> 서버 암호 해독
.NET
ClientData-> ClientEncrypt와 KEY-> 전송에 서버 -> ServerDecrypt와 KEY-> ClientData
사람이 어떤 빠르고 간단 라이브러리를 제안 할 수 읽기의 방법으로 저를 제공하는 일 위로?
감사
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace ServiceConsole
{
public class Obfuscation
{
public static byte[] Encrypt(string data)
{
return Encrypt(data, SecurityCredentials.KeyString, SecurityCredentials.IvString);
}
public static byte[] Encrypt(string data, string key, string iv)
{
return Encrypt(data, key, iv, SecurityCredentials.PaddingString);
}
public static byte[] Encrypt(string data, string key, string iv, char paddingCharacter)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.PadLeft(32, paddingCharacter).Substring(0, 32));
byte[] ivBytes = Encoding.UTF8.GetBytes(iv.PadLeft(32, paddingCharacter).Substring(0, 32));
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.BlockSize = 256;
rijndaelManaged.KeySize = 256;
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform iCryptoTransform = rijndaelManaged.CreateEncryptor(keyBytes, ivBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Write);
StreamWriter streamWriter = new StreamWriter(cryptoStream);
streamWriter.Write(data);
streamWriter.Flush();
cryptoStream.FlushFinalBlock();
byte[] returnBytes = memoryStream.ToArray();
/// Disposal
streamWriter.Dispose();
cryptoStream.Dispose();
iCryptoTransform.Dispose();
memoryStream.Dispose();
rijndaelManaged.Clear();
///
return returnBytes;
}
public static string Decrypt(byte[] data)
{
return Decrypt(data, SecurityCredentials.KeyString, SecurityCredentials.IvString);
}
public static string Decrypt(byte[] data, string key, string iv)
{
return Decrypt(data, key, iv, SecurityCredentials.PaddingString);
}
public static string Decrypt(byte[] data, string key, string iv, char paddingCharacter)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.PadLeft(32, paddingCharacter).Substring(0, 32));
byte[] ivBytes = Encoding.UTF8.GetBytes(iv.PadLeft(32, paddingCharacter).Substring(0, 32));
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.BlockSize = 256;
rijndaelManaged.KeySize = 256;
MemoryStream memoryStream = new MemoryStream(data);
ICryptoTransform iCryptoTransform = rijndaelManaged.CreateDecryptor(keyBytes, ivBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream);
string returnString = streamReader.ReadLine();
/// Disposal
streamReader.Dispose();
cryptoStream.Dispose();
iCryptoTransform.Dispose();
memoryStream.Dispose();
rijndaelManaged.Clear();
///
return returnString;
}
}
}
(또는 그것을 주변에 구글!)
깔끔하고 깔끔하고 정적입니다. :) – divinci
여기 an example입니다 :
private static string CreateEncryptedString(string myString, string hexiv, string key)
{
RijndaelManaged alg = new RijndaelManaged();
alg.Padding = PaddingMode.Zeros;
alg.Mode = CipherMode.CBC;
alg.BlockSize = 16 * 8;
alg.Key = ASCIIEncoding.UTF8.GetBytes(key);
alg.IV = StringToByteArray(hexiv);
ICryptoTransform encryptor = alg.CreateEncryptor(alg.Key, alg.IV);
MemoryStream msStream = new MemoryStream();
CryptoStream mCSWriter = new CryptoStream(msStream, encryptor, CryptoStreamMode.Write);
StreamWriter mSWriter = new StreamWriter(mCSWriter);
mSWriter.Write(myString);
mSWriter.Flush();
mCSWriter.FlushFinalBlock();
var EncryptedByte = new byte[msStream.Length];
msStream.Position = 0;
msStream.Read(EncryptedByte, 0, (int)msStream.Length);
return ByteArrayToHexString(EncryptedByte);
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars/2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i/2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
public static string ByteArrayToHexString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
쉽게 해독 알고리즘으로 나올 수 있고, 예
Re : 솔루션 검색 중 ... 예,하지만 나쁜 것을 복사하지 말아주십시오.) (http://www.codinghorror.com/blog/archives/001268.html) – StingyJack
Winforms 또는 웹 응용 프로그램? – StingyJack