使用 rijndael 解密。 DCPcrypt 库。德尔福
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我自己正在使用这个库,但我正在以其他方式加密/解密字符串。
您收到错误的原因是您操作的数据类型错误。您正在传递字符串,但应该传递数据缓冲区以进行解密。
在这行代码中:
该方法不需要字符串。
像这样更改您的代码:
编辑后:
WideStrings 示例:
希望这会有所帮助。
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:
This method, is not expecting the strings.
Change your code like this:
After edit:
Example for WideStrings:
Hope this helps.
如果您在使用我之前发布的代码时遇到问题,请尝试使用流的此版本。
以下是如何使用它:
以及字符串版本:
如果您需要更多帮助,请告诉我。
If your having trouble with the code i posted before try this version with streams.
and here is how to use it:
and a version for strings:
if you need any more help let me know.
您对他们提供的演示有疑问吗:
另外,您是否尝试过其他可以解决问题的库:
Are you having some issues with the demo they provided:
Also, did you try other libraries that might clear things up:
我经常使用 DCPCrypt 组件,并为它们编写了一个包装类以使其更易于使用。
首先,我假设您已将组件放在表单上,因为我没有看到任何构造函数/析构函数被调用,也没有看到分组密码类的本地实例,如果不是这种情况,那么第一个问题就是这个。
如果您的局部变量显示为不可访问,请确保应用程序是在调试中构建的且未经优化,这可能会阻止调试器监视变量。
最后这里是一些可能有帮助的代码,我没有任何加密数据,所以无法测试它,我从未使用过 rijndael 密码,所以无法提供任何帮助。
在此代码中,向量是一个动态数组,应该在调用过程之前设置其长度并填充数据,密钥也是一个包含哈希摘要或简单密钥的字符串,具体取决于数据的加密方式。
至于为什么需要哈希,我认为这是为了提高安全性,以便黑客很难解密该数据。
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.
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.