2016-10-13 21 views
-2

Delphi 7で文字列を暗号化する必要があります。DCPCrypt(3DES)を使用してDelphi 7で文字列を暗号化します

私は、ファイルを暗号化された例を学び、文字列のためにそれを適応させることを試みたが、私は無残に失敗し怖い...

これは私の関数である:

function Encrypt3DES(psString, psKey: string): string; 
var 
    lCipher:TDCP_3des; 
    CipherIV: array of byte;  // the initialisation vector (for chaining modes) 
    lHash:TDCP_sha256; 
    lHashDigest: array of byte; // the result of hashing the passphrase with the salt 
    Salt: array[0..7] of byte; // a random salt to help prevent precomputated attacks 
    i:integer; 
begin 
    lHash:=TDCP_sha256.Create(nil); 
    SetLength(lHashDigest,lHash.HashSize div 8); 
    for i := 0 to 7 do 
     Salt[i] := Random(256); // just fill the salt with random values (crypto secure PRNG would be better but not _really_ necessary) 

    //strmOutput.WriteBuffer(Salt,Sizeof(Salt)); // write out the salt so we can decrypt! ***I don't know what to do with this*** 

    lHash.Init; 
    lHash.Update(Salt[0],Sizeof(Salt)); // hash the salt 
    lHash.UpdateStr(psKey); // and the passphrase 
    lHash.Final(lHashDigest[0]);   // store the output in HashDigest 

    lCipher:=TDCP_3des.Create(nil); 
    //3DES is a block cipher, we need an initialisation vector 

    SetLength(CipherIV,TDCP_blockcipher(lCipher).BlockSize div 8); 
    for i := 0 to (Length(CipherIV) - 1) do 
     CipherIV[i] := Random(256);   // again just random values for the IV 

    //strmOutput.WriteBuffer(CipherIV[0],Length(CipherIV)); // write out the IV so we can decrypt! ***I don't know what to do with this*** 

    lCipher.Init(lHashDigest[0],TNeo.Min(lCipher.MaxKeySize,lHash.HashSize),CipherIV); // initialise the cipher with the hash as key 
    TDCP_blockcipher(lCipher).CipherMode := cmCBC; // use CBC chaining when encrypting 

    //lCipher.EncryptStream(strmInput,strmOutput,strmInput.Size); // encrypt the entire file 
    result:=lCipher.EncryptString(psString); 
    lCipher.Burn; // important! get rid of keying information 
    //strmInput.Free; 
    //strmOutput.Free; 
end; 

してください、クマ暗号化の仕組みについて私はまったく気づいていません。文字列を暗号化せずにバイナリを暗号化していることは分かっていますが、それをコードに変換する方法はわかりません。 私はそれを実行するたびに、私は別の結果を得ます(ランダムな値を使用すると普通です)が、別のサーバーに送信してチェックできるようにする必要があるかどうかわかりませんそこの完全性。

持ち運びにくいが、私のAPIのJava機能を与えたが、明らかに私はそれを使用することはできません。

public byte [] encrypt_3DES(final String claveHex, final String datos) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { 
    byte [] ciphertext = null; 
    // Crea la clave 
    DESedeKeySpec desKeySpec = new DESedeKeySpec(toByteArray(claveHex)); 
    SecretKey desKey = new SecretKeySpec(desKeySpec.getKey(), "DESede"); 
    // Crea un cifrador 
    Cipher desCipher = Cipher.getInstance("DESede/CBC/NoPadding"); 

    // Inicializa el cifrador para encriptar 
    desCipher.init(Cipher.ENCRYPT_MODE, desKey, new IvParameterSpec(IV)); 

    // Se añaden los 0 en bytes necesarios para que sea un múltiplo de 8 
    int numeroCerosNecesarios = 8 - (datos.length() % 8); 
    if (numeroCerosNecesarios == 8) { 
     numeroCerosNecesarios = 0; 
    } 
    ByteArrayOutputStream array = new ByteArrayOutputStream(); 
    array.write(datos.getBytes("UTF-8"), 0, datos.length()); 
    for (int i = 0; i < numeroCerosNecesarios; i++) { 
     array.write(0); 
    } 
    byte [] cleartext = array.toByteArray(); 
    // Encripta el texto 
    ciphertext = desCipher.doFinal(cleartext); 
    return ciphertext; 
} 

