Using DES Algorithm in Various Languages

Home / Using DES Algorithm in Various Languages

Using DES Algorithm in Various Languages

March 8, 2017 | Article | No Comments

Data Encryption Standard (DES) or should be Data Encryption Algorithm, is a symmetric-key algorithm for the encryption of electronic data. Although this algorithm is now considered as insecure, it was highly influental in the advancement of modern cryptography. Most course on cryptography still present DES when they are discussing about block cipher especially symmetric one.

This article will demonstrate how to use DES for encrypt and decrypt content in various programming language. The snippet code here should be platform independent unless said otherwise. The implementation will be based on library or framework for respective programming language. If there are more than one implementation, we will divide the section for each implementation.

If you need information about specific detail in DES, go to online course such as this

The Implementation

Implementation: C#

In C# implementation, each algorithm is provided by a service provider. All operations should be done in byte array, therefore we have some string and byte array conversion in some place.

The key can be an input from user or generated by you. Just remember to use byte array.

// Library
using System.Text;
using System.Security.Cryptography;

// Key and Initialization Vector
byte[] key = ASCIIEncoding.ASCII.GetBytes("12345678");
byte[] iv  = ASCIIEncoding.ASCII.GetBytes("01234567");

DESCryptoServiceProvider crypto = new DESCryptoServiceProvider();

// Tweak the provider
crypto.Key = key;
crypto.Mode = CipherMode.CBC;    // Options: ECB, CFB

The following is using stream for manipulating content.

// Encrypt
ICryptoTransofrm transform = crypto.CreateEncryptor();
MemoryStream memStream = new MemoryStream();
CryptoStream cryStream = new CryptoStream(memStream, transform, CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryStream);
writer.Write(plaintext);
writer.Flush();
cryStream.FlushFinalBlock();
writer.Flush();
byte[] ciphertext = memStream.GetBuffer();


// Decrypt
ICryptoTransform transform = crypto.CreateDecryptor();
MemoryStream memStream = new MemoryStream(ciphertext);
CryptoStream cryStream = new CryptoStream(memStream, transform, CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryStream);
string plaintext = reader.ReadToEnd();

This one is straightforward implementation without utilizing stream

// Encrypt
byte[] buff = ASCIIEncoding.ASCII.GetBytes(plaintext);
crypto.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length);


// Decrypt
byte[] buff = ASCIIEncoding.ASCII.GetBytes(ciphertext);
crypto.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length);

Implementation: Java

The java implementation is called JCE (Java Cryptography Extension). All operations should be done in byte array, therefore we have some string and byte array conversion in some place.

// Library
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

In this case we can generate a key or using our own key.

// Key generator
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecretKey key = keygen.generateKey();


// Using own key
String strkey = "12345678";
DESKeySpec dks = new DESKeySpec(strkey.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey key = skf.generateSecret(dks);

Creating cipher instance from Cipher class, specifying some information such as algorithm name, mode, and padding scheme. The last two are optional. All should be separated by a slash.

// Encrypt
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding");
byte[] text = plaintext.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(text);


// Decrypt
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding");
byte[] text = ciphertext.getBytes();
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(text);

Implementation: Visual Basic (VB) .NET

Actually, VB.NET implementation is similar to C# one. Each algorithm is provided by a service provider. All operations should be done in byte array, therefore we have some string and byte array conversion in some place.

The key can be an input from user or generated by you. Just remember to use byte array.

// Library
Imports System.Security.Cryptography;

// Key and Initialization Vector
Dim key() As Byte = System.Text.Encoding.Unicode.GetBytes("12345678");
Dim iv()  As Byte = System.Text.Encoding.Unicode.GetBytes("01234567"); 

Dim crypto As DESCryptoServiceProvider = new DESCryptoServiceProvider; // Tweak the provider 
crypto.Key = key;
crypto.Mode = CipherMode.CBC; // Options: ECB, CFB

The following is using stream for manipulating content.

// Encrypt
Dim transform As ICryptoTransform = crypto.CreateEncryptor();
Dim memStream As New System.IO.MemoryStream;
Dim cryStream As New CryptoStream(memStream, transform, CryptoStreamMode.Write);
Dim text() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext);

cryStream.Write(text,0, text.Length)
cryStream.FlushFinalBlock()

Dim result() As Byte = memStream.ToArray


// Decrypt
Dim transform As ICryptoTransform = crypto.CreateDecryptor();
Dim memStream As New System.IO.MemoryStream;
Dim cryStream As New CryptoStream(memStream, transform, CryptoStreamMode.Write);

cryStream.Write(ciphertext,0, ciphertext.Length)
cryStream.FlushFinalBlock()

Dim result() As Byte = memStream.ToArray

Implementation: Python

There are some libraries in python that implementing DES algorithm.Each has different style and flavor. Here we use PyCrypto library as example. As Python is not strongly typed, we should not worry about the data type. However internally all operation is done in block so you might consider to use byte array.

The key and be an input from user or generated by you. Here, we have two example.

# Library
from Crypto.Cipher import DES
from Crypto import Random

key = b'eight byte key'
iv = Random.new().read(DES.block_size)

crypto = DES.new(key, MODE_CBC, iv)
# other option: MODE_ECB, MODE_CFB

Then we did something like this:

# Encrypt
result = crypto.encrypt(plaintext)

# Decrypt
result = crypto.decrypt(plaintext)

 

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial