3

i am trying to decrypt one string in c# encrypted in Delphi with Cipher1 3.0, Part I from Delphi Encryption Compendium. I use TCipher_Rijndael.

string that i encrypt is : this-is-a-test-example

password: pass

encrypted values is : iKBC8kX4ZEk4A1pCj6jwEegqjpxhqw==

When i try to decrypt this in c# i recive error: Length of the data to decrypt is invalid.

Did anyone have the same problem, and what is a solution?

Here is a decrypt method in c#:

public static byte[] Decrypt(byte[] cipherData,
                                byte[] Key, byte[] IV)
    {

        MemoryStream ms = new MemoryStream();  
        Rijndael alg = Rijndael.Create();      
        alg.Key = Key;
        alg.IV = IV;
        CryptoStream cs = new CryptoStream(ms,
            alg.CreateDecryptor(), CryptoStreamMode.Write);           
        cs.Write(cipherData, 0, cipherData.Length);    
        cs.Close();
        byte[] decryptedData = ms.ToArray();
        return decryptedData;
    }

and here is encrypt code in Delphi:

with TCipher_Rijndael.Create('pass',  nil) do
      begin
        memo2.lines.add ( CodeString( 'this-is-a-test-example' , paEncode, fmtDEFAULT));
        Free;
      end;

Thanks.

10
  • 1
    This smells of encoding issues - how are you converting to string in both applications? Commented Apr 16, 2011 at 23:46
  • Are you decoding the string into a byte array before attempting to decrypt it? The encrypted string you entered is encoded with Base64 encoding -- the two equals signs at the end are a giveaway. So first you'll have to convert them to a byte array using Convert.FromBase64String. Commented Apr 16, 2011 at 23:54
  • Of course i convert string to byte array. Commented Apr 17, 2011 at 0:01
  • What is the encrypted result if C# encrypts it? and can C# decrypt its encrypted string? Commented Apr 17, 2011 at 0:04
  • 1
    @buda: The question here is, the c# encryption result is the same as Delphi result? how it is different? Answering this will help you and maybe us to find where's the problem. Commented Apr 17, 2011 at 9:24

4 Answers 4

5

You need to determine all of the details of how that value was encrypted:

  1. What block cipher mode of operation was used? ECB tends to be a default.
  2. What padding scheme was used? Perhaps no padding in your case.
  3. How was the key derived from the password? Perhaps with PBKDF2 or simply a MD5 hash.
  4. What was the initialization vector? Note that only some cipher modes require one.
  5. How was the output encoded? It appears to be Base64 encoding for you.

Only once you know exactly how it was encrypted can you reverse the process to properly decrypt it. You might want to try posting more code or details about how it was encrypted. Then someone might be able to determine how you need to go about decrypting it. I've seen this scenario with unknowns before and I managed to guess the details to find the answer. I tried a few common ways with your encrypted string, but I can't easily decrypt it without more details.

Sign up to request clarification or add additional context in comments.

Comments

3

You are saying that you can encrypt in C# and decrypt in C#. You can also encrypt in C# and decrypt in Delphi. This means that your C# encryption is definitely fine. That leaves just one possible culprit: Delphi encryption.

So do this:

  • Encrypt in C#.
  • Encrypt in Delphi.
  • Compare the outputs.

They can't possibly be the same. See in what way they differ. Is one Base64-encoded and the other just raw bytes? Is one padded with == and the other not? Do they use different Base64 variants?

1 Comment

If you also post both "encrypt" outputs here we'll have a better chance of helping you.
1

Try going the other way. i.e. encrypt the string in c# and see if it matches. See if your C# solution can even eat its own dogfood.

2 Comments

Everything is OK when i encrypt string in c# and decrypt in c#, it all works.
So how long is the encrypted string from c#?
0

My guess is that the Delphi encryption encodes the string as an Ansi string (one byte per character) and the C# encryption encodes the string as a Unicode UTF16 string (two bytes per character). If you are using Delphi 2007 and below this is almost certainly the case.

PS The length of the encrypted string in Delphi is also a bit of a giveaway - 32 characters for a 22 character raw string implies one byte per character - this is not the case for C#.

1 Comment

Make sure that the string encoding used by Delphi is UTF16 (the same as C#), or use Ansi encoding specifically in C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.