报文加密

应用层自定义通讯(报文)加密,有两种调用方式,客户端调用/weex层调用

客户端调用方式

1.注册自定义加密类型

    //madp_statue_finish中注册
    MADSingleClass.getInstance.madp_statue_finish = ^(id z) {
        NSInteger type = 3;
        //type = 3 对url/header/body处理
        //type = 1 对body处理
        [MADSingleClass.getInstance registryCustomEncryption:type];
    };

2.完善加解密回调

/**
 自定义报文加解密
  包含两种类型,A:只操作body;B:需要借助于url/header/body
  如果使用A类型:
  encryptionsCallBack  输入端为需要加密的body,输出端为加密后的body;
  decryptionsCallBack  输入端为待解密的body,输出端为解密后的body;
  如果使用B类型:
  encryptionsCallBack 输入端为body,输出为{Url:"",Header:{需要添加的key-vlue},Body:"加密后的body"}
  decryptionsCallBack  输入端为{Url:"",Header:{},Body:""},输出端为解密后的body;
  
  使用B类型,加密的body回传时需要转为Base64格式,该处理只是sdk内部的操作,不涉及服务端的特殊处理。
 */

当type = 1时

    //解密
    [MADSingleClass getInstance].decryptionsCallBack = ^NSData *(NSData *p0) {
        NSData *data;
        NSString *bodyStr = [[NSString alloc]initWithData:p0 encoding:NSUTF8StringEncoding];

        char *pszBuff = (char *)[bodyStr UTF8String];
        char* pszDecryptBuff = [powercrypto getPlainValue:pszBuff];
        if (pszDecryptBuff == NULL) {
            WXLogInfo(@"解密失败");
            return p0;
        }
        data = [[NSData alloc]initWithBytes:pszDecryptBuff length:strlen(pszDecryptBuff)];
        
        return data;
    };
    //加密
    [MADSingleClass getInstance].encryptionsCallBack = ^NSData *(NSData *p0) {
        NSData *data;
        
        NSString *bodyStr = [[NSString alloc]initWithData:p0 encoding:NSUTF8StringEncoding];

        char* pszBuff = NULL;

        char* key = "KEY-VALUE";

        pszBuff = [powercrypto getCipherValue:(char *)[bodyStr UTF8String] publicKey:key];
        if (pszBuff == NULL) {
            WXLogInfo(@"加密失败");
            return p0;
        }
        data = [[NSData alloc]initWithBytes:pszBuff length:strlen(pszBuff)];
        
        if (pszBuff) {
            free(pszBuff);
        }
        return data;
    };

当 type = 3时

解密:需要对得到的密文进行解码
加密:需要对得到的密文进行编码

    //解密
    [MADSingleClass getInstance].decryptionsCallBack = ^NSData *(NSData *p0) {
        
        NSString *decryptStr = [[NSString alloc]initWithData:p0 encoding:NSUTF8StringEncoding];
        NSMutableDictionary *dic = [[[MADSingleClass getInstance] parseJSONStringToNSDictionary:decryptStr] mutableCopy];
        //对加密的报文进行Base64解码
        NSData *bodyData = [[NSData alloc] initWithBase64EncodedString:dic[@"Body"] options:0];
        char *pszBuff = (char *)[bodyData bytes];
        char* pszDecryptBuff = [powercrypto getPlainValue:pszBuff];
        
        if (pszDecryptBuff == NULL) {
            WXLogInfo(@"解密失败");
            return p0;
        }
        NSData *resultData = [[NSData alloc]initWithBytes:pszDecryptBuff length:strlen(pszDecryptBuff)];

        return resultData;
    };
    //加密
    [MADSingleClass getInstance].encryptionsCallBack = ^NSData *(NSData *p0) {

        NSString *encryptStr = [[NSString alloc]initWithData:p0 encoding:NSUTF8StringEncoding];
        NSMutableDictionary *dic = (NSMutableDictionary *)[[MADSingleClass getInstance] parseJSONStringToNSDictionary:encryptStr];
        char* pszBuff = NULL;
        char* key = "KEY-VALUE";
        if ([dic[@"Body"] length] < 0) {
            return p0;
        }
        WXLogInfo(@"上送报文:\n%@",dic[@"Body"]);
        pszBuff = [powercrypto getCipherValue:(char *)[dic[@"Body"] UTF8String] publicKey:key];
        if (pszBuff == NULL) {
            WXLogInfo(@"加密失败");
            return p0;
        }
        NSData *data = [[NSData alloc]initWithBytes:pszBuff length:strlen(pszBuff)];
        
        if (pszBuff) {
            free(pszBuff);
        }
        //对加密的报文进行Base64编码
        NSString *bodyStr = [data base64EncodedStringWithOptions:0];

        [dic setObject:bodyStr forKey:@"Body"];
        NSData *resultData =[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];

        return resultData;
    };

weex调用方式

1.通过module添加注册方法

WX_EXPORT_METHOD(@selector(registryEncryption))

2.在registryEncryption中注册自定义加解密类型

NSInteger type = 3;
//type = 3 对url/header/body处理
//type = 1 对body处理
[MADSingleClass.getInstance registryCustomEncryption:type];

3.在registryEncryption中 实现加解密

    /* 已type = 3 为例, 实现内容与客户端调用方式一致*/
    MADContext * context = ((WeexViewController *)self.weexInstance.viewController).ctx;    
    //解密
    context.decryptionsCallBack = ^NSData *(NSData *p0) {
    //以下为操作示例,具体项目具体分析
        NSString *decryptStr = [[NSString alloc]initWithData:p0 encoding:NSUTF8StringEncoding];
        NSMutableDictionary *dic = [[[MADSingleClass getInstance] parseJSONStringToNSDictionary:decryptStr] mutableCopy];
        //对加密的报文进行Base64解码
        NSData *bodyData = [[NSData alloc] initWithBase64EncodedString:dic[@"Body"] options:0];
        char *pszBuff = (char *)[bodyData bytes];
        char* pszDecryptBuff = [powercrypto getPlainValue:pszBuff];
        
        if (pszDecryptBuff == NULL) {
            WXLogInfo(@"解密失败");
            return p0;
        }
        NSData *resultData = [[NSData alloc]initWithBytes:pszDecryptBuff length:strlen(pszDecryptBuff)];
        return resultData;
    };
    //加密
    context.encryptionsCallBack = ^NSData *(NSData *p0) {
    //以下为操作示例,具体项目具体分析
        NSString *encryptStr = [[NSString alloc]initWithData:p0 encoding:NSUTF8StringEncoding];
        NSMutableDictionary *dic = (NSMutableDictionary *)[[MADSingleClass getInstance] parseJSONStringToNSDictionary:encryptStr];
        char* pszBuff = NULL;
        char* key = "KEY-VALUE";
        if ([dic[@"Body"] length] < 0) {
            return p0;
        }
        WXLogInfo(@"上送报文:\n%@",dic[@"Body"]);
        pszBuff = [powercrypto getCipherValue:(char *)[dic[@"Body"] UTF8String] publicKey:key];
        if (pszBuff == NULL) {
            WXLogInfo(@"加密失败");
            return p0;
        }
        NSData *data = [[NSData alloc]initWithBytes:pszBuff length:strlen(pszBuff)];
        
        if (pszBuff) {
            free(pszBuff);
        }
        //对加密的报文进行Base64编码
        NSString *bodyStr = [data base64EncodedStringWithOptions:0];

        [dic setObject:bodyStr forKey:@"Body"];
        NSData *resultData =[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];

        return resultData;
    }