TripleDes problem in .Net

zequion 446 Reputation points
2024-07-22T03:56:40.81+00:00

I use TripleDes to encrypt/decrypt. I have a program in the .Net Framework version and in the .Net version.

According to the code I show, in .NetFw it returns an object "System.Security.Cryptography.CryptoAPITransform" to TDes_ICryptoTransform and for .Net it returns an object "System.Security.Cryptography.UniversalCryptoDecryptor".

It seems that "System.Security.Cryptography.CryptoAPITransform" is not supported in .Net and when trying to decode a string encoded in .NetFw in .Net, the results are different.

How can I get "System.Security.Cryptography.CryptoAPITransform" to return in .Net or to always generate "System.Security.Cryptography.UniversalCryptoDecryptor" in both .NetFw and .Net?

in both systems, TDes_TripleDESCryptoServiceProvider is an object "System.Security.Cryptography.TripleDESCryptoServiceProvider".


System.Security.Cryptography.ICryptoTransform TDes_ICryptoTransform = TDes_TripleDESCryptoServiceProvider.CreateEncryptor(key, iv);
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 46,466 Reputation points Microsoft Vendor
    2024-07-22T11:20:45.48+00:00

    Hi @zequion , Welcome to Microsoft Q&A,

    Use the TripleDESCryptoServiceProvider class to ensure that the same encryption and decryption logic is used on .NET Framework and .NET Core, avoiding issues caused by platform differences.

    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    
    public class TripleDESEncryption
    {
        public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
        {
            using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
            {
                tdes.Key = key;
                tdes.IV = iv;
                tdes.Mode = CipherMode.CBC;
                tdes.Padding = PaddingMode.PKCS7;
    
                using (ICryptoTransform encryptor = tdes.CreateEncryptor())
                {
                    return PerformCryptography(data, encryptor);
                }
            }
        }
    
        public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
        {
            using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
            {
                tdes.Key = key;
                tdes.IV = iv;
                tdes.Mode = CipherMode.CBC;
                tdes.Padding = PaddingMode.PKCS7;
    
                using (ICryptoTransform decryptor = tdes.CreateDecryptor())
                {
                    return PerformCryptography(data, decryptor);
                }
            }
        }
    
        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(data, 0, data.Length);
                    cryptoStream.FlushFinalBlock();
                    return memoryStream.ToArray();
                }
            }
        }
    }
    
    class Program
    {
        static void Main()
        {
            byte[] key = Encoding.UTF8.GetBytes("123456789012345678901234"); // 24 bytes for TripleDES
            byte[] iv = Encoding.UTF8.GetBytes("12345678"); // 8 bytes for TripleDES
            byte[] data = Encoding.UTF8.GetBytes("Hello World!");
    
            // Encrypt the data
            byte[] encryptedData = TripleDESEncryption.Encrypt(data, key, iv);
            Console.WriteLine("Encrypted data: " + Convert.ToBase64String(encryptedData));
    
            // Decrypt the data
            byte[] decryptedData = TripleDESEncryption.Decrypt(encryptedData, key, iv);
            Console.WriteLine("Decrypted data: " + Encoding.UTF8.GetString(decryptedData));
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.