2012-01-29 140 views
3

我有以下ActionScript的代碼:轉換ActionScript來德爾福

function EncryptString(SrcStr:String, KeyStr:String) : String 
{ 
    var KeyHexed:* = Hex.toArray(Hex.fromString(KeyStr)); 
    var SrcHexed:* = Hex.toArray(Hex.fromString(SrcStr)); 
    var NullPadded:* = new NullPad(); 
    var Cipher:* = Crypto.getCipher("simple-aes128-cfb8", KeyHexed, NullPadded); 
    NullPadded.setBlockSize(Cipher.getBlockSize()); 
    Cipher.encrypt(SrcHexed); 
    return Base64.encodeByteArray(SrcHexed); 
} 

如何使用Delphi Encryption Compendium (DEC)我轉換爲德爾福?

感謝您的幫助!

編輯1:

我嘗試以下德爾福代碼:

function EncryptString(Param1, Param2: String): String; 
var 
    Cipher: TCipher_Rijndael; 
begin 
    Cipher := TCipher_Rijndael.Create; 
    Cipher.Mode := cmCFB8; 
    Cipher.Init(Param2, '', $FF); 
    Result := Cipher.EncodeBinary(TFormat_HEX.Encode(Param1), TFormat_MIME64); 
    Cipher.Free; 
end; 
+0

我已將代碼添加到問題 – 2012-01-29 16:53:24

+5

好的。所以你有代碼。有什麼問題? – 2012-01-29 16:53:49

+0

您是使用ANSI還是Unicode德爾福? – 2012-01-29 19:23:59

回答

0

這是你在找什麼(樣)?

注意:Binary = RawByteString;

uses 
    DECUtil, DECCipher, DECHash, DECFmt; 

var 
    ACipherClass: TDECCipherClass = TCipher_Rijndael; 
    ACipherMode: TCipherMode = cmCBCx; //cmCFB8 
    AHashClass: TDECHashClass = THash_Whirlpool; 
    ATextFormat: TDECFormatClass = TFormat_Mime64; 
    AKDFIndex: LongWord = 1; 

function Encrypt(const AText: String; const APassword: String): String; overload; 
var 
    ASalt: Binary; 
    AData: Binary; 
    APass: Binary; 
begin 
    with ValidCipher(ACipherClass).Create, Context do 
    try 
    ASalt := RandomBinary(16); 
    APass := ValidHash(AHashClass).KDFx(APassword[1], Length(APassword) * SizeOf(APassword[1]), ASalt[1],  Length 
(ASalt), KeySize, TFormat_Copy, AKDFIndex); 
    Mode := ACipherMode; 
    Init(APass); 
    SetLength(AData, Length(AText) * SizeOf(AText[1])); 
    Encode(AText[1], AData[1], Length(AData)); 
    Result := ValidFormat(ATextFormat).Encode(ASalt + AData + CalcMAC); 
    finally 
    Free; 
    ProtectBinary(ASalt); 
    ProtectBinary(AData); 
    ProtectBinary(APass); 
    end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    s, k: WideString; 
begin 
    s := 'Please accept this as answer'; 
    k := 'Stackoverflow'; 
    Memo1.Lines.Clear; 
    Memo1.Lines.Add('Encode Test: ' + Encrypt(s, k) + sLineBreak); 
end;