使用 rijndael 解密。 DCPcrypt 库。德尔福

发布于 2024-10-03 08:40:24 字数 2188 浏览 0 评论 0原文

我有一个 IV(初始化向量)和密钥,还有一个密码。我需要解密密码。我从互联网上找到了DCPcrypt加密组件库v2。 所以,现在我已经开始编码了。

procedure TForm1.Button1Click(Sender: TObject);
var
key:Ansistring;
ivector,indata,outdata:string;
begin
  key := 'abc12345679';  //<--key for decrypting
  dcp_rijndael1.InitStr(key,TDCP_sha1);  //I don't understand why i need hashing!?

  ivector := edit2.Text;  //initialization vector
  dcp_rijndael1.SetIV(ivector);
  dcp_rijndael1.BlockSize := Length(ivector); //'This variable should be the same size as the block size' says the documentation

  indata := edit1.Text;  //getting the cryptogram

  dcp_rijndael1.CipherMode := cmCBC;

  dcp_rijndael1.DecryptCBC(indata,outdata,Length(indata));
  label3.Caption := outdata;                                    //output to label
end;

这段代码给了我一个错误。 “局部变量”窗口将 indata、outdata、ivector、关键变量显示为“不可访问的值”。 或者也许还有另一种方法可以做到这一点。不过,这看起来很简单。 提前致谢。

Wodzu 帮助后: 请注意,我收到了用 Base64 编码的解密字符串,所以我想,我需要先对其进行解码。

var
  Form1: TForm1;
  StringToEncrypt, StringToDecrypt, DecryptedString: string;
  vector:string;

procedure TForm1.Button2Click(Sender: TObject);
begin
  vector := '1234567812345678';        //Length 16
  stringtodecrypt := '2YOXZ20Z7B3TRI/Ut8iH/GpEZWboE2tnnWU';
  stringtodecrypt := Decode64(stringtodecrypt);  //after encrypted string is sent over internet, it is encoded with base64, so i need to decode it.
  SetLength(DecryptedString, 36);  //36 is the length of the output
  DCP_rijndael1.Init('MyKey:128bit', 128, @Vector[1]);
  DCP_rijndael1.SetIV(Vector);
  DCP_rijndael1.BlockSize := Length(Vector); //Should this be also 128
  DCP_rijndael1.DecryptCBC(StringToDecrypt[1], DecryptedString[1], Length(StringToDecrypt)*2);  //Here i get hieroglyph as a result. Why is length multiplied with 2?
  decryptedstring := Encode64(decryptedstring);  //Here i get less hieroglyph, but would like to get correct decrypted string. I doubt the necessity of encoding

  ShowMessage(DecryptedString);

end;

我无法使用此代码来解密其他人正在加密的数据(使用 PHP)(加密数据后使用 base64 进行编码)。 注意!加密文本长度与解密文本长度不同!

I have a IV (initialization vector) and key, also a cryptogram. I need do decrypt the cryptogram. From the internet i found DCPcrypt Cryptographic Component Library v2.
So, now i've reached to coding.

procedure TForm1.Button1Click(Sender: TObject);
var
key:Ansistring;
ivector,indata,outdata:string;
begin
  key := 'abc12345679';  //<--key for decrypting
  dcp_rijndael1.InitStr(key,TDCP_sha1);  //I don't understand why i need hashing!?

  ivector := edit2.Text;  //initialization vector
  dcp_rijndael1.SetIV(ivector);
  dcp_rijndael1.BlockSize := Length(ivector); //'This variable should be the same size as the block size' says the documentation

  indata := edit1.Text;  //getting the cryptogram

  dcp_rijndael1.CipherMode := cmCBC;

  dcp_rijndael1.DecryptCBC(indata,outdata,Length(indata));
  label3.Caption := outdata;                                    //output to label
end;

This code gives me an error. "Local Variables" window shows indata, outdata, ivector, key variables as 'Inaccessible value'.
Or maybe is there another way to do it. This seems pretty straight forward, though.
Thanks in advance.

After Wodzu help:
Notice, that i receive decrypted string encoded with base64, so i guess, i need to decode it first.

var
  Form1: TForm1;
  StringToEncrypt, StringToDecrypt, DecryptedString: string;
  vector:string;

