I am using TripleDESCryptoServiceProvider class encrypt string which is provided Here
I am getting same encrypted string in the desktop code(not WP7 simple c#) with initilization vector = { 0, 0, 0, 0, 0, 0, 0, 0}
but if i change the IV to { 64, 64, 64, 64, 64, 64,64, 64} i am getting different encrypted strings .
for converting from byte string i am using the base64 encoding.
Here is my code:
Note:If i use the WP7 encrypted string tro decrypt in desktop first 8 characters are not decrypted correctly, remaining characters are correct. means in “Hi my friends ! I am safe with TripleDES :)”); only “ends ! I am safe with TripleDES :)” this much correct. some junk at starting.
private static byte[] sharedkey ={
0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11,
0x12, 0x11, 0x0D, 0x0B, 0x07, 0x02, 0x04, 0x08,
0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11
};
private static byte[] sharedvector = new byte[] { 64, 64, 64, 64, 64, 64, 64, 64 };
var toEncrypt = Encoding.UTF8.GetBytes(“Hi my friends ! I am safe with TripleDES :)”);
TripleDESCryptoServiceProvider tdesAlgorithm = new TripleDESCryptoServiceProvider();
// Create the encryptor
ICryptoTransform encryptor = tdesAlgorithm.CreateEncryptor(sharedkey, sharedvector);
var cryptedBytes = encryptor.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
// Create the decryptor
ICryptoTransform decryptor = tdesAlgorithm.CreateDecryptor(sharedkey, sharedvector);
var decryptedBytes = decryptor.TransformFinalBlock(cryptedBytes, 0, cryptedBytes.Length);
var uncryptedString = Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length);
EDIT: Looks like for ECB, the IV is ignored. In the code provided by Nocolus. Will you please tell me how to change that.
Try this post. Although, it is for AES encryption, it might help in your situation.
Bouncy Castle’s code supports Triple DES.
Hope that helps.
AFAIK, ECB does not need an Initialization Vector. Also, avoid ECB if you can, the alternatives are more secure.