0

I'm trying to encrypt a text someone entered into a richTextBox, but i'm having some troubles with it. I use this code when a Button is pressed to encrypt the text of the richTextBox:

cData = richTextBox1.Text;
pBytes = Encoding.ASCII.GetBytes(cData);
string pKey2 = InputBox("Encryption-Key", "Enter a Encryption-Key:");
if (pKey2.Length % 8 != 0)
{
    int pKey2NeededLength = 0;
    for (int i = 0; i < (pKey2.Length + 8); i++)
    {
        if ((i+pKey2.Length) % 8 == 0)
        {
            pKey2NeededLength = i;
            MessageBox.Show("" + pKey2NeededLength);
            break;
        }
    }
    StringBuilder sB = new StringBuilder();
    string[] pArray = { "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678" };
    sB.Append(pKey2 + pArray[pKey2NeededLength-1]);
    pKey2 = sB.ToString();
    MessageBox.Show("" + pKey2);
}
pKey = Encoding.ASCII.GetBytes(pKey2);
MessageBox.Show(""+pKey);
desObj.Key = pKey;//Error occures here
desObj.Mode = CipherMode.CBC;
desObj.Padding = PaddingMode.PKCS7;
System.IO.MemoryStream mS = new System.IO.MemoryStream();
CryptoStream cS = new CryptoStream(mS, desObj.CreateEncryptor(), CryptoStreamMode.Write);
cS.Write(pBytes, 0, pBytes.Length);
cS.Close();
cBytes = mS.ToArray();
mS.Close();
richTextBox1.Text = Encoding.ASCII.GetString(cBytes);

These are the local variables i use:

string cData;
byte[] cBytes;
byte[] pBytes;
byte[] pBytes2;
byte[] pKey;
SymmetricAlgorithm desObj;#

and i'm doing this when the form is initialized:

desObj = Rijndael.Create();

So i know that the Key must be divisible by 8. So i tried to extend the Key i enter in the textbox so that the result is divisibnle by 8, so f.e. if the entered string is "test" it will extend this string to "test1234".

Error message:

An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional information: The given key has an invalid size for this algorithm

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
SyxDuLappen
  • 59
  • 2
  • 8
  • 1
    What error *occures here*? – Sinatr Feb 10 '16 at 11:14
  • An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional information: The given key has an invalid size for this algorithm . – SyxDuLappen Feb 10 '16 at 11:15
  • 1
    AES in .NET supports key sizes of 128, 192, or 256 bit, not every length that is divisible by 8. – Jens Feb 10 '16 at 11:19
  • how about 8 & 16Bit!? – SyxDuLappen Feb 10 '16 at 11:20
  • 1
    Don't just encode a password and use it as a key. Use a key derivation function to transform the password to a key. Basically hashing the password and adding a salt. .NET has an implementation of PBKDF2 in the `Rfc2898DeriveBytes` class. – Jens Feb 10 '16 at 11:23
  • I'm not really sure what you mean with "8 & 16Bit". You might want to take a look at the following example for using Rijndael in C#: http://stackoverflow.com/questions/202011/encrypt-and-decrypt-a-string as there are a lot of improvements to be made with how you use the "key" and your streams. – Mark Feb 10 '16 at 11:25

0 Answers0