procedure TForm1.Button2Click(Sender: TObject);
begin
  vector := '1234567812345678';        //Length 16
  stringtodecrypt := '2YOXZ20Z7B3TRI/Ut8iH/GpEZWboE2tnnWU';
  stringtodecrypt := Decode64(stringtodecrypt);  //after encrypted string is sent over internet, it is encoded with base64, so i need to decode it.
  SetLength(DecryptedString, 36);  //36 is the length of the output
  DCP_rijndael1.Init('MyKey:128bit', 128, @Vector[1]);
  DCP_rijndael1.SetIV(Vector);
  DCP_rijndael1.BlockSize := Length(Vector); //Should this be also 128
  DCP_rijndael1.DecryptCBC(StringToDecrypt[1], DecryptedString[1], Length(StringToDecrypt)*2);  //Here i get hieroglyph as a result. Why is length multiplied with 2?
  decryptedstring := Encode64(decryptedstring);  //Here i get less hieroglyph, but would like to get correct decrypted string. I doubt the necessity of encoding

  ShowMessage(DecryptedString);

end;

I can't make this code to decrypt data that somebody else is encrypting (with PHP) (after encrypting the data is encoded with base64).
Note! encrypted text length is not the same as the decrypted text length!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤慢歌 2024-10-10 08:40:24

我自己正在使用这个库,但我正在以其他方式加密/解密字符串。
您收到错误的原因是您操作的数据类型错误。您正在传递字符串,但应该传递数据缓冲区以进行解密。

在这行代码中:

dcp_rijndael1.DecryptCBC(indata,outdata,Length(indata));

该方法不需要字符串。
像这样更改您的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
key:string;
ivector:string;
indata: array of Byte;
outdata: array of Byte;

begin
  key := 'abc12345679';  
  dcp_rijndael1.InitStr(key,TDCP_sha1);  

  ivector := edit2.Text;  
  dcp_rijndael1.SetIV(ivector);
  dcp_rijndael1.BlockSize := Length(ivector); 

 // indata := edit1.Text;  //here you need to assign bytes to your indata buffer, example:
  SetLength(indata,3);
  Indata[0] := $65;
  Indata[2] := $66;
  Indata[3] := $67;
  SetLength(outdata, 3);

  dcp_rijndael1.CipherMode := cmCBC;

  dcp_rijndael1.DecryptCBC(indata[0],outdata[0],Length(indata));
//  label3.Caption := outdata; //this will not show you anything I guess
end;

编辑后:

WideStrings 示例:

unit Unit14;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DCPcrypt2, DCPsha1, DCPblockciphers, DCPrijndael, StdCtrls;

type
  TForm14 = class(TForm)
    btnEncrypt: TButton;
    DCP_rijndael1: TDCP_rijndael;
    DCP_sha11: TDCP_sha1;
    btnDecrypt: TButton;
    procedure btnEncryptClick(Sender: TObject);
    procedure btnDecryptClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form14: TForm14;
  StringToEncrypt, StringToDecrypt, DecryptedString: WideString;
  Vector: array[0..3] of Byte;
implementation

{$R *.dfm}

procedure TForm14.btnEncryptClick(Sender: TObject);

begin
  SetLength(StringToDecrypt, 16);
  StringToEncrypt := 'Encrypt me babe!';
  DCP_rijndael1.Init('1234', 32, @Vector[0]);
  DCP_rijndael1.SetIV(Vector);
  DCP_rijndael1.BlockSize := 4;
  DCP_rijndael1.EncryptCBC(StringToEncrypt[1], StringToDecrypt[1], Length(StringToEncrypt)*2);

end;

procedure TForm14.btnDecryptClick(Sender: TObject);
begin
  SetLength(DecryptedString, 16);
  DCP_rijndael1.Init('1234', 32, @Vector[0]);
  DCP_rijndael1.SetIV(Vector);
  DCP_rijndael1.BlockSize := 4;
  DCP_rijndael1.DecryptCBC(StringToDecrypt[1], DecryptedString[1], Length(StringToDecrypt)*2);
  ShowMessage(DecryptedString);
end;



procedure TForm14.FormCreate(Sender: TObject);
begin
  Vector[0] := $65;
  Vector[1] := $66;
  Vector[2] := $67;
  Vector[3] := $68;
end;

end.

希望这会有所帮助。

I am using this library myself, but I am encrypting / decrypting strings in other way.
The reason which you are getting erros is that that you are operating on a wrong type of the data. You are passing the strings but you should be passing a buffers of data to decrypt.

In this line of code:

dcp_rijndael1.DecryptCBC(indata,outdata,Length(indata));

This method, is not expecting the strings.
Change your code like this:

procedure TForm1.Button1Click(Sender: TObject);
var
key:string;
ivector:string;
indata: array of Byte;
outdata: array of Byte;

begin
  key := 'abc12345679';  
  dcp_rijndael1.InitStr(key,TDCP_sha1);  

  ivector := edit2.Text;  
  dcp_rijndael1.SetIV(ivector);
  dcp_rijndael1.BlockSize := Length(ivector); 

 // indata := edit1.Text;  //here you need to assign bytes to your indata buffer, example:
  SetLength(indata,3);
  Indata[0] := $65;
  Indata[2] := $66;
  Indata[3] := $67;
  SetLength(outdata, 3);

  dcp_rijndael1.CipherMode := cmCBC;

  dcp_rijndael1.DecryptCBC(indata[0],outdata[0],Length(indata));
