Back to all posts
C#.NETSecurity

How to TripleDES Encrypt Decrypt Data? – Part One

SathishApril 23, 2010
How to TripleDES Encrypt Decrypt Data? – Part One

TripleDES (3DES) is a symmetric-key block cipher that applies the DES cipher algorithm three times to each data block.

Encryption Implementation

csharp
using System;
using System.Security.Cryptography;
using System.Text;

public static string Encrypt(string plainText, string key)
{
    byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
    
    using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
    {
        tdes.Key = Encoding.UTF8.GetBytes(key.Substring(0, 24));
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;
        
        ICryptoTransform encryptor = tdes.CreateEncryptor();
        byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
        
        return Convert.ToBase64String(encryptedBytes);
    }
}

Key Points

  • TripleDES key must be 24 bytes (192 bits)
  • The algorithm applies DES three times
  • More secure than single DES but slower than AES
  • Still used in legacy systems

Continue to Part Two for the decryption implementation.

0claps
Share this post

Comments

Protected by reCAPTCHA v3

No comments yet. Be the first to comment.