I have been trying rather unsuccessfully to encrypt a plaintext using 3DES in java by using the BouncyCastle suite. This result is supposed to match that produced by an existing C# implementation because I plan to be decrypting it later.
I keep getting different results though I am convinced I have produced the "equivalent" of the C# algo in Java. Could someone kindly look through both snippets and advise? I would be most grateful.
C# encryption:
public static byte[] encryptStringToBytes_3DES(string plainText, string passKey)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
// Declare the streams used
// to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
// used to encrypt the data.
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
string passphrase = passKey;
byte[] iv = ascii.GetBytes("AVREWASH");
byte[] key = ascii.GetBytes(passphrase);
try
{
// Create a TripleDES object
// with the specified key and IV.
//Console.WriteLine("Key size is " + tdes.KeySize+" and IV is "+tdes.IV+" and that of key is "+key.Length);
tdes.Key = key;
tdes.IV = iv;
tdes.Padding = PaddingMode.Zeros;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = tdes.CreateEncryptor(tdes.Key, tdes.IV);
// Create the streams used for encryption.
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);
//Write all data to the stream.
swEncrypt.Write(plainText);
}
catch (Exception ex)
{
Console.WriteLine("Error is " + ex.Message);
while (true)
{
}
}
finally
{
// Clean things up.
// Close the streams.
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
// Clear the TripleDES object.
if (tdes != null)
tdes.Clear();
}
// Return the encrypted bytes from the memory stream.
return msEncrypt.ToArray();
}
There is this helper function I use in converting results to Hex...
public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
The Java snippet supposed to be doing the "equivalent" encryption follows too:
public void encrypt(String plaintext, String IV, String tripleDesKey){
try{
SecretKey keySpec = new SecretKeySpec(tripleDesKey.getBytes("US-ASCII"),"DESede");
IvParameterSpec iv = new IvParameterSpec(IV.getBytes("US-ASCII"));
Cipher e_cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
e_cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
byte [] cipherText = e_cipher.doFinal(plaintext.trim().getBytes("US-ASCII"));
System.out.println("Ciphertext: " + asHex(cipherText));
}
catch(Exception exc){
ex.printStackTrace();
}
}
here is its corresponding Hex.. function
public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
PLEASE HELP.