Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use This Code To Encrypt tryed with the Key value as "abc" also in int valves like "123" so and so but geting dis error pls Help

My Error is "Specified key is not a valid size for this algorithm."

C#
public static string Encrypt(string input, string key)
      {
          byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
          TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
          tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
          tripleDES.Mode = CipherMode.ECB;
          tripleDES.Padding = PaddingMode.PKCS7;
          ICryptoTransform cTransform = tripleDES.CreateEncryptor();
          byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
          tripleDES.Clear();
          return Convert.ToBase64String(resultArray, 0, resultArray.Length);
      }


Help Pls
Posted
Updated 3-Jul-13 21:21pm
v2

 
Share this answer
 
Comments
usha C 4-Jul-13 3:31am    
public override void GenerateKey()

How to use dis method in this code i dont have idea pls help me
Richard MacCutchan 4-Jul-13 4:30am    
I would suggest by reading the documentation and trying some sample code.
 
Share this answer
 
Comments
usha C 4-Jul-13 3:56am    
Am geting dis error if i used my code in that method


"The method or operation is not implemented."

pls do explain me clearly Thank You...,
praks_1 4-Jul-13 4:13am    
post ur code
C#
public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV)
        {
            
                // Create a MemoryStream.
                MemoryStream mStream = new MemoryStream();

                // Create a CryptoStream using the MemoryStream  
                // and the passed key and initialization vector (IV).
                CryptoStream cStream = new CryptoStream(mStream,
                    new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
                    CryptoStreamMode.Write);

                // Convert the passed string to a byte array. 
                byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);

                // Write the byte array to the crypto stream and flush it.
                cStream.Write(toEncrypt, 0, toEncrypt.Length);
                cStream.FlushFinalBlock();

                // Get an array of bytes from the  
                // MemoryStream that holds the  
                // encrypted data. 
                byte[] ret = mStream.ToArray();

                // Close the streams.
                cStream.Close();
                mStream.Close();

                // Return the encrypted buffer. 
                return ret;
           
            

        }



Here is my code
 
Share this answer
 
v2
Comments
Dmitry Bond 29-Mar-23 9:19am    
Examples has no sense! Because I NEED TO ENCRYPT WITH MY KEY but not with the default key for this algorithm. But how to build own valid key - this is not explained. :-\

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