하안인 님이 쓰신 글 :
:
: 암호화 관련 샘플을 찾았는데 델파이로 되어 있어서
: c++빌더로 변환 도움을 구하고자 합니다.
:
:
: uses
: DCPcrypt2, DCPrijndael, DCPbase64;
:
: // Some constants that are dependant on the cipher being used
: // Assuming MCRYPT_RIJNDAEL_128 (i.e., 128bit blocksize, 256bit keysize)
: const
: KeySize = 32; // 32 bytes = 256 bits
: BlockSize = 16; // 16 bytes = 128 bits
:
: {$R *.dfm}
:
: // Pad a string with zeros so that it is a multiple of size
: function PadWithZeros(const str : string; size : integer) : string;
: var
: origsize, i : integer;
: begin
: Result := str;
: origsize := Length(Result);
: if ((origsize mod size) <> 0) or (origsize = 0) then
: begin
: SetLength(Result,((origsize div size)+1)*size);
: for i := origsize+1 to Length(Result) do
: Result[i] := #0;
: end;
: end;
:
: // Encrypt a string and return the Base64 encoded result
: procedure TfrmMain.btnEncryptClick(Sender: TObject);
: var
: Cipher : TDCP_rijndael;
: Data, Key, IV : string;
: begin
: // Pad Key, IV and Data with zeros as appropriate
: Key := PadWithZeros(boxKey.Text,KeySize);
: IV := PadWithZeros(boxIV.Text,BlockSize);
: Data := PadWithZeros(boxPlainTextIn.Text,BlockSize);
: // Create the cipher and initialise according to the key length
: Cipher := TDCP_rijndael.Create(Self);
: if Length(boxKey.Text) <= 16 then
: Cipher.Init(Key[1],128,@IV[1])
: else if Length(boxKey.Text) <= 24 then
: Cipher.Init(Key[1],192,@IV[1])
: else
: Cipher.Init(Key[1],256,@IV[1]);
: // Encrypt the data
: Cipher.EncryptCBC(Data[1],Data[1],Length(Data));
: // Free the cipher and clear sensitive information
: Cipher.Free;
: FillChar(Key[1],Length(Key),0);
: // Display the Base64 encoded result
: boxCipherTextOut.Text := Base64EncodeStr(Data);
: end;
:
: procedure TfrmMain.btnDecryptClick(Sender: TObject);
: var
: Cipher : TDCP_rijndael;
: Data, Key, IV : string;
: begin
: // Pad Key and IV with zeros as appropriate
: Key := PadWithZeros(boxKey.Text,KeySize);
: IV := PadWithZeros(boxIV.Text,BlockSize);
: // Decode the Base64 encoded string
: Data := Base64DecodeStr(boxCipherTextIn.Text);
: // Create the cipher and initialise according to the key length
: Cipher := TDCP_rijndael.Create(Self);
: if Length(boxKey.Text) <= 16 then
: Cipher.Init(Key[1],128,@IV[1])
: else if Length(boxKey.Text) <= 24 then
: Cipher.Init(Key[1],192,@IV[1])
: else
: Cipher.Init(Key[1],256,@IV[1]);
: // Decrypt the data
: Cipher.DecryptCBC(Data[1],Data[1],Length(Data));
: // Free the cipher and clear sensitive information
: Cipher.Free;
: FillChar(Key[1],Length(Key),0);
: // Display the result
: boxPlainTextOut.Text := Data;
: end;
:
: end.
:
: 빌더로 변환하면 어떻게 되는지 몰라서 도움구합니다.
:
: 감사합니다.
답변:
프로그래밍 질문이 아니고, 델파이 코드를 C++로 포팅해 달라는 요구네요.
올린 글들 보니, 블럭체인 모드로 AES 암호화를 하려고 하는 것 같은데
AES는 하다못해 위키에서도 알고리즘을 친절하게 잘 설명하고 있으니 직접 구현해서 쓰세요.
AES 알고리즘이 그렇게 복잡하지도 않습니다.
알고리즘을 직접 구현할 여건이 안된다면...
델파이 컴포넌트 기웃거리지 말고 OS에서 제공해주는 Crypto API 이용해서 코딩하세요.
C++ 랭귀지 사용하면서 뭘 그렇게 델파이 코드에 의존하려고 합니까.
#include <wincrypt.h> // OS가 제공해주는 Crypto API 헤더
CryptAcquireContext() API 호출해서 프로바이더 핸들 열고
Key와 IVector를 초기화 한 후...
CryptSetKeyParam() API 이용해서
DWORD dwMode = CRYPT_MODE_CBC; // 블럭체인모드
CryptSetKeyParam(hAesKey, KP_MODE, (BYTE*)&dwMode, 0);
블럭체인모드로 지정하고
CryptEncrypt()/CryptDecrypt() API 호출해서 암호화/복호화 하면 됍니다.
API 사용법은 MSDN 문서 찾아 보세요.
|