1

AES-256-CBC を使用してデータを暗号化するために OpenSSL::Cipher を使用しているサーバーから情報を送信しています。Delphi XE8 でコーディングされたアプリケーションでデータを受信し、TPLB 3 OpenSSL を使用してデータを復号化しようとしています。私が試したすべての情報から、一致するすべての情報、キー、ivなどがありますが、復号化しようとするとエラーまたはジャンクデータが表示されます. 適切に復号化するために TPLB 3 のセットアップ/構成に欠けているものがあると思いますが、一生それを理解することはできません。どんな助けでも大歓迎です。

Delphi 復号化

function TLicenseReload.Decode(L, K, I: string): string;
var
  cdec: TOpenSSL_Codec;
  s: string;
  sOut,
  sIn: TStream;
begin
  Result := '';
  cdec := TOpenSSL_Codec.Create(nil);
  sIn := TStringStream.Create;
  sout := TStringStream.Create;
  try
    sIn.Write(L, length(L));
    sIn.Position := 0;

    cdec.SetKey(TEncoding.Default.GetBytes(K));
    cdec.SetIV(TEncoding.Default.GetBytes(I));
    cdec.Cipher := cipher_aes_256_cbc;
    cdec.PaddingScheme := {padNone;//}padPKCS;
    //cdec.LibName := 'libeay32.dll'; //toggled on and off to attempt to decrypt correctly
    //cdec.LibPath := ExtractFilePath(Application.Exename); //toggled on and off to attempt to decrypt correctly
    //cdec.RequiredVersion := '1.0.1.7'; //toggled on and off to attempt to decrypt correctly
    cdec.isLoaded := true; //receive an access violation if this is not set
    cdec.Decrypt(sOut, sIn);
    //s := sOut.DataString;   //was using TStringStream but wasn't working so switched to TStream
    sOut.ReadBuffer(s[1], sOut.Size - sOut.Position);
    result := s;
  finally
    sOut.Free;
    sIn.Free;
    cdec.Free;
  end;
end;

ルビーの暗号化

begin
unless loc.nil?
  cip = OpenSSL::Cipher.new('AES-256-CBC')
  cip.encrypt
  cip.key = Digest::SHA1.hexdigest(loc.l_hash[0..31].upcase).upcase
  lic_iv = cip.random_iv
  lic_iv = Base64.encode64(lic_iv)
  enc_lic_date = cip.update(loc.licensed_through.to_s + ':' + loc.customer.purchased.to_s) + cip.final
  enc_lic_date = Base64.encode64(enc_lic_date)#.encode('utf-8')
  #enc_lic_date << cip.final
end
rescue StandardError => e
  error_message = e.to_s
  puts e.to_s
end

編集:

私は戻って、すべてを再確認しました(基本的に最初からやり直しました)。サーバー上で暗号化されている (Base64 エンコードされる前の) バイトが、クライアントで復号化されている (Base64 デコード後) バイトと同じであることを確認しました。しかし、私はまだ「ジャンク」を出しています。

更新された (雑然とした) Delphi 復号化

function TLicenseReload.DecodeLicense(L, K, I: string): string;
var
  cdec: TOpenSSL_Codec;
  s: string;
  sOut,
  sIn: TStringStream;
  x,
  y: TBytes;
  z: string;
begin
  Result := '';
  cdec := TOpenSSL_Codec.Create(nil);
  sIn := TStringStream.Create;
  sout := TStringStream.Create;
  try
    SetLength(x, Length(K));
    SetLength(y, Length(DecodeBase64(I)));
    //SetLength(z, Length(DecodeBase64(L)));
    x := TEncoding.UTF8.GetBytes(K);
    y := DecodeBase64(I);
    //z := string(DecodeBase64(L));

    //sIn.WriteString(z);//, length(z));
    sIn.WriteData(DecodeBase64(L), length(DecodeBase64(L)));
    sIn.Position := 0;

    //cdec.SetKey(TEncoding.UTF8.GetBytes(unbaseit(K)));
    //cdec.SetIV(TEncoding.UTF8.GetBytes(unbaseit(I)));
    cdec.SetKey(TEncoding.UTF8.GetBytes(K));
    cdec.SetIV(DecodeBase64(I));
    cdec.Cipher := cipher_aes_256_cbc;
    cdec.PaddingScheme := padNone;//}padPKCS;
    //cdec.LibName := 'libeay32.dll';
    //cdec.LibPath := ExtractFilePath(Application.Exename);
    //cdec.RequiredVersion := '1.0.1.7';
    cdec.isLoaded := true;
    cdec.Decrypt(sOut, sIn);
    s := sOut.DataString;
    //sOut.ReadBuffer(s[1], sOut.Size - sOut.Position);
    result := s;
  finally
    sOut.Free;
    sIn.Free;
    cdec.Free;
  end;
end;

編集 2 TPLB3 には、パディング用の 2 つのオプション、なしまたは PKCS があります。None を設定すると、がらくたが出てきます。PKCS を設定すると、「OpenSSL 暗号化エラー」が発生します。結果のエンコーディングは問題ではないようですが、それでもジャンクです。

4

1 に答える 1