/// <summary> /// Decrypt method /// </summary> public override byte[] Decrypt(byte[] encryptedBytes, string username) { try { if (encryptedBytes == null) { throw new Exception("Invalid decryption context !"); } byte[] decryptedBytes = XORUtilities.XOREncryptOrDecrypt(encryptedBytes, XORUtilities.XORKey); int size = GetSizeFromMode(_mode); byte[] userbuff = new byte[decryptedBytes.Length - size]; Buffer.BlockCopy(decryptedBytes, size, userbuff, 0, decryptedBytes.Length - size); this.CheckSum = userbuff; byte[] decryptedkey = new byte[size]; Buffer.BlockCopy(decryptedBytes, 0, decryptedkey, 0, size); return(decryptedkey); } catch (System.Security.Cryptography.CryptographicException ce) { Log.WriteEntry(string.Format("Crytograpphic Error for user {0} \r {1}", ce.Message, username), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } catch (Exception ex) { Log.WriteEntry(string.Format("Crytograpphic Error for user {0} \r {1}", ex.Message, username), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// NewEncryptedKey method implementation /// </summary> public override byte[] NewEncryptedKey(string username) { try { byte[] plainBytes = GenerateKey(username); byte[] encryptedBytes = null; using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) { aesAlg.BlockSize = 128; aesAlg.KeySize = 256; aesAlg.Key = AESKey; aesAlg.IV = AESIV; aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = PaddingMode.PKCS7; using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)) { encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); } } return(XORUtilities.XOREncryptOrDecrypt(encryptedBytes, this.XORSecret)); } catch (CryptographicException ce) { Log.WriteEntry(string.Format("(AES256Encryption Encrypt) : Crytographic error for user {1} \r {0} \r {2}", ce.Message, username, ce.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } catch (Exception ex) { Log.WriteEntry(string.Format("(AES256Encryption Encrypt) : Encryption error for user {1} \r {0} \r {2}", ex.Message, username, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// Encrypt method /// </summary> public override byte[] Encrypt(string username) { try { if (Certificate == null) { throw new Exception("Invalid encryption certificate !"); } byte[] plainBytes = GenerateKey(username); byte[] encryptedBytes = null; var key = Certificate.GetRSAPublicKey(); if (key == null) { throw new CryptographicException("Invalid public Key !"); } if (key is RSACng) { encryptedBytes = ((RSACng)key).Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256); } else { encryptedBytes = ((RSACryptoServiceProvider)key).Encrypt(plainBytes, true); } return(XORUtilities.XOREncryptOrDecrypt(encryptedBytes, this.XORSecret)); } catch (Exception ex) { Log.WriteEntry(string.Format("Crytograpphic Error for user {0} \r {1}", ex.Message, username), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// ReadMessage method override /// </summary> public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType) { byte[] unencryptedBytes = new byte[stream.Length]; stream.Read(unencryptedBytes, 0, (int)stream.Length); byte[] cryptedBytes = XORUtilities.XOREncryptOrDecrypt(unencryptedBytes, XORUtilities.UtilitiesKey); MemoryStream stm = new MemoryStream(cryptedBytes); return(innerEncoder.ReadMessage(stm, maxSizeOfHeaders)); }
/// <summary> /// WriteMessage method override /// </summary> public override void WriteMessage(Message message, Stream stream) { byte[] cryptedBytes = new byte[stream.Length]; stream.Read(cryptedBytes, 0, (int)stream.Length); byte[] unencryptedBytes = XORUtilities.XOREncryptOrDecrypt(cryptedBytes, XORUtilities.UtilitiesKey); MemoryStream stm = new MemoryStream(unencryptedBytes); innerEncoder.WriteMessage(message, stm); stream.Flush(); }
/// <summary> /// ReadConfigurationForCache method implementation /// </summary> private byte[] ReadConfigurationForCache() { MFAConfig config = CFGUtilities.ReadConfiguration(); XmlConfigSerializer xmlserializer = new XmlConfigSerializer(typeof(MFAConfig)); MemoryStream stm = new MemoryStream(); using (StreamReader reader = new StreamReader(stm)) { xmlserializer.Serialize(stm, config); stm.Position = 0; return(XORUtilities.XOREncryptOrDecrypt(stm.ToArray(), XORUtilities.XORKey)); } }
/// <summary> /// Encrypt method /// </summary> public override byte[] Encrypt(string username) { try { if (string.IsNullOrEmpty(username)) { throw new Exception("Invalid encryption context !"); } byte[] plainBytes = GenerateKey(username); return(XORUtilities.XOREncryptOrDecrypt(plainBytes, this.XORSecret)); } catch (Exception ex) { Log.WriteEntry(string.Format("Crytograpphic Error for user {0} \r {1}", ex.Message, username), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// Decrypt method /// </summary> public override byte[] Decrypt(byte[] encryptedBytes, string username) { try { if (Certificate == null) { throw new Exception("Invalid decryption certificate !"); } byte[] decryptedBytes = XORUtilities.XOREncryptOrDecrypt(encryptedBytes, this.XORSecret); byte[] fulldecryptedBytes = null; var key = Certificate.GetRSAPrivateKey(); if (key == null) { throw new CryptographicException("Invalid private Key !"); } if (key is RSACng) { fulldecryptedBytes = ((RSACng)key).Decrypt(decryptedBytes, RSAEncryptionPadding.OaepSHA256); } else { fulldecryptedBytes = ((RSACryptoServiceProvider)key).Decrypt(decryptedBytes, true); } byte[] userbuff = new byte[fulldecryptedBytes.Length - 128]; Buffer.BlockCopy(fulldecryptedBytes, 128, userbuff, 0, fulldecryptedBytes.Length - 128); this.CheckSum = userbuff; byte[] decryptedkey = new byte[128]; Buffer.BlockCopy(fulldecryptedBytes, 0, decryptedkey, 0, 128); return(decryptedkey); } catch (System.Security.Cryptography.CryptographicException ce) { Log.WriteEntry(string.Format("Crytograpphic Error for user {0} \r {1}", ce.Message, username), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } catch (Exception ex) { Log.WriteEntry(string.Format("Crytograpphic Error for user {0} \r {1}", ex.Message, username), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// EncryptBuffer method implementation (compress) /// </summary> private ArraySegment <byte> EncryptBuffer(ArraySegment <byte> buffer, BufferManager bufferManager, int messageOffset) { byte[] unencryptedBytes = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, unencryptedBytes, 0, unencryptedBytes.Length); byte[] cryptedBytes = XORUtilities.XOREncryptOrDecrypt(unencryptedBytes, XORUtilities.UtilitiesKey); int totalLength = messageOffset + cryptedBytes.Length; byte[] bufferManagerBuffer = bufferManager.TakeBuffer(totalLength); Array.Clear(bufferManagerBuffer, 0, bufferManagerBuffer.Length); Array.Copy(cryptedBytes, 0, bufferManagerBuffer, messageOffset, cryptedBytes.Length); bufferManager.ReturnBuffer(buffer.Array); ArraySegment <byte> byteArray = new ArraySegment <byte>(bufferManagerBuffer, messageOffset, cryptedBytes.Length); return(byteArray); }
/// <summary> /// GetDecryptedKey method implementation /// </summary> public override byte[] GetDecryptedKey(byte[] encryptedBytes, string username) { try { if (encryptedBytes == null) { throw new Exception("Invalid decryption context !"); } return(Caesar(XORUtilities.XOREncryptOrDecrypt(encryptedBytes, this.XORSecret), -3)); } catch (CryptographicException ce) { Log.WriteEntry(string.Format("(CesarEncryption2 Decrypt) : Crytographic error for user {1} \r {0} \r {2}", ce.Message, username, ce.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } catch (Exception ex) { Log.WriteEntry(string.Format("(CesarEncryption2 Decrypt) : Decryption error for user {1} \r {0} \r {2}", ex.Message, username, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// NewEncryptedKey method implementation /// </summary> public override byte[] NewEncryptedKey(string username) { try { if (string.IsNullOrEmpty(username)) { throw new Exception("Invalid encryption context !"); } byte[] plainBytes = GenerateKey(); return(Caesar(XORUtilities.XOREncryptOrDecrypt(plainBytes, this.XORSecret), +3)); } catch (CryptographicException ce) { Log.WriteEntry(string.Format("(CesarEncryption2 Encrypt) : Crytographic error for user {1} \r {0} \r {2}", ce.Message, username, ce.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } catch (Exception ex) { Log.WriteEntry(string.Format("(Encryption2 Encrypt) : Encryption error for user {1} \r {0} \r {2}", ex.Message, username, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// GetDecryptedKey method implementation /// </summary> public override byte[] GetDecryptedKey(byte[] encryptedBytes, string username) { try { byte[] fulldecryptedBytes = null; byte[] decryptedBytes = XORUtilities.XOREncryptOrDecrypt(encryptedBytes, this.XORSecret); using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) { aesAlg.BlockSize = 128; aesAlg.KeySize = 256; aesAlg.Key = AESKey; aesAlg.IV = AESIV; aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = PaddingMode.PKCS7; using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)) { fulldecryptedBytes = decryptor.TransformFinalBlock(decryptedBytes, 0, decryptedBytes.Length); } } byte[] userbuff = new byte[fulldecryptedBytes.Length - datalen]; Buffer.BlockCopy(fulldecryptedBytes, datalen, userbuff, 0, fulldecryptedBytes.Length - datalen); this.CheckSum = userbuff; byte[] decryptedkey = new byte[datalen]; Buffer.BlockCopy(fulldecryptedBytes, 0, decryptedkey, 0, datalen); return(decryptedkey); } catch (CryptographicException ce) { Log.WriteEntry(string.Format("(AES256Encryption Decrypt) : Crytographic error for user {1} \r {0} \r {2}", ce.Message, username, ce.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } catch (Exception ex) { Log.WriteEntry(string.Format("(AES256Encryption Decrypt) : Decryption error for user {1} \r {0} \r {2}", ex.Message, username, ex.StackTrace), System.Diagnostics.EventLogEntryType.Error, 0000); return(null); } }
/// <summary> /// PipeClientOnEncryptBytes method implementation /// </summary> private byte[] PipeClientOnEncryptBytes(byte[] clearvalue) { return(XORUtilities.XOREncryptOrDecrypt(clearvalue, Proofkey)); }
/// <summary> /// PipeClientOnDecrypt method implementation /// </summary> private string PipeClientOnDecrypt(string cryptedvalue) { byte[] byt = XORUtilities.XOREncryptOrDecrypt(System.Convert.FromBase64String(cryptedvalue), Proofkey); return(System.Text.Encoding.UTF8.GetString(byt)); }
/// <summary> /// PipeClientOnEncrypt method implementation /// </summary> private string PipeClientOnEncrypt(string clearvalue) { byte[] byt = XORUtilities.XOREncryptOrDecrypt(System.Text.Encoding.UTF8.GetBytes(clearvalue), Proofkey); return(System.Convert.ToBase64String(byt)); }
/// <summary> /// PipeServerOnDecryptBytes method implementation /// </summary> private byte[] PipeServerOnDecryptBytes(byte[] cryptedvalue) { return(XORUtilities.XOREncryptOrDecrypt(cryptedvalue, Proofkey)); }