首页 新闻 会员 周边 捐助

急求java RC2加密算法

0
悬赏园豆:100 [已关闭问题] 关闭于 2011-11-30 13:59

下面是一段C++  RC2加密   要求要用java 重写  能互相加密解密
QSBEncryptRc2::QSBEncryptRc2()
{
    EncryKey = "DingXin Communication Key 20080613";
}

//解密失败时返回失败描述
AnsiString QSBEncryptRc2::GetDecryptErrMsg()
{
    int ErrorCode;
    AnsiString ErrMsg;

    ErrorCode = GetLastError();
    switch( ErrorCode )
    {
        case ERROR_INVALID_HANDLE:
            ErrMsg = "其中一个参数是非法句柄";
            break;
        case NTE_BAD_ALGID:
            ErrMsg = "不支持的加密算法";
            break;
        case NTE_BAD_DATA:
            ErrMsg = "解密的数据非法";
            break;
        case NTE_BAD_FLAGS:
            ErrMsg = "标识参数不为0";
            break;
        case NTE_BAD_HASH:
            ErrMsg = "哈希参数非法";
            break;
        case NTE_BAD_KEY:
            ErrMsg = "密钥是非法句柄";
            break;
        case NTE_BAD_LEN:
            ErrMsg = "解密数据内存太小";
            break;
        case NTE_BAD_UID:
            ErrMsg = "找不到CSP上下文";
            break;
        case NTE_DOUBLE_ENCRYPT:
            ErrMsg = "应用企图解密已经解密的数据";
            break;
        case NTE_FAIL:
            ErrMsg = "程序由于未知原因结束";
            break;
        default:
            ErrMsg = "未知错误";
    }
    return ErrMsg;
}

//加密失败时返回失败描述
AnsiString QSBEncryptRc2::GetEncrytErrMsg()
{
    int ErrorCode;
    AnsiString ErrMsg;

    ErrorCode = GetLastError();
    switch( ErrorCode )
    {
        case ERROR_INVALID_HANDLE:
            ErrMsg = "其中一个参数是非法句柄";
            break;
        case ERROR_INVALID_PARAMETER:
            ErrMsg = "非法参数";
            break;
        case NTE_BAD_ALGID:
            ErrMsg = "不支持的加密算法";
            break;
        case NTE_BAD_DATA:
            ErrMsg = "解密的数据非法";
            break;
        case NTE_BAD_FLAGS:
            ErrMsg = "标识参数不为0";
            break;
        case NTE_BAD_HASH:
            ErrMsg = "哈希参数非法";
            break;
        case NTE_BAD_KEY:
            ErrMsg = "密钥是非法句柄";
            break;
        case NTE_BAD_LEN:
            ErrMsg = "解密数据内存太小";
            break;
        case NTE_BAD_UID:
            ErrMsg = "找不到CSP上下文";
            break;
        case NTE_DOUBLE_ENCRYPT:
            ErrMsg = "应用企图加密已经加密的数据";
            break;
        case NTE_NO_MEMORY:
            ErrMsg = "内存不足";
            break;
        case NTE_FAIL:
            ErrMsg = "程序由于未知原因结束";
            break;
        default:
            ErrMsg = "未知错误";
    }
    return ErrMsg;
}

