Encryption of sensitive data is very important in most of the software applications today. In this feed I’ll show you how to encrypt and decrypt a string data using the encryption classes. The algorithm using one key to encrypt and the same key to decrypt the data. I’ll be using the TripleDES encryption algorithm which is considered to be very secure. It performs three times as much encryption as the standard DES.
.NET (C#) Implementation
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
public class TripleDESImplementation
{
//Encryption Key
private byte[]
EncryptionKey { get; set;
}
// The Initialization Vector for the DES encryption routine
private byte[] IV { get; set; }
/// <summary>
/// Constructor for
TripleDESImplementation class
/// </summary>
/// <param name="encryptionKey">The
24-byte encryption key (24 character ASCII)</param>
/// <param name="IV">The 8-byte DES encryption initialization vector (8
characters ASCII)</param>
public TripleDESImplementation(string encryptionKey, string
IV)
{
if (string.IsNullOrEmpty(encryptionKey))
{
throw new ArgumentNullException("'encryptionKey'
parameter cannot be null.", "encryptionKey");
}
if (string.IsNullOrEmpty(IV))
{
throw new ArgumentException("'IV'
parameter cannot be null or empty.", "IV");
}
EncryptionKey = Encoding.ASCII.GetBytes(encryptionKey);
// Ensures length of 24 for encryption key
Trace.Assert(EncryptionKey.Length == 24, "Encryption key must be exactly 24 characters of
ASCII text (24 bytes)");
this.IV = Encoding.ASCII.GetBytes(IV);
// Ensures length of 8 for init. vector
Trace.Assert(IV.Length == 8, "Init. vector must be exactly 8 characters of ASCII
text (8 bytes)");
}
/// <summary>
/// Encrypts a text block
/// </summary>
public string
Encrypt(string textToEncrypt)
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = EncryptionKey;
tdes.IV = IV;
byte[] buffer = Encoding.ASCII.GetBytes(textToEncrypt);
return Convert.ToBase64String(tdes.CreateEncryptor().TransformFinalBlock(buffer,
0, buffer.Length));
}
/// <summary>
/// Decrypts an encrypted
text block
/// </summary>
public string
Decrypt(string textToDecrypt)
{
byte[] buffer = Convert.FromBase64String(textToDecrypt);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = EncryptionKey;
des.IV = IV;
return Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer,
0, buffer.Length));
}
}
Java Implementation
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class TripleDesImplementation {
private String key;
private String initializationVector;
public TripleDesImplementation(String key, String initializationVector)
{
this.key = key;
this.initializationVector = initializationVector;
}
public String encryptText(String plainText) throws Exception{
//---- Use the same 3DES key and IV-------------------------
byte[] plaintext = plainText.getBytes();
byte[] tdesKeyData = key.getBytes();
byte[] myIV = initializationVector.getBytes();
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede");
IvParameterSpec ivspec = new IvParameterSpec(myIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
byte[] cipherText = c3des.doFinal(plaintext);
return Base64Coder.encodeString(new String(cipherText));
}
}
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class TripleDesImplementation {
private String key;
private String initializationVector;
public TripleDesImplementation(String key, String initializationVector)
{
this.key = key;
this.initializationVector = initializationVector;
}
public String encryptText(String plainText) throws Exception{
//---- Use the same 3DES key and IV-------------------------
byte[] plaintext = plainText.getBytes();
byte[] tdesKeyData = key.getBytes();
byte[] myIV = initializationVector.getBytes();
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede");
IvParameterSpec ivspec = new IvParameterSpec(myIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
byte[] cipherText = c3des.doFinal(plaintext);
return Base64Coder.encodeString(new String(cipherText));
}
}