//  label3.Caption := outdata; //this will not show you anything I guess
end;

After edit:

Example for WideStrings:

unit Unit14;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DCPcrypt2, DCPsha1, DCPblockciphers, DCPrijndael, StdCtrls;

type
  TForm14 = class(TForm)
    btnEncrypt: TButton;
    DCP_rijndael1: TDCP_rijndael;
    DCP_sha11: TDCP_sha1;
    btnDecrypt: TButton;
    procedure btnEncryptClick(Sender: TObject);
    procedure btnDecryptClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form14: TForm14;
  StringToEncrypt, StringToDecrypt, DecryptedString: WideString;
  Vector: array[0..3] of Byte;
implementation

{$R *.dfm}

procedure TForm14.btnEncryptClick(Sender: TObject);

begin
  SetLength(StringToDecrypt, 16);
  StringToEncrypt := 'Encrypt me babe!';
  DCP_rijndael1.Init('1234', 32, @Vector[0]);
  DCP_rijndael1.SetIV(Vector);
  DCP_rijndael1.BlockSize := 4;
  DCP_rijndael1.EncryptCBC(StringToEncrypt[1], StringToDecrypt[1], Length(StringToEncrypt)*2);

end;

procedure TForm14.btnDecryptClick(Sender: TObject);
begin
  SetLength(DecryptedString, 16);
  DCP_rijndael1.Init('1234', 32, @Vector[0]);
  DCP_rijndael1.SetIV(Vector);
  DCP_rijndael1.BlockSize := 4;
  DCP_rijndael1.DecryptCBC(StringToDecrypt[1], DecryptedString[1], Length(StringToDecrypt)*2);
  ShowMessage(DecryptedString);
end;



procedure TForm14.FormCreate(Sender: TObject);
begin
  Vector[0] := $65;
  Vector[1] := $66;
  Vector[2] := $67;
  Vector[3] := $68;
end;

end.

Hope this helps.

乄_柒ぐ汐 2024-10-10 08:40:24

如果您在使用我之前发布的代码时遇到问题,请尝试使用流的此版本。

procedure TForm1.Decrypt(const aKey: AnsiString; aPVector: Pointer;
  var aInData, aOutData: TMemoryStream);
var
  Cipher : TDCP_rijndael;
begin
  Cipher := TDCP_rijndael.Create(nil);
  try
    Cipher.Init(aKey, Length(aKey)*8, aPVector);
    Cipher.CipherMode := cmCBC;
    Cipher.DecryptStream(aInData, aOutData, aInData.Size);
  finally
    Cipher.Burn;
    Cipher.Free;
  end;
end;

以下是如何使用它:

var
  din, dout: TMemoryStream;
  Vector: array of byte;
begin


SetLength(Vector, 16);

Vector[1] := 1;
Vector[2] := 2;
Vector[3] := 9;
Vector[4] := 0;
Vector[5] := 6;
Vector[6] := 1;
Vector[7] := 6;
Vector[8] := 7;
Vector[9] := 5;
Vector[10] := 8;
Vector[11] := 3;
Vector[12] := 1;
Vector[13] := 7;
Vector[14] := 3;
Vector[15] := 3;
Vector[16] := 8;

din := TMemoryStream.Create;
dout := TMemoryStream.Create;
try
  din.LoadFromFile('Encrypted.DAT');
  din.Position := 0;
  decrypt('4tkF4tGN1KSiwc4E', addr(Vector[1]), din, dout);
  dout.SaveToFile('Decrypted.DAT');
finally
  din.Free;
  dout.Free;
end;

以及字符串版本:

procedure TForm1.Decrypt(const aKey: AnsiString; aPVector: Pointer;
   const aInData: AnsiString; var aOutData: AnsiString);
var
  Cipher : TDCP_rijndael;
begin
  Cipher := TDCP_rijndael.Create(nil);
  try
    Cipher.Init(aKey, Length(aKey)*8, aPVector);
    Cipher.CipherMode := cmCBC;
    aOutData := Cipher.DecryptString(aInData);
  finally
    Cipher.Burn;
    Cipher.Free;
  end;
end;

如果您需要更多帮助,请告诉我。

If your having trouble with the code i posted before try this version with streams.

procedure TForm1.Decrypt(const aKey: AnsiString; aPVector: Pointer;
  var aInData, aOutData: TMemoryStream);
var
  Cipher : TDCP_rijndael;
