Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I wanna do encryption and decryption of a string in C# Visual Studio 2005. I did some search on google and got a code in VB.Net, and I converted it to C#. But its returning this string while encrypting... "Specified key is not a valid size for this algorithm.". Can anybody help me please. is this the right way in Visual Studio 2005 ?
Here is the code...
C#
public static string Decrypt(string encryptedText)
      {
          byte[] rgbIV = { 0x21, 0x43, 0x56, 0x87, 0x10, 0xfd, 0xea, 0x1c };

          try
          {
              byte[] key = Encoding.UTF8.GetBytes("A0D1X0Q");
              DESCryptoServiceProvider des = new DESCryptoServiceProvider();
              byte[] inputByteArray = Convert.FromBase64String(encryptedText.Replace(' ', '+'));
              MemoryStream ms = new MemoryStream();
              CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, rgbIV), CryptoStreamMode.Write);
              cs.Write(inputByteArray, 0, inputByteArray.Length);
              cs.FlushFinalBlock();
              Encoding encoding = Encoding.UTF8;
              return encoding.GetString(ms.ToArray());
          }
          catch (Exception e)
          {
              return e.Message;
          }
      }
      public static string Encrypt(string stringToEncrypt)
      {
          byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
          byte[] rgbIV = { 0x21, 0x43, 0x56, 0x87, 0x10, 0xfd, 0xea, 0x1c };
          try
          {
              byte[] key = Encoding.UTF8.GetBytes("A0D1X0Q");
              DESCryptoServiceProvider des = new DESCryptoServiceProvider();
              MemoryStream ms = new MemoryStream();
              CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);
              cs.Write(inputByteArray, 0, inputByteArray.Length);
              cs.FlushFinalBlock();
              return Convert.ToBase64String(ms.ToArray());
          }
          catch (Exception e)
          {
              return e.Message;
          }
      }
Posted
Updated 12-Feb-14 20:54pm
v2

1 solution

error description says what the error is.
Quote:
"Specified key is not a valid size for this algorithm."


let's check key which is "A0D1X0Q" which has the length of 7.

you can find key sizes here:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.legalkeysizes%28v=vs.110%29.aspx[^]

msdn says the key size for DES algorithm is 64bits=8 bytes which is not your key size.
 
Share this answer
 
v3
Comments
Yesudasan Moses 13-Feb-14 3:30am    
Yea,, this solved my issue... Thankyou...
But How can I use 128, 256 bit encryption ?
Vedat Ozan Oner 13-Feb-14 3:36am    
use other algorithms. the link that I just provided contains a list of the algorithms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900