RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数组合成私钥。公钥是可发布的供任何人使用,私钥则为自己所有,供解密之用。
RSA加密算法
支付宝提供的Demo执行流程如下:
(仅作理解之用,里面还涉及OpenSSL相关文件的调用,暂时不具体分析)
step1:给订单对象Oder各属性赋值,改写Oder的description方法,拼接订单信息字符串。
//将商品信息赋予AlixPayOrder的成员变量
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
#warning TODO:服务器提供相关交易信息
order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)
order.productName = self.product.subject; //商品标题
order.productDescription = self.product.body; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f",self.product.price]; //商品价格
order.notifyURL = @"http://www.xxx.com"; //回调URL
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1"; //默认是1
order.inputCharset = @"utf-8";
order.itBPay = @"30m"; //订单过期时间30分钟
order.showUrl = @"m.alipay.com";
//将商品信息拼接成字符串
NSString *orderSpec = [order description];
step2:用私钥生成签名器signer
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
//所调用的工厂方法
id<DataSigner> CreateRSADataSigner(NSString *privateKey) {
return [[RSADataSigner alloc] initWithPrivateKey:privateKey];
}
//所调用的初始化方法
- (id)initWithPrivateKey:(NSString *)privateKey {
if (self = [super init]) {
_privateKey = [privateKey copy];
}
return self;
}
step3:给订单信息生成签名
NSString *signedString = [signer signString:orderSpec];
//该签名方法仅供参考,外部商户可用自己方法替换
- (NSString *)signString:(NSString *)string {
//在Document文件夹下创建私钥文件
NSString *signedString = nil;
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentPath stringByAppendingPathComponent:@"AlixPay-RSAPrivateKey"];
//把密钥写入文件
NSString *formatKey = [self formatPrivateKey:_privateKey];
[formatKey writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
const char *message = [string cStringUsingEncoding:NSUTF8StringEncoding];
int messageLength = strlen(message);
unsigned char *sig = (unsigned char *)malloc(256);
unsigned int sig_len;
//该方法为 openssl_wrapper.h中的方法
int ret = rsa_sign_with_private_key_pem((char *)message, messageLength, sig, &sig_len, (char *)[path UTF8String]);
//签名成功,需要给签名字符串base64编码和UrlEncode,该两个方法也可以根据情况替换为自己函数
if (ret == 1) {
NSString *base64String = base64StringFromData([NSData dataWithBytes:sig length:sig_len]);
//NSData *UTF8Data = [base64String dataUsingEncoding:NSUTF8StringEncoding];
signedString = [self urlEncodedString:base64String];
}
free(sig);
return signedString;
}
格式化�私钥
- (NSString *)formatPrivateKey:(NSString *)privateKey {
const char *pstr = [privateKey UTF8String];
int len = [privateKey length];
NSMutableString *result = [NSMutableString string];
[result appendString:@"-----BEGIN PRIVATE KEY-----\n"];
int index = 0;
int count = 0;
while (index < len) {
char ch = pstr[index];
if (ch == '\r' || ch == '\n') {
++index;
continue;
}
[result appendFormat:@"%c", ch];
if (++count == 79) {
[result appendString:@"\n"];
count = 0;
}
index++;
}
[result appendString:@"\n-----END PRIVATE KEY-----"];
return result;
}