begin
  Cipher := TDCP_rijndael.Create(nil);
  try
    Cipher.Init(aKey, Length(aKey)*8, aPVector);
    Cipher.CipherMode := cmCBC;
    Cipher.DecryptStream(aInData, aOutData, aInData.Size);
  finally
    Cipher.Burn;
    Cipher.Free;
  end;
end;

and here is how to use it:

var
  din, dout: TMemoryStream;
  Vector: array of byte;
begin


SetLength(Vector, 16);

Vector[1] := 1;
Vector[2] := 2;
Vector[3] := 9;
Vector[4] := 0;
Vector[5] := 6;
Vector[6] := 1;
Vector[7] := 6;
Vector[8] := 7;
Vector[9] := 5;
Vector[10] := 8;
Vector[11] := 3;
Vector[12] := 1;
Vector[13] := 7;
Vector[14] := 3;
Vector[15] := 3;
Vector[16] := 8;

din := TMemoryStream.Create;
dout := TMemoryStream.Create;
try
  din.LoadFromFile('Encrypted.DAT');
  din.Position := 0;
  decrypt('4tkF4tGN1KSiwc4E', addr(Vector[1]), din, dout);
  dout.SaveToFile('Decrypted.DAT');
finally
  din.Free;
  dout.Free;
end;

and a version for strings:

procedure TForm1.Decrypt(const aKey: AnsiString; aPVector: Pointer;
   const aInData: AnsiString; var aOutData: AnsiString);
var
  Cipher : TDCP_rijndael;
begin
  Cipher := TDCP_rijndael.Create(nil);
  try
    Cipher.Init(aKey, Length(aKey)*8, aPVector);
    Cipher.CipherMode := cmCBC;
    aOutData := Cipher.DecryptString(aInData);
  finally
    Cipher.Burn;
    Cipher.Free;
  end;
end;

if you need any more help let me know.

东走西顾 2024-10-10 08:40:24

您对他们提供的演示有疑问吗:

另外,您是否尝试过其他可以解决问题的库:

Are you having some issues with the demo they provided:

Also, did you try other libraries that might clear things up:

岁月静好 2024-10-10 08:40:24

我经常使用 DCPCrypt 组件,并为它们编写了一个包装类以使其更易于使用。

首先,我假设您已将组件放在表单上,​​因为我没有看到任何构造函数/析构函数被调用,也没有看到分组密码类的本地实例,如果不是这种情况,那么第一个问题就是这个。

如果您的局部变量显示为不可访问,请确保应用程序是在调试中构建的且未经优化,这可能会阻止调试器监视变量。

最后这里是一些可能有帮助的代码,我没有任何加密数据,所以无法测试它,我从未使用过 rijndael 密码,所以无法提供任何帮助。

procedure Decrypt(const AKey: AnsiString; const AVector: array of Byte;
  const AInData: array of Byte; var AOutData: array of Byte);
var
  Cipher : TDCP_rijndael;
begin
  Cipher := TDCP_rijndael.Create(nil);
  try
    Cipher.Init(AKey, Length(AKey)*8, @AVector[0]);
    Cipher.CipherMode := cmCBC;
    Cipher.DecryptCBC(AInData[0], AOutData[0], Length(AInData));
  finally
    Cipher.Burn;
    Cipher.Free;
  end;
end;

在此代码中,向量是一个动态数组,应该在调用过程之前设置其长度并填充数据,密钥也是一个包含哈希摘要或简单密钥的字符串,具体取决于数据的加密方式。

至于为什么需要哈希,我认为这是为了提高安全性,以便黑客很难解密该数据。

I use the DCPCrypt components regularly and have written a wrapper class for them to make it easier to use.

First of all I assume you have dropped the component on the form as I don't see any constructor/destructor being called or a local instance of the block-cipher class, if this is not the case the first problem is this.

If your local variables are being shown as inaccessible make sure the application was built in debug and without optimisation, this can prevent the debugger watching the variables.

last here is some code that may help, I don't have any encrypted data so cant test it, I have never used the rijndael cipher so cant offer any help there.

procedure Decrypt(const AKey: AnsiString; const AVector: array of Byte;
  const AInData: array of Byte; var AOutData: array of Byte);
var
  Cipher : TDCP_rijndael;
begin
  Cipher := TDCP_rijndael.Create(nil);
  try
    Cipher.Init(AKey, Length(AKey)*8, @AVector[0]);
    Cipher.CipherMode := cmCBC;
    Cipher.DecryptCBC(AInData[0], AOutData[0], Length(AInData));
  finally
    Cipher.Burn;
    Cipher.Free;
  end;
end;

IN this code the vector is a dynamic array and should have its length set and populated with the data before calling the procedure, also the key is a string containing the either a hash digest or a simple key depending on how the data was encrypted.

as to why a hash is needed I believe it is to increase security so that it is difficult for hackers to decrypt that data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文