あらゆる種類の魂が私にこれに手を与えることができる私は、私は本当に感謝されると思います。私は数日間、これに対して頭を叩いています。

ありがとうございます。

+1

私見を...そして、我々の[SynCrypto.pas](https://github.com /synopse/mORMot/blob/master/SynCrypto.pas)オープンソースユニットはDelphi 7と完全に連携しています。PKCS7パディングを使用しているC#からhttp://synopse.info/forum/viewtopic.php?id=3395を参照してください。 –

+1

@ ArnaudBouchez + 。まったく。 3DESはすでに20年間は安全ではありません。 –

+0

それは素晴らしいですが、実際に私の呼び出しではありません(私はオンライン支払いシステムのためにシステムを適応させる必要があります)。 SHA256でHMACを使用してフォームを送信する必要があります。彼らは、JSONでフォームを準備し、Base64でエンコードし、トランザクション固有のキーを作成し、エンコードされたフォームとトランザクション固有のキーに基づいてSHA256の署名を追加するよう依頼します。そのキーは、私たちの秘密のパスワードとトランザクションIDを3DESで暗号化して計算されます。これは私が立ち往生している場所です。 – Tarrakis

答えて

1

この例は、私がガイドとして間違っDCPCrypt機能を使用していたことを後で分かった3DES https://sourceforge.net/p/tplockbox/wiki/Home/

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, 
    //lockbox units 
    LbCipher, LbClass, LbAsym, LbRSA, LbString; 

type 
    TForm1 = class(TForm) 
    edPlainText: TEdit; 
    edCipherText: TEdit; 
    btnEncryptString: TButton; 
    btnDescryptString: TButton; 
    procedure btnEncryptStringClick(Sender: TObject); 
    procedure btnDescryptStringClick(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

Function TripleDesEncrypt(const APlaintext, APassphrase: String): String; 
Var 
    Key128: TKey128; 
begin 
    GenerateLMDKey(Key128, SizeOf(Key128), APassphrase); 
    result := TripleDESEncryptStringEx(APlainText, Key128, True); 
End; 

Function TripleDesDecrypt(const ACipherText, APassphrase: String): String; 
Var 
    Key128: TKey128; 
begin 
    GenerateLMDKey(Key128, SizeOf(Key128), APassphrase); 
    Try 
    result := TripleDESEncryptStringEx(ACipherText, Key128, False); 
    Except 
    Result := ''; 
    End; 
End; 

procedure TForm1.btnEncryptStringClick(Sender: TObject); 
begin 
    edCipherText.text := TripleDesEncrypt(edPlainText.Text, 'SecretPassphrase'); 
end; 

procedure TForm1.btnDescryptStringClick(Sender: TObject); 
begin 
    edPlainText.text := TripleDesDecrypt(edCipherText.text, 'SecretPassphrase'); 
end; 

end. 
+0

ありがとう、ジョン。私は月曜日にそれを試し、あなたに知らせるでしょう。 – Tarrakis

+0

ありがとうジョン、それは完璧に働いた! – Tarrakis

0

を使用して文字列を暗号化/復号化するためのオープンソースTLockBoxライブラリを使用しています。 私は誰かがDCPCrypt使用する必要がある場合には、私が見つけた他のものを投稿します:AESは3DESに好まれるべき

function Encrypt3DES(psData, psKey: string): string; 
var 
    Cipher: TDCP_3des; 
begin 
    Cipher:= TDCP_3des.Create(nil); 
    Cipher.InitStr(psKey,TDCP_sha256);   // initialize the cipher with a hash of the passphrase 
    result:=Cipher.EncryptString(psData); 
    Cipher.Burn; 
    Cipher.Free; 
end; 
関連する問題