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;
}
あらゆる種類の魂が私にこれに手を与えることができる私は、私は本当に感謝されると思います。私は数日間、これに対して頭を叩いています。
ありがとうございます。
私見を...そして、我々の[SynCrypto.pas](https://github.com /synopse/mORMot/blob/master/SynCrypto.pas)オープンソースユニットはDelphi 7と完全に連携しています。PKCS7パディングを使用しているC#からhttp://synopse.info/forum/viewtopic.php?id=3395を参照してください。 –
@ ArnaudBouchez + 。まったく。 3DESはすでに20年間は安全ではありません。 –
それは素晴らしいですが、実際に私の呼び出しではありません(私はオンライン支払いシステムのためにシステムを適応させる必要があります)。 SHA256でHMACを使用してフォームを送信する必要があります。彼らは、JSONでフォームを準備し、Base64でエンコードし、トランザクション固有のキーを作成し、エンコードされたフォームとトランザクション固有のキーに基づいてSHA256の署名を追加するよう依頼します。そのキーは、私たちの秘密のパスワードとトランザクションIDを3DESで暗号化して計算されます。これは私が立ち往生している場所です。 – Tarrakis