openssl解加密,数字签证

本文介绍了如何利用OpenSSL库进行数据加密的过程,包括库的引入、配置步骤及使用OC语言实现加密与解密的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每当心情不好的时候吧,就想来写写博客。

也有一段时间没更新博客了,忙于浮躁是不想写博客的最大的问题。

最近一直在写绘五线谱底层接口,还是想把这个东西弄出来记录下的。

看时间吧,如果有空就做个专题。

服务端最近开始抽了一样给之前的接口做安全功能,索性填坑的过程中也发一篇博客以此慰藉自己。


opensll加密:

首先得要这个opensll库,平台不带自己下。

链接我贴到网盘。

链接:http://pan.baidu.com/s/1gfdPyxl 密码:za7r

.a文件直接拖到工程目录下,

opensll里面的.c文件放入class里面就好了。


这里最重要的还是设置。

找到Build Setting里面的 User Header 加入$(PROJECT_DIR)/../Classes 

Library Search Paths 加入$(SRCROOT)/../Classes

Header Search Paths 加入$(inherited) 在加入$(SRCROOT)/../Classes

这是针对于目录放的, 如果这样做还是找不到头文件 那就要自己考虑层级关系了,找到openssl的根目录就好


好的,那么库已经弄好了,那就可以根据不同语言在Git上找库了

这里我拿的是oc的一套库,主要是太懒了,同事有现成的,我就直接拿过来做了个和C++桥接的类就去用了

记得桥接类必须是在.mm里面#import,别问我为什么。

类库也在网盘中

//
//  ObjectHelper.hpp
//  Intelligent_piano
//
//  Created by 易宥佑 on 16/6/27.
//
//

#ifndef ObjectHelper_h
#define ObjectHelper_h

class ObjectHelper
{
public:
    static ObjectHelper* getInstance();
    static ObjectHelper* m_getToolclass;
    //加密
    std::string encryptString(std::string str ,std::string publickey);
    //解密
    std::string decryptString(std::string str, std::string publickey);
    //获取时间戳
    std::string GetCalculateTime();
    //加密MD5
    std::string md5StringForString(std::string str);
    //公钥
    std::string pubkey;
    //时间戳
    std::string m_time;
    
    void str_replace(std::string & str, const std::string & strsrc, const std::string &strdst);
    
private:
    
};

#endif /* ObjectHelper_h */

//
//  ObjectHelper.cpp
//  Intelligent_piano
//
//  Created by 易宥佑 on 16/6/27.
//
//

#include "ObjectHelper.h"
#import "RSA.h"
#import "sign.h"
ObjectHelper* ObjectHelper::m_getToolclass = nullptr;


ObjectHelper* ObjectHelper::getInstance()
{
    if (!m_getToolclass)
    {
        m_getToolclass = new ObjectHelper();
    }
    return m_getToolclass;
}

std::string ObjectHelper::encryptString(std::string str, std::string publickey)
{
    NSString *stroc = [NSString stringWithCString:str.c_str()
                                                encoding:[NSString defaultCStringEncoding]];
    
    NSString *pubkeyoc = [NSString stringWithCString:publickey.c_str()
                                         encoding:[NSString defaultCStringEncoding]];
    
    
    
    NSString *ans = [RSA encryptString:stroc publicKey:pubkeyoc];
    std::string strC=  [ans UTF8String];
    str_replace(strC , "+","%2b");
    str_replace(strC, "/", "%2f");
    return strC;
}

std::string ObjectHelper::decryptString(std::string str, std::string publickey)
{
    NSString *stroc = [NSString stringWithCString:str.c_str()
                                         encoding:[NSString defaultCStringEncoding]];
    
    NSString *pubkeyoc = [NSString stringWithCString:publickey.c_str()
                                            encoding:[NSString defaultCStringEncoding]];
    
    NSString *ans = [RSA decryptString:stroc publicKey:pubkeyoc];
    std::string strC=  [ans UTF8String];
    return strC;
}


std::string ObjectHelper::GetCalculateTime()
{
    NSString* time =[sign CalculateTime];
    m_time = [time UTF8String];
    return m_time;
}


std::string ObjectHelper::md5StringForString(std::string str)
{
    //先第一个元素加 xxxx数字签名
    str.insert(1, "xxxx");
    NSString* md5 =[NSString stringWithCString:str.c_str()
                                      encoding:[NSString defaultCStringEncoding]];
    NSString* nmd5 =[sign md5StringForString:md5];
    return [nmd5 UTF8String];
}

void ObjectHelper::str_replace(std::string & str, const std::string & strsrc, const std::string &strdst)
{
    std::string::size_type pos = 0;//位置
    std::string::size_type srclen = strsrc.size();//要替换的字符串大小
    std::string::size_type dstlen = strdst.size();//目标字符串大小
    while((pos = str.find(strsrc,pos)) != std::string::npos)
    {
        str.replace(pos,srclen,strdst);
        pos += dstlen; 
    } 
}

这里有几个注意的地方,一个是url的编码, 我是用的一个很取巧的方法做的,具体可以参考html编码格式 譬如“+”变成“%2b”

整个流程是加密传数据+数字签名(时间戳+MD5)

获取数据,解密。

当然这需要和服务端沟通好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值