Black Falcon Software: Easy TripleDES Encryption & Decryption

The following code modules will allow you to encrypt\decrypt string data using the TripleDES cryptography class.  It is a great set of routines for when you need easy to use security without the requirement for the more advanced cryptographic techniques and methodologies.

Note that the modules were originally made part of a console application so do not use the “main” methods if you want to place this code in a class library.  Also note that the C# version was converted using an online code-converter, which did not take into account the VB.NET’s “ReDim” statement.  As a result, the expansion of the array where the “ReDim” comment is in the C# code will have to be modified.

Simply “cut and paste” the code you want…

Black Falcon

VB.NET

Imports System
Imports System.IO
Imports System.Text
Imports System.Security
Imports System.Collections
Imports System.Security.Cryptography

Module modMain

  Sub Main()

    Dim lsEnryptedValue   As String = ""
    Dim lsDecryptedValue  As String = ""

    lsEnryptedValue = TripleDESEncode("string-to-encrypt", "any-keystring-you-would-like")
    Console.WriteLine(lsEnryptedValue)

    lsDecryptedValue = TripleDESDecode(lsEnryptedValue, "any-keystring-you-would-like")
    Console.WriteLine(lsDecryptedValue)

    Console.ReadLine()

  End Sub

  Private Function TripleDESEncode(ByVal psValue As String, ByVal psKey As String) As String

    Dim loTripleDES            As New TripleDESCryptoServiceProvider()
    Dim loPasswordDeriveBytes  As New PasswordDeriveBytes(psKey, New Byte(-1) {})
    Dim loMemoryStream         As New MemoryStream((psValue.Length * 2) - 1)
    Dim loCryptoStream         As CryptoStream
    Dim loPlainBytes           As Byte() = Encoding.UTF8.GetBytes(psValue)
    Dim loEncryptedBytes       As Byte()

    loTripleDES.IV = New Byte(7) {}
    loTripleDES.Key = loPasswordDeriveBytes.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})

    loCryptoStream  = New CryptoStream(loMemoryStream, loTripleDES.CreateEncryptor(), CryptoStreamMode.Write)
    loCryptoStream.Write(loPlainBytes, 0, loPlainBytes.Length)
    loCryptoStream.FlushFinalBlock()

    Redim loEncryptedBytes(CInt(loMemoryStream.Length - 1))

    loMemoryStream.Position = 0
    loMemoryStream.Read(loEncryptedBytes, 0, CInt(loMemoryStream.Length))

    loCryptoStream.Close()

    Return (Convert.ToBase64String(loEncryptedBytes))
  End Function

  Public Function TripleDESDecode(ByVal psValue As String, ByVal psKey As String) As String

    Dim loTripleDES            As New TripleDESCryptoServiceProvider()
    Dim loPasswordDeriveBytes  As New PasswordDeriveBytes(psKey, New Byte(-1) {})
    Dim loEncryptedBytes       As Byte() = Convert.FromBase64String(psValue)
    Dim loMemoryStream         As New MemoryStream(psValue.Length)
    Dim loCryptoStream         As CryptoStream
    Dim loPlainBytes           As Byte()

    loTripleDES.IV = New Byte(7) {}
    loTripleDES.Key = loPasswordDeriveBytes.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})

    loCryptoStream = New CryptoStream(loMemoryStream, loTripleDES.CreateDecryptor(), CryptoStreamMode.Write)
    loCryptoStream.Write(loEncryptedBytes, 0, loEncryptedBytes.Length)
    loCryptoStream.FlushFinalBlock()

    ReDim loPlainBytes(CInt(loMemoryStream.Length - 1))

    loMemoryStream.Position = 0
    loMemoryStream.Read(loPlainBytes, 0, CInt(loMemoryStream.Length))

    loCryptoStream.Close()

    Return (Encoding.UTF8.GetString(loPlainBytes))
  End Function

End Module

C#

using System;
using System.IO;
using System.Text;
using System.Security;
using System.Collections;
using System.Security.Cryptography;

static class modMain
{

  public static void Main()
  {

    string lsEnryptedValue = "";
    string lsDecryptedValue = "";

    lsEnryptedValue = TripleDESEncode("string-to-encrypt", "any-keystring-you-would-like");
    Console.WriteLine(lsEnryptedValue);

    lsDecryptedValue = TripleDESDecode(lsEnryptedValue, "any-keystring-you-would-like");
    Console.WriteLine(lsDecryptedValue);

    Console.ReadLine();
  }

  private static string TripleDESEncode(string psValue, string psKey)
  {

    TripleDESCryptoServiceProvider loTripleDES = new TripleDESCryptoServiceProvider();
    PasswordDeriveBytes loPasswordDeriveBytes = new PasswordDeriveBytes(psKey, new byte[-1 + 1]);
    MemoryStream loMemoryStream = new MemoryStream((psValue.Length * 2) - 1);
    CryptoStream loCryptoStream = default(CryptoStream);
    byte[] loPlainBytes = Encoding.UTF8.GetBytes(psValue);
    byte[] loEncryptedBytes = null;

    loTripleDES.IV = new byte[8];
    loTripleDES.Key = loPasswordDeriveBytes.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);

    loCryptoStream = new CryptoStream(loMemoryStream, loTripleDES.CreateEncryptor(), CryptoStreamMode.Write);
    loCryptoStream.Write(loPlainBytes, 0, loPlainBytes.Length);
    loCryptoStream.FlushFinalBlock();

    // ERROR: Not supported in C#: ReDimStatement

    loMemoryStream.Position = 0;
    loMemoryStream.Read(loEncryptedBytes, 0, (int)loMemoryStream.Length);

    loCryptoStream.Close();

    return (Convert.ToBase64String(loEncryptedBytes));
  }

  public static string TripleDESDecode(string psValue, string psKey)
  {

    TripleDESCryptoServiceProvider loTripleDES = new TripleDESCryptoServiceProvider();
    PasswordDeriveBytes loPasswordDeriveBytes = new PasswordDeriveBytes(psKey, new byte[-1 + 1]);
    byte[] loEncryptedBytes = Convert.FromBase64String(psValue);
    MemoryStream loMemoryStream = new MemoryStream(psValue.Length);
    CryptoStream loCryptoStream = default(CryptoStream);
    byte[] loPlainBytes = null;

    loTripleDES.IV = new byte[8];
    loTripleDES.Key = loPasswordDeriveBytes.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);

    loCryptoStream = new CryptoStream(loMemoryStream, loTripleDES.CreateDecryptor(), CryptoStreamMode.Write);
    loCryptoStream.Write(loEncryptedBytes, 0, loEncryptedBytes.Length);
    loCryptoStream.FlushFinalBlock();

    // ERROR: Not supported in C#: ReDimStatement

    loMemoryStream.Position = 0;
    loMemoryStream.Read(loPlainBytes, 0, (int)loMemoryStream.Length);

    loCryptoStream.Close();

    return (Encoding.UTF8.GetString(loPlainBytes));
  }

}

About this entry