在说代码之前,先简单说一下我们开发App经常遇到的两种加密方式:  MD5 和RSA,今天我主要介绍的是RSA。



 安全签名机制


 MD5安全签名机制说明

MD5安全签名机制是商户和连连银通约定一个签名key,每次在做签名时将key=value附在待签名字符串后面,然后经MD5加密运算后得到一个签名串,商户和连连银通在检验时也采用同样的方式得到签名

串,经比对后确定是否一致,如果一致,则签名通过。



RSA 安全签名机制说明

在RSA签名时,需要私钥和公钥一起参与签名。私钥与公钥皆是客户通过OPENSSL来生成得出的。客户把生成出的公钥与连连银通的技术人员配置好的连连支付的公钥做交换。因此,在签名时,客户要用到的是客户的私钥及连连银通的公钥。
支付请求时签名商户当拿到请求时的待签名字符串后,把待签名字符串与商户的私钥一同放入RSA 签名函数中进行签名运算,从而得到签名结果字符串。通知或返回时验证签名商户当获得到通知或返回时的待签名字符串后,把待签名字符串、连连银通提供的公钥、连连支付通知返回参数中的参数sign的值三者一同放入RSA签名函数中进行非对称的签名运算,来判断签名是否验证通过。

需要参与签名的参数

直接把请求数据或者返回参数中的所有元素(除sign本身)按照“key值=value值”的格式拼接起来,并且把这些拼接以后的元素以“&”字符再连接起来(顺序按首字母升序排列,值为空的不参与签名),如:

这段字符串即是商户支付请求时的待签名字符串。


接下来进入正题 :

 1、 首先需要服务器那边提供给我们一个“密码加密的公钥”格式是".pem",我们需要把这个公钥拖入到我们的工程中。

 2、我们需要导入openssl.framework框架

 3、需要导入我封装的这两个类:

第一个CRSA.h
#import <Foundation/Foundation.h>
 #include <openssl/rsa.h>
 #include <openssl/pem.h>
 #include <openssl/err.h>

 typedef enum {
     KeyTypePublic,
     KeyTypePrivate
 }KeyType;

 typedef enum {
     RSA_PADDING_TYPE_NONE       = RSA_NO_PADDING,
     RSA_PADDING_TYPE_PKCS1      = RSA_PKCS1_PADDING,
     RSA_PADDING_TYPE_SSLV23     = RSA_SSLV23_PADDING
 }RSA_PADDING_TYPE;

 @interface CRSA : NSObject{
     RSA *_rsa;
 }
 + (id)shareInstance;
 - (BOOL)importRSAKeyWithType:(KeyType)type;
 - (int)getBlockSizeWithRSA_PADDING_TYPE:(RSA_PADDING_TYPE)padding_type;
 - (NSString *) encryptByRsa:(NSString*)content withKeyType:(KeyType)keyType;
 - (NSString *) decryptByRsa:(NSString*)content withKeyType:(KeyType)keyType;
 @end
第一个CRSA.m


#import "CRSA.h"
 #import <Foundation/Foundation.h>
 #define BUFFSIZE  1024
 #import "NSData+Base64.h"
 #define PADDING RSA_PADDING_TYPE_PKCS1
 @implementation CRSA

 + (id)shareInstance
 {
     static CRSA *_crsa = nil;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         _crsa = [[self alloc] init];
     });
     return _crsa;
 }

 - (BOOL)importRSAKeyWithType:(KeyType)type
 {
     FILE *file;
     NSString *keyName = type == KeyTypePublic ? @"api_public_key" : @"api_public_key";
     NSString *keyPath = [[NSBundle mainBundle] pathForResource:keyName ofType:@"pem"];
     
     file = fopen([keyPath UTF8String], "rb");
     
     if (NULL != file)
     {
         if (type == KeyTypePublic)
         {
             _rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL);
             assert(_rsa != NULL);
         }
         else
         {
             _rsa = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
             assert(_rsa != NULL);
         }
         
         fclose(file);
         
         return (_rsa != NULL) ? YES : NO;
     }
     
     return NO;
 }

 - (NSString *) encryptByRsa:(NSString*)content withKeyType:(KeyType)keyType
 {
     if (![self importRSAKeyWithType:keyType])
         return nil;
     
     int status;
     int length  = [content length];
     unsigned char input[length + 1];
     bzero(input, length + 1);
     int i = 0;
     for (; i < length; i++)
     {
         input[i] = [content characterAtIndex:i];
     }
     
     NSInteger  flen = [self getBlockSizeWithRSA_PADDING_TYPE:PADDING];
     
     char *encData = (char*)malloc(flen);
     bzero(encData, flen);
     
     switch (keyType) {
         case KeyTypePublic:
             status = RSA_public_encrypt(length, (unsigned char*)input, (unsigned char*)encData, _rsa, PADDING);
             break;
             
         default:
             status = RSA_private_encrypt(length, (unsigned char*)input, (unsigned char*)encData, _rsa, PADDING);
             break;
     }
     
     if (status)
     {
         NSData *returnData = [NSData dataWithBytes:encData length:status];
         free(encData);
         encData = NULL;
         
         NSString *ret = [returnData base64Encoding];
         return ret;
     }
     
     free(encData);
     encData = NULL;
     
     return nil;
 }
 - (NSString *) decryptByRsa:(NSString*)content withKeyType:(KeyType)keyType
 {
     if (![self importRSAKeyWithType:keyType])
         return nil;
     
     int status;
     
     NSData *data = [NSData dataWithBase64EncodedString:content];
     int length = [data length];
     
     NSInteger flen = [self getBlockSizeWithRSA_PADDING_TYPE:PADDING];
   char *decData = (char*)malloc(flen);
     bzero(decData, flen);
     
     switch (keyType) {
         case KeyTypePublic:
             status = RSA_public_decrypt(length, (unsigned char*)[data bytes], (unsigned char*)decData, _rsa, PADDING);
             break;
             
         default:
             status = RSA_private_decrypt(length, (unsigned char*)[data bytes], (unsigned char*)decData, _rsa, PADDING);
             break;
     }
     
     if (status)
     {
         NSMutableString *decryptString = [[NSMutableString alloc] initWithBytes:decData length:strlen(decData) encoding:NSASCIIStringEncoding];
         free(decData);
         decData = NULL;
         
         return decryptString;
     }
     
     free(decData);
     decData = NULL;
     
     return nil;
 }

 - (int)getBlockSizeWithRSA_PADDING_TYPE:(RSA_PADDING_TYPE)padding_type
 {
     int len = RSA_size(_rsa);
     
     if (padding_type == RSA_PADDING_TYPE_PKCS1 || padding_type == RSA_PADDING_TYPE_SSLV23) {
         len -= 11;
     }
     
     return len;
 }
 @end
