I have a .NET method for DES encryption in a line:
public static string EncryptTripleDES(string value, byte[] encryptionKey, byte[] initializationVector) { if (!value.IsNullOrEmpty()) { TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(encryptionKey, initializationVector), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cs); sw.Write(value); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); //convert back to a string return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); } else { return ""; } }
As you can see, the algorithm accepts 2 parameters - “encryption key” and “initialization vector”.
Now I need to write a DES encryption / decryption function in Java parallel to this function, so if you provide the same encryption key and initialization vector, you can decrypt something encrypted in C # in Java. (Imposes overalls on Java, discards about 10 years since the last use of Java, Googles for DES encryption in Java ...)
A decent Java DES encryption approach is found here . But oh, honey, it turns out that this algorithm insists on an initialization vector of exactly 8 bytes; the .NET code uses an init vector of 24 bytes!
? Java 8- ? , 24- ?
8 24- Java? , , , 8 , Triple DES 8 . , .NET- , , , IV . , . , 8- IV.
.NET , . , .NET , , CBC. , , , CBC PKCS5Padding . , , .
Java, , #, 24- IV, 8- IV, Java, . , - . Bouncycastle, @Tim, , , ,.NET .
MSDN:
IV , GenerateIV. IV , BlockSize.
, , BlockSize, IV .
, , , DES 8 (64 , 8 , 56 ). TripleDES (3DES) 24 ( 192 , 24 , 168 ).
, TripleDES Java ( , , SO Question), , DES .NET.
Have you looked at the Bouncy Castle library for Java? I cannot tell from their (very rare) documentation and samples, but it seems to be a widely used library, so I hope it will be so flexible. It’s worth a look, at least if you haven’t done it yet.