• openssl


    原文链接: http://www.cnblogs.com/cswuyg/p/3187462.html 

    openssl是可以很方便加密解密的库,可以使用它来对需要在网络中传输的数据加密。可以使用非对称加密:公钥加密,私钥解密。openssl提供了对RSA的支持,但RSA存在计算效率低的问题,所以一般的做法是使用对称密钥加密数据,然后再把这个只在当前有效的临时生成的对称密钥用非对称密钥的公钥加密之后传递给目标方,目标方使用约定好的非对称密钥中的私钥解开,得到数据加密的密钥,再进行数据解密,得到数据,这种使用方式很常见,可以认为是对HTTPS的裁剪。对称密钥加密可以选择AES,比DES更优秀。
    openssl库来自http://www.openssl.org/,下载到openssl源码之后,开始编译:

    产生动态库的做法:
    1、安装ActivePerl
    2、进入OpenSSL所在文件夹,运行:perl Configure VC-WIN32 --prefix=C:openssl-dll
    3、进入VC/BIN目录,运行 VCVARS32.BAT 设置环境变量
    4、返回OpenSSL目录,运行 msdo_ms
    5、在OpenSSL目录下执行编译 nmake -f ms tdll.mak
    6、把必要生成物拷贝到prefix定义的目录中 nmake -f ms tdll.mak install
    注意:可以通过修改ntdll.mak文件中的CFLAG,确定编译MT、MD库


    产生静态库的做法:
    1、安装ActivePerl
    2、perl configure VC-WIN32 --prefix=C:openssl-lib
    3、msdo_ms.bat
    4、nmake -f ms t.mak
    5、nmake -f ms t.mak install
    注意:可以通过修改nt.mak文件中的CFLAG,确定编译MT、MD库。重编的时候把生成物删掉。


    RSA加解密需要先用openssl工具生成RSA公钥和RSA私钥。方法:
    1、生成密匙文件(包含公钥和私钥):openssl genrsa -out privkey.pem 1024
    2、提取密匙文件中的公钥:openssl rsa -in privkey.pem -pubout
    1024只是测试用,使用2048位才比较安全。


    RSA加密部分代码demo:

    std::string EncodeRSAKeyFile( const std::string& strPemFileName, const std::string& strData )
    {
    if (strPemFileName.empty() || strData.empty())
    {
    assert(false);
    return "";
    }
    FILE* hPubKeyFile = NULL;
    if(fopen_s(&hPubKeyFile, strPemFileName.c_str(), "rb") || hPubKeyFile == NULL)
    {
    assert(false);
    return "";
    }
    std::string strRet;
    RSA* pRSAPublicKey = RSA_new();
    if(PEM_read_RSA_PUBKEY(hPubKeyFile, &pRSAPublicKey, 0, 0) == NULL)
    {
    assert(false);
    return "";
    }

    int nLen = RSA_size(pRSAPublicKey);
    char* pEncode = new char[nLen + 1];
    int ret = RSA_public_encrypt(strData.length(), (const unsigned char*)strData.c_str(), (unsigned char*)pEncode, pRSAPublicKey, RSA_PKCS1_PADDING);
    if (ret >= 0)
    {
    strRet = std::string(pEncode, ret);
    }
    delete[] pEncode;
    RSA_free(pRSAPublicKey);
    fclose(hPubKeyFile);
    CRYPTO_cleanup_all_ex_data();
    return strRet;
    }

    RSA解密部分代码demo:

    std::string DecodeRSAKeyFile( const std::string& strPemFileName, const std::string& strData )
    {
    if (strPemFileName.empty() || strData.empty())
    {
    assert(false);
    return "";
    }
    FILE* hPriKeyFile = NULL;
    if(fopen_s(&hPriKeyFile, strPemFileName.c_str(),"rb") || hPriKeyFile == NULL)
    {
    assert(false);
    return "";
    }
    std::string strRet;
    RSA* pRSAPriKey = RSA_new();
    if(PEM_read_RSAPrivateKey(hPriKeyFile, &pRSAPriKey, 0, 0) == NULL)
    {
    assert(false);
    return "";
    }
    int nLen = RSA_size(pRSAPriKey);
    char* pDecode = new char[nLen+1];

    int ret = RSA_private_decrypt(strData.length(), (const unsigned char*)strData.c_str(), (unsigned char*)pDecode, pRSAPriKey, RSA_PKCS1_PADDING);
    if(ret >= 0)
    {
    strRet = std::string((char*)pDecode, ret);
    }
    delete [] pDecode;
    RSA_free(pRSAPriKey);
    fclose(hPriKeyFile);
    CRYPTO_cleanup_all_ex_data();
    return strRet;
    }

    RSA的API中当使用参数RSA_PKCS1_PADDING时,明文长度不能大于密文长度-11;
    当使用参数RSA_NO_PADDING时,明文长度需要正好是128。

  • 相关阅读:
    sas中一些小的选项的含义
    C++变量学习点
    sas,log,output,ods输出管理(html output_object output_statement)
    matlab统计函数
    sas条件判断语句where,if的区别,以及where选项
    sas数组,数组的语法与一些特殊定义,获取维度大小
    sas赋值语句,累加语句,keep,drop,rename,(retain/sum statement)
    解决Xcode 4.3.2的"Could not insert new outlet connection"问题
    网络数据的XML解析
    将UIView中的图像保存到相册
  • 原文地址:https://www.cnblogs.com/huhu0013/p/4794056.html
Copyright © 2020-2023  润新知