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?

8
  • 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. Commented Sep 22, 2013 at 15:26
  • 1
    There are plenty of Rijndael implementations. Find one and use it. Commented Sep 22, 2013 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? Commented Sep 22, 2013 at 17:48
  • Please put the details in the question and not comments. Edit the question. Commented Sep 22, 2013 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? Commented Sep 22, 2013 at 21:25

1 Answer 1

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.

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

Comments

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.