In my app on windows phone 8 I need to encrypt-decrypt data using DESede/CBC/PKCS5Padding with PBKDF2 key.
I found example how to encrypt using Bouncy Castle:
http://www.go4expert.com/articles/bouncy-castle-net-implementation-triple-t24829/
And I was able to encrypt my data (text), but I can't decrypt my encrypted data using this code.
When I'm trying to call DoFinal() it throws exception "pad block corrupted".
Maybe I missed something in decrypting?
Maybe somebody knows alternative library (way) on windows phone 8 for encryption using DESede/CBC/PKCS5Padding with PBKDF2 key?
using Raksha.Crypto;
using Raksha.Crypto.Engines;
using Raksha.Crypto.Modes;
using Raksha.Crypto.Paddings;
using Raksha.Crypto.Parameters;
using System;
using System.Security.Cryptography;
using System.Text;
namespace MyNamespace
{
class EncryptDecrypt
{
Rfc2898DeriveBytes key;
BufferedBlockCipher cipherEncrypt;
BufferedBlockCipher cipherDecrypt;
private readonly int ITERACTIONCOUNT = 1000;
private readonly int KEY_LENGTH = 24;
public EncryptDecrypt(string passPhrase, string salt)
{
byte[] SALT = Encoding.UTF8.GetBytes(salt);
key = new Rfc2898DeriveBytes(passPhrase, SALT, ITERACTIONCOUNT);
DesEdeEngine desede = new DesEdeEngine();
cipherEncrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(desede));
cipherDecrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(desede));
DesEdeParameters p = new DesEdeParameters(key.GetBytes(KEY_LENGTH));
cipherEncrypt.Init(true, p);
cipherDecrypt.Init(false, p);
}
public byte[] Encrypt(byte[] dataToEncrypt)
{
try
{
byte[] outbytes = cipherEncrypt.DoFinal(dataToEncrypt);
return outbytes;
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine("Encrypt() Exception: " + ex.Message);
return new byte[] { 0 };
}
}
public byte[] Decrypt(byte[] dataToDecrypt)
{
try
{
byte[] result = cipherDecrypt.DoFinal(dataToDecrypt);
return result;
}
catch(CryptoException ex)
{
System.Diagnostics.Debug.WriteLine("Decrypt() Exception: " + ex.Message);
// ex.Message: pad block corrupted
return new byte[] { 0 };
}
}
}
Raksha.Crypto
is, I'm otherwise stumped. – Maarten Bodewes