//加密数据
void QSBEncryptRc2::EncryptData(QSBBlob &inData,QSBBlob &outData)
{
    HCRYPTPROV hProv = 0;
    HCRYPTHASH hHash = 0;
    HCRYPTKEY hKey = 0;
    DWORD dwCurrLen,dwLength;
    BYTE pbBuffer[BUFFER_SIZE];
    DWORD dwCount;
    int  eof;
    const char *ptr;
    DWORD  nLen;
    DWORD  nEncryCount;
    AnsiString ErrMsg;

    try
    {
        //连接CSP
        if(!CryptAcquireContext(&hProv, TEXT("DingXinKey"), 0, PROV_RSA_FULL, 0))
        {
            //如KEY CONTAINER不存在,则重新创建一个
            if( GetLastError() == (DWORD)NTE_BAD_KEYSET )
            {
                if(!CryptAcquireContext(&hProv, TEXT("DingXinKey"), 0, PROV_RSA_FULL,
                        CRYPT_NEWKEYSET))
                    throw QSBException(003,"创建KEY CONTAINER失败");
            }
            else
                throw QSBException(003,"连接CSP失败");
        }
        //序列化密码
        if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
            throw QSBException(003,"创建序列对象失败");
        dwLength = strlen(EncryKey.c_str());
        if(!CryptHashData(hHash, (BYTE *)EncryKey.c_str(), dwLength, 0))
            throw QSBException(003,"序列化密码失败");
        if(!CryptDeriveKey(hProv, CALG_RC2, hHash, CRYPT_EXPORTABLE, &hKey))
            throw QSBException(003,"创建密钥失败");
        //加密文件
        outData.ClearContent();
        ptr  = inData.GetBlobPtr();
        nLen = inData.GetBlobLength();
        dwCurrLen = 0;
        do
        {
            if( nLen-dwCurrLen>BLOCK_SIZE )
            {
                memcpy(pbBuffer,ptr+dwCurrLen,BLOCK_SIZE );
                eof = false;
                nEncryCount=BLOCK_SIZE;
            }
            else
            {
                memcpy(pbBuffer,ptr+dwCurrLen,nLen-dwCurrLen );
                eof = true;
                nEncryCount=nLen-dwCurrLen;
            }
            dwCount = nEncryCount;
            if(!CryptEncrypt(hKey, 0, eof, 0, pbBuffer, &dwCount,BUFFER_SIZE))
            {
                ErrMsg.sprintf("加密失败:%s",GetEncrytErrMsg().c_str() );
                throw QSBException(003,ErrMsg.c_str() );
            }
            outData.AppendBlobContent(pbBuffer,dwCount);
            dwCurrLen += nEncryCount;
        }
        while(dwCurrLen<(DWORD)nLen);
    }
    __finally
    {
        if(hHash != 0)
            CryptDestroyHash(hHash);
        if(hKey != 0)
            CryptDestroyKey(hKey);
        if(hProv != 0)
            CryptReleaseContext(hProv,0);
    }
}

//解密数据
void QSBEncryptRc2::DecryptData(QSBBlob &inData,QSBBlob &outData)
{
    HCRYPTPROV hProv = 0;
    HCRYPTHASH hHash = 0;
    HCRYPTKEY hKey = 0;
    DWORD dwCurrLen,dwLength;
    BYTE pbBuffer[BUFFER_SIZE];
    DWORD dwCount;
    int  eof;
    const char *ptr;
    DWORD  nLen;
    DWORD  nDecryCount;
    AnsiString ErrMsg;

    try
    {
        //连接CSP
        if(!CryptAcquireContext(&hProv, TEXT("DingXinKey"), 0, PROV_RSA_FULL, 0))
        {
            //如KEY CONTAINER不存在,则重新创建一个
            if( GetLastError() == (DWORD)NTE_BAD_KEYSET )
            {
                if(!CryptAcquireContext(&hProv, TEXT("DingXinKey"), 0, PROV_RSA_FULL,
                        CRYPT_NEWKEYSET))
                    throw QSBException(003,"创建KEY CONTAINER失败");
            }
            else
                throw QSBException(003,"连接CSP失败");
        }
        //序列化密码
        if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
            throw QSBException(003,"创建序列对象失败");
        dwLength = strlen(EncryKey.c_str());
        if(!CryptHashData(hHash, (BYTE *)EncryKey.c_str(), dwLength, 0))
            throw QSBException(003,"序列化密码失败");
        if(!CryptDeriveKey(hProv, CALG_RC2, hHash, CRYPT_EXPORTABLE, &hKey))
            throw QSBException(003,"创建密钥失败");
        //解密文件
        outData.ClearContent();
        ptr  = inData.GetBlobPtr();
        nLen = inData.GetBlobLength();
        dwCurrLen = 0;
        do
        {
            if( nLen-dwCurrLen>BLOCK_SIZE )
            {
                memcpy(pbBuffer,ptr+dwCurrLen,BLOCK_SIZE );
                eof = false;
                nDecryCount=BLOCK_SIZE;
            }
            else
            {
                memcpy(pbBuffer,ptr+dwCurrLen,nLen-dwCurrLen );
                eof = true;
                nDecryCount=nLen-dwCurrLen;
            }
            dwCount = nDecryCount;
            if(!CryptDecrypt(hKey, 0, eof, 0, pbBuffer, &dwCount))
            {
                ErrMsg.sprintf("解密数据失败:%s",GetDecryptErrMsg().c_str());
                throw QSBException(003,ErrMsg.c_str());
            }
            outData.AppendBlobContent(pbBuffer,dwCount);
            dwCurrLen += nDecryCount;
        }
        while(dwCurrLen<(DWORD)nLen);
    }
    __finally
    {
        if(hHash != 0)
            CryptDestroyHash(hHash);
        if(hKey != 0)
            CryptDestroyKey(hKey);
        if(hProv != 0)
            CryptReleaseContext(hProv,0);
    }
}

六月ee的主页 六月ee | 初学一级 | 园豆:200
提问于:2011-11-28 18:09
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册