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

I am new for Cryptography. I want encrypt the object in windows application and same object we have decrypt in WCF service using RijndaelManage Algorithm. Here we can able to encrypt and decrypt in windows application. But unable to decrypt in wcf service.

I got error is :Padding is invalid and cannot be removed ..

What i have written in Windows application :
C#
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
       {
           // Check arguments.
           if (plainText == null || plainText.Length <= 0)
               throw new ArgumentNullException("plainText");
           if (Key == null || Key.Length <= 0)
               throw new ArgumentNullException("Key");
           if (IV == null || IV.Length <= 0)
               throw new ArgumentNullException("IV");
           byte[] encrypted;
           // Create an RijndaelManaged object
           // with the specified key and IV.
           using (RijndaelManaged rijAlg = new RijndaelManaged())
           {
               rijAlg.Key = Key;
               rijAlg.IV = IV;

               // Create a decrytor to perform the stream transform.
               ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

               // Create the streams used for encryption.
               using (MemoryStream msEncrypt = new MemoryStream())
               {
                   using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                   {
                       using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                       {

                           //Write all data to the stream.
                           swEncrypt.Write(plainText);
                       }
                       encrypted = msEncrypt.ToArray();
                   }
               }
           }


           // Return the encrypted bytes from the memory stream.
           return encrypted;

       }


What i have written in WCF Service :

C#
static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }



My service method is : objservice.registeremployee(plainText,key,iv);


Note: Is there any other way to resolve that issue. Beacause these functionality will go for live on this monday.
Kindly help me any one.
Posted
Updated 16-Dec-14 19:39pm
Comments
Kornfeld Eliyahu Peter 11-Dec-14 13:14pm    
There should be no difference between decrypt in windows or WCF application, so you have to explain us what went wrong...

1 solution

Try flushing your streams before converting them to arrays - it's quite possible you have data "waiting" to fill an encryption block that you aren't getting before you transfer it.
Here is the method I use:
C#
/// <summary>
/// Encrypts an incoming stream to the outgoing stream.
/// </summary>
/// <remarks>We do NOT close sIn or sOut.
/// Since these could be MemoryStreams, closing them would throw away the data!
/// It is good practice to decrypt to memory, not file!
/// We don't work with files bigger than an int full. It would be fairly easy to
/// modify this to block the I/O
/// We make the output bigger than it needs to be to flush  all the "real" data
/// to the output.
/// (This should not be necessary, but when decrypting to a MemoryStream you have
/// to close it to get the last block. Don't ask me why - ask MS.)</remarks>
/// <param name="sIn">Unencrypted data to encrypt</param>
/// <param name="sOut">Encrypted output. Note this will include a cyrptoheader block
/// and so will be larger than the input.</param>
public void Encrypt(Stream sIn, Stream sOut)
    {
    string Routine = "Encrypt: ";
    if (key == null)
        {
        throw new CryptoHelperException(Routine, EX_CRYPTO_NOKEY);
        }
    if (sIn.Length > int.MaxValue)
        {
        throw new CryptoHelperException(Routine, EX_CRYPTO_TOOBIG);
        }
    sOut.Write(CreateBlockHeader(sIn), 0, encAlg.BlockSize);
    int inputLength;
    byte[] abIn;
    switch ((CryptoSecurity) schema )
        {
        case CryptoSecurity.NoEncrypt:
            inputLength = (int) sIn.Length;
            abIn = new byte[inputLength];
            sIn.Read(abIn, 0, inputLength);
            sOut.Write(abIn, 0, inputLength);
            break;
        case CryptoSecurity.TripleDES:
            CryptoStream encrypt = new CryptoStream(sOut, encAlg.CreateEncryptor(), CryptoStreamMode.Write);
            inputLength = (int)sIn.Length + encAlg.BlockSize; // Make it too big!
            abIn = new byte[inputLength];
            sIn.Read(abIn, 0, inputLength);
            encrypt.Write(abIn, 0, inputLength);
            encrypt.FlushFinalBlock();
            key.Reset();
            break;
        default:
            throw new CryptoHelperException(Routine, EX_CRYPTO_BADSCHEMA);
        }
    if (sIn.CanSeek)
        {
        sIn.Seek(0, SeekOrigin.Begin);
        }
    if (sOut.CanSeek)
        {
        sOut.Seek(0, SeekOrigin.Begin);
        }
    }


It uses AES rather than Rijndael but the principle is the same.
 
Share this answer
 

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