I am working on a project that requires 3DES encryption in Java. The problem is that I was (and will continue to) come with a 128-bit hexadecimal key, for example, "0123456789ABCDEF0123456789ABCDEF". Converting to bytes is not a problem. but the problem is that the Java cryptographic extension API will overwhelm this key, stating that it is invalid. I understand that the MSB of each byte is just a parity bit, so JCE expects me to delete them (or I think). However, in .NET, I can specify the key as provided, and it quietly handles encryption / decryption without any complaints.
Is there a way to generate the type of key that JCE expects from the type of key that is provided to me?
I found that JCE allows you to specify an 8-byte key for DES encryption, so I tried to implement 3DES as DES EDE using half of the key provided. However, I am still getting inconsistent results with .NET.
Here's the Java code:
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.IvParameterSpec; public class Main{ public static void main(String[] args) throws Exception { byte [] plain = "I eat fish every day".getBytes("utf-8"); byte [] keyBytes = new byte [] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; byte [] key2Bytes = new byte [] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0 };
and here is the .NET code:
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace EncryptionDemo { class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!");
Both produce different outputs (some characters in the Base64 string are the same)