私はCrypto APIを使用してネイティブコードでデータを暗号化し、RC2アルゴリズムとSHAを使用してキーを作成する.NET(C#)コードでこれを復号化しました。CryptAPIネイティブInterop with .NETコード
これは、ネイティブコード(この場合はデルファイ)である:今、私は、新しい、より良いAES暗号化を使用する場合に使用したい
CspParameters cspParams = new CspParameters(1);
PasswordDeriveBytes deriveBytes = new PasswordDeriveBytes(aPassword, null, "SHA-1", 1, cspParams);
byte[] rgbIV = new byte[8];
byte[] key = deriveBytes.CryptDeriveKey("RC2", "SHA1", 0, rgbIV);
var provider = new RC2CryptoServiceProvider();
provider.Key = key;
provider.IV = rgbIV;
ICryptoTransform transform = provider.CreateDecryptor();
byte[] decyptedBlob = transform.TransformFinalBlock(arData, 0, arData.Length);
:.NETコード
// Get handle to CSP
If Not CryptAcquireContext(hCryptProv, nil, nil, PROV_RSA_FULL, 0) Then
If Not CryptAcquireContext(hCryptProv, nil, nil, PROV_RSA_FULL, CRYPT_NEWKEYSET) Then
ShowMessage('CryptAcquireContext '+IntToStr(GetLastError()));
// Create a hash object
If Not CryptCreateHash(hCryptProv, CALG_SHA, 0, 0, hHash) Then
ShowMessage('CryptCreateHash '+IntToStr(GetLastError()));
// Hash the password
If Not CryptHashData(hHash,PByte(as_password), Length(as_password), 0) Then
ShowMessage('CryptHashData '+IntToStr(GetLastError()));
// Derive a session key from the hash object
If Not CryptDeriveKey(hCryptProv, CALG_RC2, hHash, CRYPT_EXPORTABLE, hKey) Then
ShowMessage('CryptDeriveKey '+IntToStr(GetLastError()));
// allocate buffer space
lul_datalen := Length(ablob_data);
lblob_buffer := ablob_data + ' ';
lul_buflen := Length(lblob_buffer);
If ab_encrypt Then
// Encrypt data
If CryptEncrypt(hKey, 0, True, 0, PByte(lblob_buffer), lul_datalen, lul_buflen) Then
lblob_value := Copy(lblob_buffer, 1, lul_datalen)
else
ShowMessage('CryptEncrypt '+IntToStr(GetLastError()))
Else
// Decrypt data
If CryptDecrypt(hKey, 0, True, 0, PByte(lblob_buffer), lul_datalen) Then
lblob_value := Copy(lblob_buffer, 1, lul_datalen)
Else
ShowMessage('CryptDecrypt '+IntToStr(GetLastError()));
// Destroy session key
If hKey > 0 Then
CryptDestroyKey(hKey);
// Destroy hash object
If hHash > 0 Then
CryptDestroyHash(hHash);
// Release CSP handle
If hCryptProv > 0 Then
CryptReleaseContext(hCryptProv, 0);
Result := lblob_value;
このしたがって、ネイティブコードではCALG_RC2
の代わりにの代わりにPROV_RSA_AES
、CALG_SHA
およびCALG_AES_256
の代わりにCALG_SHA_256
を使用したいと考えています。それはネイティブサイトで正常に動作します。
しかし、私はそれを.NETサイトで動作させることはできません。もちろん、私はRC2CryptoServiceProviderをAESCryptoServiceProviderに変更する必要があり、CspParametersを1ではなく24で初期化する必要があります。私の問題はPasswordDerivedBytesの操作方法です。必要なパラメータ値は何ですか?
ありがとうございます。
これをAESで動作させる方法の解決策はありますか?ところで、あなたのコード例に感謝します。私はDelphi XEとVS 2010 w/RC2でそれを私のために働かせました。 (しかし、AESを使ってこれを得るのが大好きです。) – Troy
気にしないでください。私はそれを考え出した。私は互換性のあるC#とDelphiの例をここに掲載しました:http://stackoverflow.com/questions/9188045/how-to-aes-128-encrypt-a-string-using-a-password-in-delphi-and-decrypt- in-c – Troy