LockBox3 DecryptStream无法创建纯流

时间:2016-07-04 10:22:51

标签: lockbox-3

当我尝试编码一些二进制数据时,一切正常:

void __fastcall TForm1::EncryptBtnClick(TObject *Sender)
{
  char plainchar[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
  char keychar[16]={0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
  char chiperchar[16];

  TMemoryStream *plainStream;
  TMemoryStream *chiperStream;
  TMemoryStream *keyStream;

  plainStream = new TMemoryStream();
  chiperStream = new TMemoryStream();
  keyStream = new TMemoryStream();

  plainStream->Clear();
  plainStream->Position=0;
  plainStream->WriteBuffer(plainchar,16);
  plainStream->Position=0;

  keyStream->Clear();
  keyStream->Position=0;
  keyStream->WriteBuffer(keychar,16);
  keyStream->Position=0;
  Codec1->Reset();
  Codec1->InitFromStream(keyStream);

  Codec1->EncryptStream(plainStream,chiperStream);

  chiperStream->Position=0;
  chiperStream->ReadBuffer(chiperchar,16);

  plainStream->Free();
  chiperStream->Free();
  keyStream->Free();
}

我得到了预期

0x3E,0x45,0xC2,0x15,0xBA,0x45,0x0E,0xA6,0xEF,0x94,0xA3,0x08,0x82,0xB8,0x1B,0xD4 

在chiperchar缓冲区中。

不幸的是,当我尝试对上面的chiper缓冲区使用DecryptStream函数时,它不会创建普通数据流(流是0字节长度)

void __fastcall TForm1::DecryptBtnClick(TObject *Sender)
{
char chiperchar[16]={0x3E,0x45,0xC2,0x15,0xBA,0x45,0x0E,0xA6,0xEF,0x94,0xA3,0x08,0x82,0xB8,0x1B,0xD4};
char keychar[16]={0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA};
char plainchar[16];

TMemoryStream *plainStream;
TMemoryStream *chiperStream;
TMemoryStream *keyStream;

plainStream = new TMemoryStream();
chiperStream = new TMemoryStream();
keyStream = new TMemoryStream();

chiperStream->Clear();
chiperStream->Position=0;
chiperStream->WriteBuffer(chiperchar,16);
chiperStream->Position=0;

keyStream->Clear();
keyStream->Position=0;
keyStream->WriteBuffer(keychar,16);
keyStream->Position=0;
Codec1->Reset();
Codec1->InitFromStream(keyStream);

plainStream->Clear();
plainStream->Position=0;

Codec1->DecryptStream(plainStream,chiperStream);

plainStream->Position=0;
chiperStream->Position=0;

plainStream->Position=0;
plainStream->ReadBuffer(plainchar,16);

plainStream->Free();
chiperStream->Free();
keyStream->Free();
}

我做错了什么?为什么DecrpytStream无法创建正确的流?

当我使用EncryptString和DecryptString函数时,一切正常,加密和解密后,我得到相同的字符串。

Codec1属性是:

AsymetricKeySizeInBits = 128
AdvancedOptions2 = []
CryptoLibrary = CryptographicLibrary1
StreamCipherId = 'native.StreamToBlock'
BlockCipherId = 'native.AES-128'
ChainId = 'native.ECB'

1 个答案:

答案 0 :(得分:0)

抱歉,我不懂C ++。我的语言是Delphi。

这有效....

procedure TForm30.Button1Click(Sender: TObject);
const
  chiperchar: utf8string = #$3E#$45#$C2#$15#$BA#$45#$0E#$A6#$EF#$94#$A3#$08#$82#$B8#$1B#$D4;
  keychar   : utf8string = #$E8#$E9#$EA#$EB#$ED#$EE#$EF#$F0#$F2#$F3#$F4#$F5#$F7#$F8#$F9#$FA;
var
  plainStream, chiperStream, keyStream: TMemoryStream;
  OriginalPlainText: string;
  ReconstructedPlainText: string;

begin
  plainStream  := TMemoryStream.Create;
  chiperStream := TMemoryStream.Create;
  keyStream    := TMemoryStream.Create;
  try
    OriginalPlainText := 'Your lips are smoother than vasoline.';
    Memo1.Lines.Add( 'Plaintext (passed as UTF-8 encoding) = "' + OriginalPlainText + '"');
    TPLB3.StreamUtils.String_to_stream( OriginalPlainText, plainStream, TEncoding.UTF8);
    Memo1.Lines.Add( 'Length of plaintext = ' + IntToStr( plainStream.Size) + ' bytes.');
    keyStream.WriteBuffer( keychar[ Low( keychar)], Length( keychar));
    keyStream.Position := 0;
    Codec1.InitFromStream( keyStream);

    Codec1.EncryptStream( plainStream, chiperStream);
    Memo1.Lines.Add( 'Base64 encoding of ciphertext = ' + TPLB3.StreamUtils.Stream_to_Base64( chiperStream));
    Memo1.Lines.Add( 'Length of ciphertext = ' + IntToStr( chiperStream.Size) + ' bytes.');

    Codec1.Reset;
    chiperStream.Position := 0;
    plainStream.Size := 0;
    Codec1.DecryptStream( plainStream, chiperStream);
    ReconstructedPlainText := TPLB3.StreamUtils.Stream_to_string( plainStream, TEncoding.UTF8);
    Memo1.Lines.Add( 'Reconstructed plaintext = "' + ReconstructedPlainText + '"');

  finally
    plainStream.Free;
    chiperStream.Free;
    keyStream.Free;
    end
end;

这是使用https://github.com/SeanBDurkin/tplockbox版本3.6.3 这适用于Delphi,而不是C ++