EDN Admin
Well-known member
Hi all,
Im pretty new to Encryption/Decryption and I have been struggling for the last 2 days trying to decrypt data encrypted in Java.
From my understanding, the following java code use DES mode ECB with PKCS5Padding for Padding.
The key is: 7IC64w6ksLU
The value to decode is: cy0vlmQq8RIc3lalnjPKpA==
The java encryption script is the following: import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
public String ALGORITHM = "DES/ECB/PKCS5Padding";
public String HASH_ALGORITHM = "SHA";
public String CHARSET = "UTF-8";
private SecretKey desKey;
private FieldHelper encrypted = null;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
if (first){
first = false;
try {
setSecretKey("7IC64w6ksLU");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
r = createOutputRow(r, data.outputRowMeta.size());
// Get the value from an input field
String test_value = get(Fields.In, "vht_tax_number").getString(r);
// play around with it
if(test_value != null) {
String regexed=test_value.replaceAll("[^0-9]","");
String Encrypted = encrypt(regexed);
// Set a value in a new output field
get(Fields.Out, "encrypted").setValue(r, Encrypted);
} else {
get(Fields.Out, "encrypted").setValue(r, test_value);
}
// Send the row on to the next step.
putRow(data.outputRowMeta, r);
return true;
}
public String encrypt(Object valueToHide) {
if (valueToHide == null) {
return "";
}
try {
// Initialize the cipher for encryption
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, desKey);
// Our cleartext
byte[] cleartext = valueToHide.toString().getBytes(CHARSET);
// Encrypt the cleartext
byte[] ciphertext = cipher.doFinal(cleartext);
return new String(Base64.encodeBase64(ciphertext), CHARSET);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private SecretKey unwrapEncodedKey(String key) throws Exception {
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecretKey desKey = keygen.generateKey();
// Create the cipher
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init((Cipher.UNWRAP_MODE), desKey);
byte[] bytes = Base64.decodeBase64(key.getBytes());
SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
DESKeySpec keyspec = new DESKeySpec(bytes);
SecretKey k = desFactory.generateSecret(keyspec);
return k;
}
public void setSecretKey(String secretKey) throws Exception {
if (!StringUtils.isEmpty(secretKey)) {
desKey = this.unwrapEncodedKey(secretKey);
// Create the cipher
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init((Cipher.WRAP_MODE), desKey);
}
}
My C# decryption code is the following:using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class ConsoleApplication3
{
static void Main(string[] args)
{
try
{
// Create a new TripleDESCryptoServiceProvider object
// to generate a key and initialization vector (IV).
DESCryptoServiceProvider tDESalg = new DESCryptoServiceProvider();
tDESalg.Mode = CipherMode.ECB;
//tDESalg.Padding = PaddingMode.PKCS7;
tDESalg.GenerateKey();
//byte[] key = tDESalg.Key;
//Console.Write("Generated key: n");
//foreach(byte b in key){
// Console.Write(b + "n");
//}
//Console.Write("nn");
tDESalg.Key = System.Convert.FromBase64String("71C64w6ksLU=");
// Decrypt the buffer back to a string.
string Final = DecryptTextFromMemory(System.Convert.FromBase64String("cy0vlmQq8RIc3lalnjPKpA=="), tDESalg.Key, tDESalg.IV);
// Display the decrypted string to the console.
Console.WriteLine(Final);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new DESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];
// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
//Convert the buffer into a string and return it.
return new ASCIIEncoding().GetString(fromEncrypt);
//String res = System.Convert.ToBase64String(fromEncrypt);
//return res;
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
}
Please let me know if you have any suggestion for decrypting this value. I cant point out what I am doing wrong.
Regards,
Greg
View the full article
Im pretty new to Encryption/Decryption and I have been struggling for the last 2 days trying to decrypt data encrypted in Java.
From my understanding, the following java code use DES mode ECB with PKCS5Padding for Padding.
The key is: 7IC64w6ksLU
The value to decode is: cy0vlmQq8RIc3lalnjPKpA==
The java encryption script is the following: import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
public String ALGORITHM = "DES/ECB/PKCS5Padding";
public String HASH_ALGORITHM = "SHA";
public String CHARSET = "UTF-8";
private SecretKey desKey;
private FieldHelper encrypted = null;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
if (first){
first = false;
try {
setSecretKey("7IC64w6ksLU");
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
r = createOutputRow(r, data.outputRowMeta.size());
// Get the value from an input field
String test_value = get(Fields.In, "vht_tax_number").getString(r);
// play around with it
if(test_value != null) {
String regexed=test_value.replaceAll("[^0-9]","");
String Encrypted = encrypt(regexed);
// Set a value in a new output field
get(Fields.Out, "encrypted").setValue(r, Encrypted);
} else {
get(Fields.Out, "encrypted").setValue(r, test_value);
}
// Send the row on to the next step.
putRow(data.outputRowMeta, r);
return true;
}
public String encrypt(Object valueToHide) {
if (valueToHide == null) {
return "";
}
try {
// Initialize the cipher for encryption
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, desKey);
// Our cleartext
byte[] cleartext = valueToHide.toString().getBytes(CHARSET);
// Encrypt the cleartext
byte[] ciphertext = cipher.doFinal(cleartext);
return new String(Base64.encodeBase64(ciphertext), CHARSET);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private SecretKey unwrapEncodedKey(String key) throws Exception {
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecretKey desKey = keygen.generateKey();
// Create the cipher
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init((Cipher.UNWRAP_MODE), desKey);
byte[] bytes = Base64.decodeBase64(key.getBytes());
SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
DESKeySpec keyspec = new DESKeySpec(bytes);
SecretKey k = desFactory.generateSecret(keyspec);
return k;
}
public void setSecretKey(String secretKey) throws Exception {
if (!StringUtils.isEmpty(secretKey)) {
desKey = this.unwrapEncodedKey(secretKey);
// Create the cipher
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init((Cipher.WRAP_MODE), desKey);
}
}
My C# decryption code is the following:using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class ConsoleApplication3
{
static void Main(string[] args)
{
try
{
// Create a new TripleDESCryptoServiceProvider object
// to generate a key and initialization vector (IV).
DESCryptoServiceProvider tDESalg = new DESCryptoServiceProvider();
tDESalg.Mode = CipherMode.ECB;
//tDESalg.Padding = PaddingMode.PKCS7;
tDESalg.GenerateKey();
//byte[] key = tDESalg.Key;
//Console.Write("Generated key: n");
//foreach(byte b in key){
// Console.Write(b + "n");
//}
//Console.Write("nn");
tDESalg.Key = System.Convert.FromBase64String("71C64w6ksLU=");
// Decrypt the buffer back to a string.
string Final = DecryptTextFromMemory(System.Convert.FromBase64String("cy0vlmQq8RIc3lalnjPKpA=="), tDESalg.Key, tDESalg.IV);
// Display the decrypted string to the console.
Console.WriteLine(Final);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new DESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];
// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
//Convert the buffer into a string and return it.
return new ASCIIEncoding().GetString(fromEncrypt);
//String res = System.Convert.ToBase64String(fromEncrypt);
//return res;
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
}
Please let me know if you have any suggestion for decrypting this value. I cant point out what I am doing wrong.
Regards,
Greg
View the full article