You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

147 lines
4.9 KiB

//-----------------------------------------------------------------------
// <copyright file="TripleDES.cs" company="Origtek">
// TripleDES belongs to Copyright (c) Origtek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace OT.COM.Encryption
{
using System;
using System.IO;
using System.Security.Cryptography;
/// <summary>
/// 3DES Encrypt Decrypt
/// </summary>
public partial class TripleDES : AEncryption
{
/// <summary>
/// Main encrypt function
/// </summary>
/// <param name="plainText">
/// Plain text to be encrypt
/// </param>
/// <param name="byaKey">
/// Encrypt key
/// </param>
/// <param name="byaIV">
/// Encrypt initial vector
/// </param>
/// <returns>
/// Encrypt result byte
/// </returns>
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 != 16) && (byaKey.Length != 24)))
{
throw new ArgumentException("Key == null || ((Key.Length != 16) && (Key.Length != 24))");
}
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 (TripleDESCryptoServiceProvider 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))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
//// Return the encrypted bytes from the memory stream.
return encrypted;
}
/// <summary>
/// Main decrypt function
/// </summary>
/// <param name="cipherText">
/// Plain text to be decrypt
/// </param>
/// <param name="byaKey">
/// decrypt key
/// </param>
/// <param name="byaIV">
/// decrypt initial vector
/// </param>
/// <returns>
/// decrypt result
/// </returns>
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 != 16) && (byaKey.Length != 24)))
{
throw new ArgumentException("Key == null || ((Key.Length != 16) && (Key.Length != 24))");
}
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 (TripleDESCryptoServiceProvider 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;
}
}
}