因此,我当前的任务包括获取给定的文本字符串(例如:ABC123),并使用LockBox3的EncryptString(源,目标)函数对其进行加密。我可以成功地加密字符串,并将输出保存到.txt文件中。
这个过程的下一步是使用LockBox3的EncryptFile(源,目标)函数获取包含我已经加密的字符串的.txt,并使用AES-128(与字符串加密相同,但使用diff密码)加密所述文件
基本上,我可以获得正确加密的字符串并输出到.txt文件。然后我请求用户获取.txt文件,并将其带入我的程序中。然后程序会尝试获取该文件并对其进行进一步加密。当我这样做时,我会得到一个要输出的文件。。然而,当我去解密所述文件时,生成的.txt不包含原始文本。。或任何与此相关的文本。我基本上对如何加密.txt文件感到困惑。有什么建议吗?如果这个问题/代码不够具体,我深表歉意。请让我知道还有什么,如果我需要澄清的话,以便更好地帮助你们理解我正在努力解决的问题!谢谢
第1版:
好的,各位,谢谢你们的建议。澄清:
我在解密过程中拥有的流稍后将被使用,这样在我解密文件后,我可以从中读取并解密剩余的加密(来自第一步)字符串。
进一步澄清:
我用于加密字符串的编解码器(Codec1)使用带有CBC的AES-128,标签为"0",AsymetricKeySize为1024(我很确定这与这种类型的加密无关,对吗?)我用于加密FILES(上面的Codec2)的编解码器具有相同的设置,但Codec1和Codec2的密码不同。基本上,我使用Codec1加密字符串并将其写入.txt,然后使用Codec2加密所述文件。。最终对其进行解密并使用流从所述文件中读取并再次使用Codec1对该字符串进行解密。
我的文件加密/解密代码:
字符串加密:
procedure TForm1.Button1Click(Sender: TObject);
begin
codec1.Password := WORD_1;
//Begin encryption
sPlainText := Serial_number.Number; //Store Serial Number of machine
codec1.EncryptString(sPlainText,CipherText); //Encrypt (base64)
listbox2.Clear;
listbox2.AddItem(Ciphertext, AnsiCipher);
end;
将加密字符串写入文件并保存:
saveDialog := TSaveDialog.Create(self);
saveDialog.Title := 'Choose location to save Authentication Code';
saveDialog.InitialDir := 'C:';
saveDialog.DefaultExt := '';
saveDialog.FilterIndex := 1;
saveDialog.Execute();
glb_fileName1 := saveDialog.FileName;
//open stream and write cipher to a .txt of chosen location
try
Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmOpenReadWrite);
except
Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmCreate);
end;
for k := 1 to (Length(CipherText)) do
buff[k] := byte(CipherText[k]);
ptr := @buff[1];
Stream.WriteBuffer(ptr^, Length(CipherText));
Stream.Free;
saveDialog.Free;
抓取.txt的位置进行文件加密:
procedure TForm1.Button4Click(Sender: TObject);
var
fileName : string;
holder_obj : TSerial_number;
begin
holder_obj := Tserial_number.Create;
listbox4.Clear;
if OpenTextFileDialog1.Execute() then
fileName := OpenTextFileDialog1.FileName;
listbox4.AddItem(filename, holder_obj);
end;
文件加密:
Codec2.Password := WORD_2;
sCrypt := glb_fileName1 + '_enc.txt';
Codec2.EncryptFile(glb_fileName1+'.txt', sCrypt);
抓取加密文件进行解密:
procedure TForm1.Button3Click(Sender: TObject);
var
holder_obj : TSerial_number;
begin
holder_obj := Tserial_number.Create;
listbox3.Clear;
if OpenTextFileDialog1.Execute() then
glb_fileName2 := OpenTextFileDialog1.FileName;
listbox3.AddItem(glb_filename2, holder_obj);
end;
文件解密(一旦我拥有解密的文件,就打开一个流从中读取,这样我就可以解密它所包含的加密字符串):
procedure TForm1.Button5Click(Sender: TObject);
var
saveDialog : TSaveDialog;
begin
saveDialog := TSaveDialog.Create(self);
saveDialog.Title := 'Choose location to save Decrypted Authentication Code';
saveDialog.InitialDir := 'C:';
saveDialog.DefaultExt := '';
saveDialog.Execute();
glb_fileName1:= saveDialog.FileName;
//open stream and write cipher to a .txt of chosen location
try
Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmOpenReadWrite);
except
Stream := TFileStream.Create(saveDialog.GetNamePath + saveDialog.FileName + '.txt', fmCreate);
end;
Stream.Free;
Codec2.Password := WORD_2;
Codec2.DecryptFile(glb_fileName2, saveDialog.FileName + '.txt');
saveDialog.Free;
end;
您提供的代码非常复杂,可以尝试查看出了什么问题。如果你只是想看看编码/解码是否有效,你应该只需要简单的代码,比如下面的代码。只需在硬盘上放一个测试文件,然后对名称进行硬编码。这将使您知道,如果InputFile.txt和Un-EncryptedFile.text相同,则编码/解码是有效的。
一旦你有了工作,你就可以开始建立完整的日常生活。你发布的代码与按钮点击之间使用的全局变量和刚刚命名的1、2等非常混淆。你创建的流什么都不做,只会让问题更加混淆。把事情简单化,首先让它发挥作用。
procedure TestEncodeDecode();
begin
Codec2.Password := WORD_2;
Codec2.EncryptFile('c:InputFile.txt', 'c:EncryptedFile.txt');
Codec2.DecryptFile('c:EncryptedFile.txt', 'c:Un-EncryptedFile.txt');
end;
我也对你的问题感到困惑。冒着误解你的问题的风险,我假设你在尝试:
- 对字符串进行加密
- 将加密字符串存储在文件中
- 加密文件(双重加密)
- 反转前面的步骤以重建原始字符串
CCD_ 1方法证明了这一点。
如果这种解释是正确的,请考虑以下解决方案。(在Delphi 2010中进行了测试。未在XE2)中进行测试
unit uDoubleEncrypt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, uTPLb_CryptographicLibrary, uTPLb_BaseNonVisualComponent,
uTPLb_Codec;
type
TmfmDoubleEncrypt = class(TForm)
Codec1: TCodec;
Codec2: TCodec;
CryptographicLibrary1: TCryptographicLibrary;
btnGo: TButton;
memoLog: TMemo;
dlgSave1: TSaveDialog;
dlgOpen1: TOpenDialog;
procedure btnGoClick(Sender: TObject);
private
FFileName_Plain, FFileName_Cipher: string;
sSerial: string;
function EncryptStringWithCodec1( const sPlaintext: string): ansistring;
function GetFileName( dlgOpenX: TOpenDialog; var sFN: string): boolean;
procedure SaveAnsiStringToFile( const sFN: string; const sSerialCipherText: AnsiString);
function ReconstructSerial: string;
public
procedure Put( const LineFmt: string; const Args: array of const);
procedure Button1Click;
procedure Button4Click;
function SelfTest: boolean;
end;
var
mfmDoubleEncrypt: TmfmDoubleEncrypt;
implementation
{$R *.dfm}
procedure TmfmDoubleEncrypt.btnGoClick( Sender: TObject);
var
WORD_1, WORD_2: string;
begin
WORD_1 := 'red';
WORD_2 := 'blue';
sSerial := '123'; // Serial_number.Number; // Store Serial Number of machine
Codec1.Password := WORD_1;
Codec2.Password := WORD_2;
// Run the self test.
SelfTest;
// Clean up.
Codec1.Burn;
Codec2.Burn
// You may also want to delete temporary files here.
end;
function TmfmDoubleEncrypt.EncryptStringWithCodec1(
const sPlaintext: string): ansistring;
begin
// Assume Codec1 properties already set-up:
// 1. Password
// 2. CryptoLibrary
// 3. Cipher (at design-time)
// 4. Chain-mode
Codec1.Reset; // Normally not necessary. A defence agains the codec being left in a corrupt state.
Codec1.EncryptString( sPlaintext, result)
end;
function TmfmDoubleEncrypt.GetFileName(
dlgOpenX: TOpenDialog; var sFN: string): boolean;
begin
result := dlgOpenX.Execute;
if result then
sFN := dlgOpenX.FileName
end;
procedure TmfmDoubleEncrypt.Put(
const LineFmt: string; const Args: array of const);
begin
memoLog.Lines.Add( Format( LineFmt, Args))
end;
procedure TmfmDoubleEncrypt.SaveAnsiStringToFile(
const sFN: string; const sSerialCipherText: AnsiString);
const
Modes: array[boolean] of word = (fmCreate, fmOpenReadWrite);
var
SaveStream: TStream;
begin
SaveStream := TFileStream.Create( sFN, Modes[ FileExists( sFN)]);
try
SaveStream.Size := 0;
if sSerialCipherText <> '' then
SaveStream.WriteBuffer( sSerialCipherText[1], Length( sSerialCipherText))
finally
SaveStream.Free
end
end;
procedure TmfmDoubleEncrypt.Button1Click;
// This method is equivalent to gEdit101's Button1Click()
var
sPlainText: string;
sSerialCipherText: AnsiString;
sFN: string;
begin
sPlainText := sSerial;
sSerialCipherText := EncryptStringWithCodec1( sPlainText);
Put( 'Encrypted serial number is %s', [sSerialCipherText]);
if GetFileName( dlgOpen1, sFN) then
begin
SaveAnsiStringToFile( sFN, sSerialCipherText);
FFileName_Plain := sFN; // Store for Button4Click()
Put('encrypted serial number save to file "%s".',[sFN])
end;
end;
procedure TmfmDoubleEncrypt.Button4Click;
// This method is equivalent to gEdit101's Button4Click()
var
sPlainText: string;
sSerialCipherText: AnsiString;
sFN: string;
begin
Codec2.Reset;
FFileName_Cipher := FFileName_Plain + '_enc.dat'; // Not a text file. + '_enc.txt' would be wrong.
Codec2.EncryptFile( FFileName_Plain, FFileName_Cipher);
Put( 'Double Encrypted serial number is now stored in file "%s"', [FFileName_Cipher]);
end;
function TmfmDoubleEncrypt.ReconstructSerial: string;
var
CipherStream, PlainStream: TStream;
sEncryptedSerial: AnsiString;
begin
CipherStream := TFileStream.Create( FFileName_Cipher, fmOpenRead);
PlainStream := TMemoryStream.Create;
try
Codec2.Reset;
Codec2.DecryptStream( PlainStream, CipherStream);
PlainStream.Position := 0;
SetLength( sEncryptedSerial, PlainStream.Size);
if Length( sEncryptedSerial) > 0 then
PlainStream.ReadBuffer( sEncryptedSerial[1], Length( sEncryptedSerial));
Codec1.Reset;
Codec1.DecryptString( result, sEncryptedSerial)
finally
CipherStream.Free;
PlainStream.Free
end
end;
function TmfmDoubleEncrypt.SelfTest: boolean;
var
sRecon: string;
begin
Put('Commencing self test...',[]);
try
Button1Click; // 1st encryption
Button4Click; // 2nd encryption
sRecon := ReconstructSerial; // Reconstruction
result := sSerial = sRecon
finally
Put('Finished self test. Result = %s',[BoolToStr( result, True)]);
end;
end;
end.
这个单元的dfm是。。。
object mfmDoubleEncrypt: TmfmDoubleEncrypt
Left = 0
Top = 0
Caption = 'Double Encrypt'
ClientHeight = 304
ClientWidth = 643
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
DesignSize = (
643
304)
PixelsPerInch = 96
TextHeight = 13
object btnGo: TButton
Left = 8
Top = 8
Width = 75
Height = 25
Caption = 'Go'
TabOrder = 0
OnClick = btnGoClick
end
object memoLog: TMemo
Left = 8
Top = 39
Width = 627
Height = 257
Anchors = [akLeft, akTop, akRight, akBottom]
Color = clInfoBk
ReadOnly = True
ScrollBars = ssVertical
TabOrder = 1
end
object Codec1: TCodec
AsymetricKeySizeInBits = 1024
AdvancedOptions2 = []
CryptoLibrary = CryptographicLibrary1
Left = 440
Top = 112
StreamCipherId = 'native.StreamToBlock'
BlockCipherId = 'native.AES-128'
ChainId = 'native.CBC'
end
object Codec2: TCodec
AsymetricKeySizeInBits = 1024
AdvancedOptions2 = []
CryptoLibrary = CryptographicLibrary1
Left = 536
Top = 112
StreamCipherId = 'native.StreamToBlock'
BlockCipherId = 'native.AES-128'
ChainId = 'native.CBC'
end
object CryptographicLibrary1: TCryptographicLibrary
Left = 480
Top = 48
end
object dlgSave1: TSaveDialog
InitialDir = 'C:Temp'
Title = 'Choose location to save Authentication Code'
Left = 440
Top = 176
end
object dlgOpen1: TOpenDialog
InitialDir = 'C:'
Left = 536
Top = 176
end
end