我使用数据对象形式的WCF Web服务通过Internet将数据从服务器发送到客户端。我创建了一个Class,它是Serializable,并使用这个类来发送我的数据。
以下是我班级的一个例子:
[Serializable]
public class DBOList
{
public string A{ get; set; }
public string B { get; set; }
}
我是否可以加密此对象中的数据,并将其作为流发送到客户端?
如果没有,最好的办法是什么?
加密代码:
DBOList NewLst = new DBOList();
NewLst.A = "Value 1";
NewLst.B = "Value 2";
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// Encryption
using (var fs = new MemoryStream())
{
var cryptoStream = new CryptoStream(fs, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
BinaryFormatter formatter = new BinaryFormatter();
// This is where you serialize the class
formatter.Serialize(cryptoStream, NewLst);
cryptoStream.FlushFinalBlock();
}
答案 0 :(得分:2)
最好使用SSL,这将增加您需要的所有安全性,同时避免大多数陷阱。
除此之外,您当然可以使用CryptoStream
。您只能加密字节,但您已经通过提及Serializable
表明您已理解这一点。
请注意,如果您想创建自己的安全流,则需要:
AesManaged
HMACSHA256
相当安全。如果这没有任何响铃,使用最新的TLS实现。
答案 1 :(得分:0)
这是AES加密和解密C#中的类对象的最佳方法。在这里,我解释有关AES密钥和AES IV的用法。并提供一个示例,该示例使用AES加密和C#中的类对象的解密,在文件流中写入和读取byte []。
public class Profile
{
[JsonPropertyName("name")]
[JsonProperty(PropertyName = "name")]
internal string Name { get; set; }
[JsonPropertyName("password")]
[JsonProperty(PropertyName = "password")]
internal string Password { get; set; }
[JsonPropertyName("profileData")]
[JsonProperty(PropertyName = "profileData")]
public byte[] ProfileData { get; set; }
}
AES密钥使用密钥进行对称算法。这是秘密密钥,是您保密的东西。任何知道您的密钥(或可以猜到它的密钥)的人都可以解密您用它加密的任何数据(或伪造您用它计算出的任何验证码,等等)。
AES IV 用作对称算法的初始化向量(IV)。从最广泛的意义上讲,初始化向量只是用于启动某些迭代过程的初始值。因此,您可以自己维护代码。
private readonly static byte[] Key = Convert.FromBase64String("AsISxq9OwdZag1163OJqwovXfSWG98m+sPjVwJecfe4=");
private readonly static byte[] IV = Convert.FromBase64String("Aq0UThtJhjbuyWXtmZs1rw==");
class Program
{
private readonly static byte[] Key = Convert.FromBase64String("AsISxq9OwdZag1163OJqwovXfSWG98m+sPjVwJecfe4=");
private readonly static byte[] IV = Convert.FromBase64String("Aq0UThtJhjbuyWXtmZs1rw==");
public static Profile Profile { get; set; }
static void Main(string[] args)
{
Profile = new Profile();
string fileName = "D:\\Profile.txt";
Profile.Name = "Ramesh";
Profile.Password = "Password";
Console.WriteLine("Enter your option:");
Console.WriteLine("1. Encryption");
Console.WriteLine("2. Decryption");
string option = Console.ReadLine();
if (option == "1")
{
FileStream fsWrite = new FileStream(fileName, FileMode.Create, FileAccess.Write);
string serializeProfile = Newtonsoft.Json.JsonConvert.SerializeObject(Profile);
Profile.ProfileData = EncryptStringToBytes(serializeProfile);
fsWrite.Write(Profile.ProfileData, 0, Profile.ProfileData.Length);
fsWrite.Close();
}
else
{
FileStream fsRead = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fsRead);
long numBytes = new FileInfo(fileName).Length;
string decryptedText = DecryptStringFromBytes(br.ReadBytes((int)numBytes));
Profile DeserializeProfile = Newtonsoft.Json.JsonConvert.DeserializeObject<Profile>(decryptedText);
Console.WriteLine("Name :" + DeserializeProfile.Name);
Console.WriteLine("Password :" + DeserializeProfile.Password);
Console.ReadKey();
fsRead.Close();
}
}
private static byte[] EncryptStringToBytes(string profileText)
{
byte[] encryptedAuditTrail;
using (Aes newAes = Aes.Create())
{
newAes.Key = Key;
newAes.IV = IV;
ICryptoTransform encryptor = newAes.CreateEncryptor(Key, IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(profileText);
}
encryptedAuditTrail = msEncrypt.ToArray();
}
}
}
return encryptedAuditTrail;
}
private static string DecryptStringFromBytes(byte[] profileText)
{
string decryptText;
using (Aes newAes = Aes.Create())
{
newAes.Key = Key;
newAes.IV = IV;
ICryptoTransform decryptor = newAes.CreateDecryptor(Key, IV);
using (MemoryStream msDecrypt = new MemoryStream(profileText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
decryptText = srDecrypt.ReadToEnd();
}
}
}
}
return decryptText;
}
}