//----------------------------------------------------------------------- // // DES belongs to Copyright (c) Origtek. All rights reserved. // //----------------------------------------------------------------------- namespace OT.COM.Encryption { using System; using System.IO; using System.Security.Cryptography; /// /// DES Encrypt Decrypt /// public partial class DES : AEncryption { /// /// Main encrypt function /// /// /// Plain text to be encrypt /// /// /// Encrypt key /// /// /// Encrypt initial vector /// /// /// Encrypt result byte /// public override byte[] EncryptStringToByte(string plainText, byte[] byaKey, byte[] byaIV) { //// Check arguments. if (plainText == null || plainText.Length <= 0) { throw new ArgumentNullException("plainText"); } if (byaKey == null || byaKey.Length != 8) { throw new ArgumentException("Key == null || Key.Length != 8"); } if (byaIV == null || byaIV.Length != 8) { throw new ArgumentException("IV == null || IV.Length != 8"); } byte[] encrypted; //// Create an Aes object //// with the specified key and IV. using (DESCryptoServiceProvider desAlg = new()) { desAlg.Key = byaKey; desAlg.IV = byaIV; //// Create a decrytor to perform the stream transform. ICryptoTransform encryptor = desAlg.CreateEncryptor(desAlg.Key, desAlg.IV); //// Create the streams used for encryption. using (MemoryStream msEncrypt = new()) { using (CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new(csEncrypt)) { //// Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } //// Return the encrypted bytes from the memory stream. return encrypted; } /// /// Main decrypt function /// /// /// Plain text to be decrypt /// /// /// decrypt key /// /// /// decrypt initial vector /// /// /// decrypt result /// public override string DecryptByteToString(byte[] cipherText, byte[] byaKey, byte[] byaIV) { //// Check arguments. if (cipherText == null || cipherText.Length <= 0) { throw new ArgumentNullException("cipherText"); } if (byaKey == null || byaKey.Length != 8) { throw new ArgumentException("Key == null || Key.Length != 8"); } if (byaIV == null || byaIV.Length != 8) { throw new ArgumentException("IV == null || IV.Length != 8"); } //// Declare the string used to hold //// the decrypted text. string plaintext = null; //// Create an Aes object //// with the specified key and IV. using (DESCryptoServiceProvider desAlg = new()) { desAlg.Key = byaKey; desAlg.IV = byaIV; //// Create a decrytor to perform the stream transform. ICryptoTransform decryptor = desAlg.CreateDecryptor(desAlg.Key, desAlg.IV); //// Create the streams used for decryption. using (MemoryStream msDecrypt = new(cipherText)) { using (CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new(csDecrypt)) { //// Read the decrypted bytes from the decrypting stream //// and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } } }