C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[36384] Crypto 암호화->복호화가 안됩니다..
DoyongID [doyongid] 1610 읽음    2004-07-21 11:33
예전에 콘솔모드에서 짰을 때는 제대로 되었던것 같았는데.. GUI환경으로 바꾸고 나서부터 복호화가 제대로 안되는 것 같습니다..

1바이트짜리를 암호화하려고 하는데...

0xf0 를 암호화하면  0x35가 되는데, 이걸 다시 복호화하면 0x20이 되어버립니다..

신기한 것이 같은 프로그램 내에서 암호화 복호화를 반복하면 제대로 되는데, A(서버)라는 프로그램에서 암호화한 후, B(클라이언트)라는 프로그램에서 복호화하면 제대로 안됩니다..

패스워드 방식이고요.. 길이는 고정길이(?) 방식으로 했습니다...

소스는 아래와 같습니다.

CPP파일
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 생성자
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
TPacketKey::TPacketKey()
{
    J2FastCryptMem_hProv        = 0;
    J2FastCryptMem_hKey            = 0;
    J2FastCryptMem_hXchgKey        = 0;
    J2FastCryptMem_hHash        = 0;
    J2FastCryptMem_bMakeFast    = TRUE;
}
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 초기화
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
BOOL TPacketKey::Init(char *szPassword, BOOL bMakeFast)
{
    ALG_ID Algid;
    BOOL bRet=FALSE;

    //------------------------------
    //이미 초기화 되어있다면 초기화 하지 않는다.
    //재설정을 하려면 J2FastCryptMem_Clear();를 먼저 호출한다.
    if(J2FastCryptMem_hProv!=0)
    {
        return TRUE;
    }
    //------------------------------
    Release();

    J2FastCryptMem_bMakeFast = bMakeFast;
    if(bMakeFast)
    {
        Algid=CALG_RC4;
    }
    else
    {
        Algid=CALG_RC2;
    }

    // Get handle to the default provider.
    if(!CryptAcquireContext(&J2FastCryptMem_hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0))
    {
        if(!CryptAcquireContext(&J2FastCryptMem_hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET))
        {
            //printf("Error %x during CryptAcquireContext!\n", GetLastError());
            goto done;
        }
    }

    if(szPassword == NULL)
    {
        szPassword="DefaultPassword_J2DeCryptFile";
    }
    // EnCrypt the file with a session key derived from a password.

    // Create a hash object.
    if(!CryptCreateHash(J2FastCryptMem_hProv, CALG_MD5, 0, 0, &J2FastCryptMem_hHash))
    {
        //printf("Error %x during CryptCreateHash!\n", GetLastError());
        goto done;
    }

    // Hash in the password data.
    if(!CryptHashData(J2FastCryptMem_hHash, (BYTE*)szPassword, strlen(szPassword), 0))
    {
        //printf("Error %x during CryptHashData!\n", GetLastError());
        goto done;
    }

    // Derive a session key from the hash object.
    if(!CryptDeriveKey(J2FastCryptMem_hProv, Algid, J2FastCryptMem_hHash, 0, &J2FastCryptMem_hKey))
    {
        //printf("Error %x during CryptDeriveKey!\n", GetLastError());
        goto done;
    }

    // Destroy the hash object.
    CryptDestroyHash(J2FastCryptMem_hHash);
    J2FastCryptMem_hHash = 0;
    bRet=TRUE;

done:
    if(bRet==FALSE)
    {
        #ifdef _DEBUG
        PutError(GetLastError());
        #endif
        Release();
    }
    return bRet;
}
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 해제
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
void TPacketKey::Release(void)
{
    // Destroy session key.
    if(J2FastCryptMem_hKey)
    {
        CryptDestroyKey(J2FastCryptMem_hKey);
        J2FastCryptMem_hKey=0;
    }

    // Release key exchange key handle.
    if(J2FastCryptMem_hXchgKey)
    {
        CryptDestroyKey(J2FastCryptMem_hXchgKey);
        J2FastCryptMem_hXchgKey=0;
    }

    // Destroy hash object.
    if(J2FastCryptMem_hHash)
    {
        CryptDestroyHash(J2FastCryptMem_hHash);
        J2FastCryptMem_hHash=0;
    }

    // Release provider handle.
    if(J2FastCryptMem_hProv)
    {
        CryptReleaseContext(J2FastCryptMem_hProv, 0);
        J2FastCryptMem_hProv=0;
    }
}
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 인코딩
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DWORD TPacketKey::Encode(IN OUT char *pData, IN  DWORD Data_len, IN  DWORD dwBUf_len)
{
    DWORD dwRet=0;

    if(J2FastCryptMem_hKey==0)
    {
        return FALSE;
    }
    if(Data_len <= 0)
    {
        return FALSE;
    }
    if(Data_len <= 0)
    {
        return 0;
    }

    //암호화가되면 크기가 증가한다.
    //크기 얻기
    dwRet=Data_len;
    if(!CryptEncrypt(J2FastCryptMem_hKey, 0, TRUE, 0, NULL, &dwRet, dwBUf_len))
    {
        return 0;
    }
    //Get Size
    if(pData==NULL)
    {
        return dwRet;
    }
    if(dwBUf_len < dwRet)
    {
        return 0;
    }
    //암호화
    dwRet=Data_len;
    if(!CryptEncrypt(J2FastCryptMem_hKey, 0, TRUE, 0, (BYTE*)pData, &dwRet, dwBUf_len))
        return 0;

    return dwRet;
}
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 디코딩
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DWORD TPacketKey::Decode(IN OUT char *pData, IN DWORD Data_len)
{
    DWORD bRet = 0;

    if(J2FastCryptMem_hKey==0)
    {
        return 0;
    }
    if(pData==NULL)
    {
        return 0;
    }
    if(Data_len <= 0)
    {
        return 0;
    }
    bRet=Data_len;
    if(!CryptDecrypt(J2FastCryptMem_hKey, 0, TRUE, 0, (BYTE*)pData, &bRet))
        return 0;

    return(bRet);
}



H파일
class TPacketKey
{
public:
    TPacketKey();

public:
    HCRYPTPROV    J2FastCryptMem_hProv;
    HCRYPTKEY    J2FastCryptMem_hKey;
    HCRYPTKEY    J2FastCryptMem_hXchgKey;
    HCRYPTHASH    J2FastCryptMem_hHash;
    BOOL        J2FastCryptMem_bMakeFast;

public:
    BOOL        Init        (char *szPassword = NULL, BOOL bMakeFast = TRUE);
    void        Release     (void);

    DWORD       Encode      (IN OUT char *pData, IN  DWORD Data_len, IN  DWORD dwBuf_len);
    DWORD       Decode      (IN OUT char *pData, IN  DWORD Data_len);

    void        PutError    (DWORD err);
};

+ -

관련 글 리스트
36384 Crypto 암호화->복호화가 안됩니다.. DoyongID 1610 2004/07/21
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.