Java Code
---------------
package JavaTripleDES;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64; <strong>//This needs to be downloaded and added to eclipse as an external library</strong>
public class DESEncryption {
private static final String UNICODE_FORMAT = "UTF8";
public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
private final KeySpec myKeySpec;
private final SecretKeyFactory mySecretKeyFactory;
private final Cipher cipher;
private final byte[] keyAsBytes;
private final String myEncryptionKey;
private final String myEncryptionScheme;
private final SecretKey key;
public DESEncryption() throws Exception {
myEncryptionKey = "hisIsSecretEncryptionKey";
myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
myKeySpec = new DESedeKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
cipher = Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
}
/**
* Method To Encrypt The String
*/
public String encrypt(String unencryptedString) {
String encryptedString = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
byte[] encryptedText = cipher.doFinal(plainText);
encryptedString = new String(Base64.encodeBase64(encryptedText));
} catch (Exception e) {
e.printStackTrace();
}
return encryptedString;
}
/**
* Method To Decrypt An Encrypted String
*/
public String decrypt(String encryptedString) {
String decryptedText = null;
try {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptedText = Base64.decodeBase64(encryptedString);
byte[] plainText = cipher.doFinal(encryptedText);
decryptedText = bytes2String(plainText);
} catch (Exception e) {
e.printStackTrace();
}
return decryptedText;
}
/**
* Returns String From An Array Of Bytes
*/
private static String bytes2String(byte[] bytes) {
StringBuilder stringBuffer = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
stringBuffer.append((char) bytes[i]);
}
return stringBuffer.toString();
}
/**
* Testing the DES Encryption And Decryption Technique
*/
public static void main(String args[]) throws Exception {
DESEncryption myEncryptor = new DESEncryption();
String stringToEncrypt = "12345678&First&Last&M&Lake%20Park";
String encrypted = myEncryptor.encrypt(stringToEncrypt);
String decrypted = myEncryptor.decrypt(encrypted);
System.out.println("String To Encrypt: " + stringToEncrypt);
System.out.println("Encrypted Value : " + encrypted);
System.out.println("Decrypted Value : " + decrypted);
}
}
-------------------
VB.NET Code
--------------------
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Form1
Private Shared ReadOnly encryptionKey As String = "ThisIsSecretEncryptionKe" <strong>//Key Length matters which is why the trailing 'y' is missing</strong>
Public Sub New()
InitializeComponent() //This is added automatically
Dim strTestValueFromJava As String = "6JtWysSeMxc1L6I0wRJi9EOakFWfOo/+uM8K0PnwhEAYenfxZ1Yw9w=="
Dim dencryptedData As String = Decrypt(strShouldBe, False)
End Sub
Public Shared Function Decrypt(cipherString As String, useHashing As Boolean) As String
Dim keyArray As Byte()
Dim toEncryptArray As Byte() = Convert.FromBase64String(cipherString.Replace(" "c, "+"c))
If useHashing Then
' If hashing was used get the hash code with regards to your key
Dim hashmd5 As New MD5CryptoServiceProvider()
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(encryptionKey))
hashmd5.Clear()
Else
' If hashing was not implemented get the byte code of the key
keyArray = UTF8Encoding.UTF8.GetBytes(encryptionKey)
End If
' Set the secret key for the tripleDES algorithm
Dim tdes As New TripleDESCryptoServiceProvider()
tdes.Key = keyArray
tdes.Mode = CipherMode.ECB
tdes.Padding = PaddingMode.PKCS7
Dim cTransform As ICryptoTransform = tdes.CreateDecryptor()
Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
tdes.Clear()
' Return the Clear decrypted TEXT
Return UTF8Encoding.UTF8.GetString(resultArray)
End Function
End Class