【问题标题】:Delphi: string encryption method and base64Delphi:字符串加密方法和base64
【发布时间】:2011-07-31 17:17:17
【问题描述】:

请给我推荐一个好的字符串加密方法。不是异或,不够强。

我可以使用 Base64 来表示加密的字符串,但在字符串的末尾没有“=”吗?我可以手动添加。正常吗?那就是用户将在程序中使用没有“=”的Base64,我将添加它。我不想看到带有'='的视图,这不好:)

谢谢!!!

【问题讨论】:

  • 不明白为什么要去掉 =
  • 我有一个编辑,用户必须在其中粘贴激活密钥。结尾有“==”不好。
  • 用户不关心 = 激活密钥!
  • @David Heffernan,使用“=”很有趣 :) 我这辈子从没见过这样的激活字符串 :)
  • @Downvoter:当然。它适合这个场合:)

标签: delphi encryption base64


【解决方案1】:

这是一个加密库:http://www.cityinthesky.co.uk/opensource/dcpcrypt

是的,您可以显示一个末尾不带“=”符号的 base64 字符串。您只需要确保当您将值传递给方法时,该方法足够聪明,可以在尝试解密之前将其重新添加。这是很常见的情况。

【讨论】:

    【解决方案2】:

    这里有一个函数(或几个函数)来编码和解码您可以使用的字符串,您可以使用 Base64Encode('待编码的字符串') 和 Base64Decode('待解码的字符串') 调用它,希望对您有所帮助。

    const
    B64: array[0..63] of byte= (65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
    81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,
    109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,
    54,55,56,57,43,47);
    
    function B64Encode(pInput: pointer; pOutput: pointer; Size: longint): longint;
    var
      i, iptr, optr: integer;
      Input, Output: PByteArray;
    begin
     Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
     iptr:= 0; optr:= 0;
     for i:= 1 to (Size div 3) do
      begin
        Output^[optr+0]:= B64[Input^[iptr] shr 2];
        Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
        Output^[optr+2]:= B64[((Input^[iptr+1] and 15) shl 2) + (Input^[iptr+2] shr 6)];
        Output^[optr+3]:= B64[Input^[iptr+2] and 63];
        Inc(optr,4); Inc(iptr,3);
      end;
    case (Size mod 3) of
     1: begin
        Output^[optr+0]:= B64[Input^[iptr] shr 2];
        Output^[optr+1]:= B64[(Input^[iptr] and 3) shl 4];
        Output^[optr+2]:= byte('=');
        Output^[optr+3]:= byte('=');
    end;
     2: begin
        Output^[optr+0]:= B64[Input^[iptr] shr 2];
        Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
        Output^[optr+2]:= B64[(Input^[iptr+1] and 15) shl 2];
        Output^[optr+3]:= byte('=');
     end;
    end;
     Result:= ((Size+2) div 3) * 4;
    end;
    
    
    function Base64Encode(const Value: AnsiString): AnsiString;
     begin
      SetLength(Result,((Length(Value)+2) div 3) * 4);
      B64Encode(@Value[1],@Result[1],Length(Value));
    end;
    
    
    function B64Decode(pInput: pointer; pOutput: pointer; Size: longint): longint;
     var
      i, j, iptr, optr: integer;
      Temp: array[0..3] of byte;
      Input, Output: PByteArray;
     begin
      Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
      iptr:= 0; optr:= 0;
      Result:= 0;
      for i:= 1 to (Size div 4) do
      begin
       for j:= 0 to 3 do
        begin
         case Input^[iptr] of
          65..90 : Temp[j]:= Input^[iptr] - Ord('A');
          97..122: Temp[j]:= Input^[iptr] - Ord('a') + 26;
          48..57 : Temp[j]:= Input^[iptr] - Ord('0') + 52;
          43     : Temp[j]:= 62;
          47     : Temp[j]:= 63;
          61     : Temp[j]:= $FF;
        end;
       Inc(iptr);
    end;
      Output^[optr]:= (Temp[0] shl 2) or (Temp[1] shr 4);
      Result:= optr+1;
     if (Temp[2]<> $FF) and (Temp[3]= $FF) then
      begin
        Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
        Result:= optr+2;
        Inc(optr)
     end
      else if (Temp[2]<> $FF) then
       begin
         Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
         Output^[optr+2]:= (Temp[2] shl 6) or  Temp[3];
         Result:= optr+3;
         Inc(optr,2);
      end;
      Inc(optr);
     end;
    end;
    
    function Base64Decode(const Value: AnsiString): AnsiString;
    begin
      SetLength(Result,(Length(Value) div 4) * 3);
      SetLength(Result,B64Decode(@Value[1],@Result[1],Length(Value)));
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      相关资源
      最近更新 更多