private bool ValidateAsServer() { byte[] Data = new byte[16]; using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider()) provider.GetBytes(Data); string q = string.Join("", Data); MemoryStream Output; MemoryStream Input = new MemoryStream(Encoding.UTF8.GetBytes(q)); string SaltString = CryptographicProvider.GetCryptographicallySecureString(32); byte[] SaltBytes = Encoding.UTF8.GetBytes(SaltString); byte[] BKey = new byte[32]; byte[] IV = new byte[16]; using (RNGCryptoServiceProvider Provider = new RNGCryptoServiceProvider()) { Provider.GetBytes(IV); } string IVStr = Convert.ToBase64String(IV); using (SHA256CryptoServiceProvider SHA = new SHA256CryptoServiceProvider()) { byte[] tmp = SHA.ComputeHash(KeyBytes.Concat(SaltBytes).ToArray()); Array.Copy(tmp, 0, BKey, 0, 16); Array.Copy(tmp, 0, BKey, 15, 16); } using (AesCryptoServiceProvider AES = new AesCryptoServiceProvider()) { AES.Mode = CipherMode.ECB; AES.KeySize = 128; using (ICryptoTransform Encryptor = AES.CreateEncryptor(BKey, IV)) { Output = new MemoryStream(); CryptoStream Crypto = new CryptoStream(Output, Encryptor, CryptoStreamMode.Write); Input.Position = 0; Input.CopyTo(Crypto); Crypto.FlushFinalBlock(); } } string MyToken = Convert.ToBase64String(Output.ToArray()); MutualKeyProtocalValidationRequest MSG = new MutualKeyProtocalValidationRequest() { IV = IVStr, Salt = SaltString, RawToken = q }; MutualKeyProtocalValidationResponse resp = Channels["$SnooperSec.MutualKeyProtocal.SignToken"].Query <MutualKeyProtocalValidationResponse>(MSG); return(resp.Token == MyToken); }
public override bool EncryptStream(MemoryStream Input, out MemoryStream Output, ref Dictionary <string, string> Headers) { if (Headers.ContainsKey("$Channel") && RawChannels.Contains(Headers["$Channel"])) { return(base.EncryptStream(Input, out Output, ref Headers)); } try { string SaltString = CryptographicProvider.GetCryptographicallySecureString(32); byte[] SaltBytes = Encoding.UTF8.GetBytes(SaltString); byte[] BKey = new byte[32]; byte[] IV = new byte[16]; using (RNGCryptoServiceProvider Provider = new RNGCryptoServiceProvider()) { Provider.GetBytes(IV); } string IVStr = Convert.ToBase64String(IV); using (SHA256CryptoServiceProvider SHA = new SHA256CryptoServiceProvider()) { byte[] tmp = SHA.ComputeHash(KeyBytes.Concat(SaltBytes).ToArray()); Array.Copy(tmp, 0, BKey, 0, 16); Array.Copy(tmp, 0, BKey, 15, 16); } using (AesCryptoServiceProvider AES = new AesCryptoServiceProvider()) { AES.Mode = CipherMode.ECB; AES.KeySize = 128; using (ICryptoTransform Encryptor = AES.CreateEncryptor(BKey, IV)) { Output = new MemoryStream(); CryptoStream Crypto = new CryptoStream(Output, Encryptor, CryptoStreamMode.Write); Input.Position = 0; Input.CopyTo(Crypto); Crypto.FlushFinalBlock(); } } Headers.Add("$ENCMODE", GetType().Name); Headers.Add("$ENCSALT", SaltString); Headers.Add("$ENCIV", IVStr); Output.Position = 0; return(true); } catch (CryptographicException) { Output = null; return(false); } }