1

I've got this C# code to encrypt/decrypt strings:

private static byte[] EncryptString(byte[] clearText, byte[] Key, byte[] IV)
{
    MemoryStream ms = new MemoryStream();
    Rijndael alg = Rijndael.Create();
    alg.Key = Key;
    alg.IV = IV;
    CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
    cs.Write(clearText, 0, clearText.Length);
    cs.Close();
    byte[] encryptedData = ms.ToArray();
    return encryptedData;
}

public static string EncryptString(string clearText, string Password)
{
    byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x74, 0x68, 0x69, 0x73, 0x69, 0x61, 0x74, 0x65, 0x73, 0x74 });
    byte[] encryptedData = EncryptString(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
    return Convert.ToBase64String(encryptedData);
}

private static byte[] DecryptString(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;
}

public static string DecryptString(string cipherText, string Password)
{
    if (!string.IsNullOrEmpty(cipherText))
    {
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x74, 0x68, 0x69, 0x73, 0x69, 0x61, 0x74, 0x65, 0x73, 0x74 });
        byte[] decryptedData = DecryptString(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        return System.Text.Encoding.Unicode.GetString(decryptedData);
    }
    else
    {
        return "";
    }
}

Encrypted strings should be stored locally in the registry or a file. A Delphi application has to have access to this strings too. Please note that, for some reason, it is not possible to outsource the encryption/decryption code to a DLL.

My Problem is that i am not able to produce "PasswordDerivedBytes" in Delphi. Can somebody please give me a hint how that can work?

Action Heinz
  • 722
  • 10
  • 23
  • 3
    Unfortunately, StackOverflow isn't here to convert your code from language to language. You need to do your own research and do this yourself, and when you have a specific issue, ask about that specific issue. – Jerry Dodge Sep 22 '13 at 15:26
  • 1
    There are plenty of Rijndael implementations. Find one and use it. – David Heffernan Sep 22 '13 at 16:27
  • 1
    I'm sorry for asking my question in an improper way. Yes you are right, you can have the impression that i want a conversation of my code from C# to Delphi but that was not my intention. Please let me clarify my question: my special problem is that i'm not able to get the counterpart of "PasswordDerivedBytes" in Delphi. Therefore i'm not able to get the right result in Delphi. Can you give me a hint how to build PasswordDerivedBytes in Delphi? – Action Heinz Sep 22 '13 at 17:48
  • Please put the details in the question and not comments. Edit the question. – David Heffernan Sep 22 '13 at 18:30
  • `PasswordDerivedBytes` must be a type which is proprietary to the code you posted, but your code doesn't include this. Are you sure this is all the code? – Jerry Dodge Sep 22 '13 at 21:25
  • A Google search nets [this](http://msdn.microsoft.com/en-us/library/system.security.cryptography.passwordderivebytes.aspx) and the call is also documented [here](http://stackoverflow.com/questions/9231754/c-sharp-passwordderivebytes-confusion) for what it's worth. This is probably the code that is being called. It should be noted that the call is deprecated and [Rfc2898DeriveBytes](http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx) seems to be favored. – Glenn1234 Sep 23 '13 at 03:28
  • Though, if you are trying to duplicate this in straight Delphi as opposed to .NET, try something like [this](http://stackoverflow.com/questions/14411975/simple-code-to-encrypt-an-ini-file-string-using-a-password/14462455#14462455) instead of trying to reinvent the wheel. – Glenn1234 Sep 23 '13 at 03:29
  • Check this [SO reply](http://stackoverflow.com/a/18016448/197962) for a (fairly rough) PHP implementation of PBKDF1 / PasswordDeriveBytes that works against MS / C# encryption. I know it's not Delphi but I don't think it's rocket science to translate it. – shunty Sep 23 '13 at 08:33

1 Answers1

0

I would suggest to you to use the Delphi Encryption Compedium. It has almost anything you need cryptowise and hase some examples which mostly do what you want to do.