/// <summary>
        /// Decrypt data using this key.
        /// </summary>
        /// <param name="data">16, 32 or 48 hex data to decrypt.</param>
        /// <returns>Decrypted data.</returns>
        public string Decrypt(string data)
        {
            ValidateEncryptDecryptData(data);

            if (data.Length == 16)
            {
                return(TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data));
            }

            if (data.Length == 32)
            {
                return(TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(0, 16)) +
                       TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(16, 16)));
            }

            return(TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(0, 16)) +
                   TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(16, 16)) +
                   TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(32, 16)));
        }
        /// <summary>
        /// Decrypt data using CBC mode.
        /// </summary>
        /// <param name="data">16, 32 or 48 hex data to decrypt.</param>
        /// <returns>Decrypted data.</returns>
        public string DecryptCbc(string data)
        {
            ValidateEncryptDecryptData(data);

            if (data.Length == 16)
            {
                return(TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data));
            }

            if (data.Length == 32)
            {
                var resulta1 = TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(0, 16));
                return(resulta1 + TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(16, 16)).XorHex(data.Substring(0, 16)));
            }

            var resulta2 = TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(0, 16));
            var resultb  = TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(16, 16).XorHex(data.Substring(0, 16)));

            return(resulta2 + resultb + TripleDes.TripleDesDecrypt(PartA, PartB, PartC, data.Substring(32, 16).XorHex(data.Substring(16, 16))));
        }
Пример #3
0
 public void TestTripleDes(byte[] key1, byte[] key2, byte[] key3, byte[] data, byte[] expected)
 {
     Assert.AreEqual(expected, TripleDes.TripleDesEncrypt(key1, key2, key3, data));
     Assert.AreEqual(data, TripleDes.TripleDesDecrypt(key1, key2, key3, expected));
 }