RC2 Encryption/Decryption

2.6k views Asked by At

When I'm trying to decrypt a string the visual studio throws an exception: System.Security.Cryptography.CryptographicException and says that length of data for decrypting is invalid. Exception trows when compiler reaches cs.Close() in RC2_Decrypt method.

static byte[] RC2_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
    {
        byte[] encryptedBytes = null;
        string salt = "D495560961CCCFE0";
        byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
        using (MemoryStream msStream = new MemoryStream())
        {
            using (RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider())
            {
                RC2.KeySize = 128;
                RC2.BlockSize = 64;
                var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                RC2.Key = key.GetBytes(RC2.KeySize / 8);
                RC2.IV = key.GetBytes(RC2.BlockSize / 8);
                RC2.Mode = CipherMode.CBC;
                using (var cs = new CryptoStream(msStream, RC2.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                    cs.Close();
                }
                encryptedBytes = msStream.ToArray();
            }
        }
        return encryptedBytes;
    }

static byte[] RC2_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
    {
        byte[] decryptedBytes = null;
        string salt = "D495560961CCCFE0";
        byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
        using (MemoryStream msStream = new MemoryStream())
        {
            using (RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider())
            {
                RC2.KeySize = 128;
                RC2.BlockSize = 64;
                var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                RC2.Key = key.GetBytes(RC2.KeySize / 8);
                RC2.IV = key.GetBytes(RC2.BlockSize / 8);
                RC2.Mode = CipherMode.CBC;
                using (var cs = new CryptoStream(msStream, RC2.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                    cs.Close();
                }
                decryptedBytes = msStream.ToArray();
            }
        }
        return decryptedBytes;
    }

Here example I'm just testing those methods. So for a start I'm tried to encrypt a simple string.

static void Main(string[] args)
    {
        string password = "770A8A65DA156D24EE2A093277530142";
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        Console.WriteLine("Encrypting");
        string str = "Hello world";
        Console.WriteLine(EncString(str, password));
        byte[] encArray = Encoding.UTF8.GetBytes(str);
        Console.WriteLine(DecString(str, password));
        Console.ReadKey();
    }

Methods that I'm using for string encrypting:

 static string EncString(string message, string password)
    {
        byte[] byresToBeEncrypted = Encoding.UTF8.GetBytes(message);
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] bytesToBeEncrypted = RC2_Encrypt(byresToBeEncrypted, passwordBytes);
        string result = Convert.ToBase64String(byresToBeEncrypted);
        return result;
    }

static string DecString(string message, string password)
    {
        byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(message);
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] bytesToBeDecrypted = RC2_Decrypt(bytesToBeEncrypted, passwordBytes);
        string result = Encoding.UTF8.GetString(bytesToBeDecrypted);
        return result;
    }

Well those methods can encrypt and decrypt text file and thats all I need. But I still have a question. Why this doesn't work with simple string variable?

0

There are 0 answers