第二个类 :NSData+Base64.h
 #import <Foundation/Foundation.h>

 @interface NSData (Base64)

 + (id)dataWithBase64EncodedString:(NSString *)string;     //  Padding '=' characters are optional. Whitespace is ignored.
 - (NSString *)base64Encoding;

 @end
第二个类NSData+Base64.m
 #import <Foundation/Foundation.h>
 static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

 @implementation NSData (Base64)

 + (id)dataWithBase64EncodedString:(NSString *)string;
 {
     if (string == nil)
         [NSException raise:NSInvalidArgumentException format:nil];
     if ([string length] == 0)
         return [NSData data];
     
     static char *decodingTable = NULL;
     if (decodingTable == NULL)
     {
         decodingTable = malloc(256);
         if (decodingTable == NULL)
             return nil;
         memset(decodingTable, CHAR_MAX, 256);
         NSUInteger i;
         for (i = 0; i < 64; i++)
             decodingTable[(short)encodingTable[i]] = i;
     }
     
     const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
     if (characters == NULL)     //  Not an ASCII string!
         return nil;
     char *bytes = malloc((([string length] + 3) / 4) * 3);
     if (bytes == NULL)
         return nil;
     NSUInteger length = 0;

     NSUInteger i = 0;
     while (YES)
     {
         char buffer[4];
         short bufferLength;
         for (bufferLength = 0; bufferLength < 4; i++)
         {
             if (characters[i] == '\0')
                 break;
             if (isspace(characters[i]) || characters[i] == '=')
                 continue;
             buffer[bufferLength] = decodingTable[(short)characters[i]];
             if (buffer[bufferLength++] == CHAR_MAX)      //  Illegal character!
             {
                 free(bytes);
                 return nil;
             }
         }
         
         if (bufferLength == 0)
             break;
         if (bufferLength == 1)      //  At least two characters are needed to produce one byte!
         {
             free(bytes);
             return nil;
         }
         
         //  Decode the characters in the buffer to bytes.
         bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
         if (bufferLength > 2)
             bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
         if (bufferLength > 3)
             bytes[length++] = (buffer[2] << 6) | buffer[3];
     }
     
     realloc(bytes, length);
     return [NSData dataWithBytesNoCopy:bytes length:length];
 }

 - (NSString *)base64Encoding;
 {
     if ([self length] == 0)
         return @"";

     char *characters = malloc((([self length] + 2) / 3) * 4);
     if (characters == NULL)
         return nil;
     NSUInteger length = 0;
     
     NSUInteger i = 0;
     while (i < [self length])
     {
         char buffer[3] = {0,0,0};
         short bufferLength = 0;
         while (bufferLength < 3 && i < [self length])
             buffer[bufferLength++] = ((char *)[self bytes])[i++];
         
         //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
         characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
         characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
         if (bufferLength > 1)
             characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
         else characters[length++] = '=';
         if (bufferLength > 2)
             characters[length++] = encodingTable[buffer[2] & 0x3F];
         else characters[length++] = '=';    
     }
     
     return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
 }

 @end
最后也是最简单地调用方法了: 在你需要调用的类里面导入头文件#import "CRSA.h"
写下面这两行代码就可以对我们的字符串(我这里是登录密码)进行RSA加密了:
        CRSA *crsa = [CRSA shareInstance];
         NSString *password = [crsa encryptByRsa:self.loginCode.text withKeyType:KeyTypePublic];//需要说明的是KeyTypePublic这个是结构体,我们前端向服务器发送加密以后的密码要用到公钥,服务器用对应的私钥进行解密,一对一的。

以上就是我开发项目中用到的RSA 加密,希望能对大家用帮助。