RSA算法及其在iOS中的使用


因為項目中需要傳輸用戶密碼,為了安全需要用RSA加密,所以就學習了下RSA加密在iOS中的應用。
關於RSA的歷史及原理,下面的兩篇文章講的很清楚了:
 
簡單來說,RSA建立在一個數學難題之上,就是大數分解:將兩個大素數相乘十分容易,但是想要對其乘積進行因式分解卻極其困難。至於為什么難,難在哪里那就是數學家的事了。。。
明白了這個就可以大致知道RSA的原理:非對稱加密
(1)乙方生成兩把密鑰(公鑰和私鑰)。公鑰是公開的,任何人都可以獲得,私鑰則是保密的。
(2)甲方獲取乙方的公鑰,然后用它對信息加密。
(3)乙方得到加密后的信息,用私鑰解密。
 
就好比有一套特殊的鎖和鑰匙,鎖是公開的,誰都可以拿這個鎖來鎖住他的東西,只有有鑰匙的人可以打開。
那么問題來了,既然鎖是公開的,難道不能通過鎖的結構來倒推出鑰匙的形狀嗎?
答案是:不能!因為這個鎖是特殊的,它就特殊在很難倒推。(這個倒不是絕對的,也許將來某一天大數分解的數學難題解決了,這種算法就不安全了,詳見開頭鏈接)
 
我遇到的應用場景是,客戶端有服務器的公鑰,客戶端要把用戶的密碼用公鑰加密上后上傳到服務器,服務器可以用私鑰解密。
所以客戶端要做的是,將需要加密的內容用服務器給的公鑰進行RSA加密。
iOS上並沒有直接的RSA加密API,所以需要折騰一下。
gitHub上的代碼大同小異,主要是三個方法(抄自 https://github.com/ideawu/Objective-C-RSA
注意代碼里有個kSecPaddingPKCS1是作者寫死的,而我們的項目中需要傳kSecPaddingNone才行!!!
 
+ (NSData *)stripPublicKeyHeader:(NSData *)d_key{
// Skip ASN.1 public key header
if (d_key == nil) return(nil);

unsigned long len = [d_key length];
if (!len) return(nil);

unsigned char *c_key = (unsigned char *)[d_key bytes];
unsigned int  idx    = 0;

if (c_key[idx++] != 0x30) return(nil);

if (c_key[idx] > 0x80) idx += c_key[idx] - 0x80 + 1;
else idx++;

// PKCS #1 rsaEncryption szOID_RSA_RSA
static unsigned char seqiod[] =
{ 0x30,   0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
0x01, 0x05, 0x00 };
if (memcmp(&c_key[idx], seqiod, 15)) return(nil);

idx += 15;

if (c_key[idx++] != 0x03) return(nil);

if (c_key[idx] > 0x80) idx += c_key[idx] - 0x80 + 1;
else idx++;

if (c_key[idx++] != '\0') return(nil);

// Now make a new NSData from this buffer
return([NSData dataWithBytes:&c_key[idx] length:len - idx]);
}

 

 
 
+ (SecKeyRef)addPublicKey:(NSString *)key{
NSRange spos = [key rangeOfString:@"-----BEGIN PUBLIC KEY-----"];
NSRange epos = [key rangeOfString:@"-----END PUBLIC KEY-----"];
if(spos.location != NSNotFound && epos.location != NSNotFound){
NSUInteger s = spos.location + spos.length;
NSUInteger e = epos.location;
NSRange range = NSMakeRange(s, e-s);
key = [key substringWithRange:range];
}
key = [key stringByReplacingOccurrencesOfString:@"\r" withString:@""];
key = [key stringByReplacingOccurrencesOfString:@"\n" withString:@""];
key = [key stringByReplacingOccurrencesOfString:@"\t" withString:@""];
key = [key stringByReplacingOccurrencesOfString:@" "  withString:@""];

// This will be base64 encoded, decode it.
NSData *data = base64_decode(key);
data = [RSA stripPublicKeyHeader:data];
if(!data){
return nil;
}

NSString *tag = @"what_the_fuck_is_this";
NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];

// Delete any old lingering key with the same tag
NSMutableDictionary *publicKey = [[NSMutableDictionary alloc] init];
[publicKey setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
SecItemDelete((__bridge CFDictionaryRef)publicKey);

// Add persistent version of the key to system keychain
[publicKey setObject:data forKey:(__bridge id)kSecValueData];
[publicKey setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)
kSecAttrKeyClass];
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)
kSecReturnPersistentRef];

CFTypeRef persistKey = nil;
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKey, &persistKey);
if (persistKey != nil){
CFRelease(persistKey);
}
if ((status != noErr) && (status != errSecDuplicateItem)) {
return nil;
}

[publicKey removeObjectForKey:(__bridge id)kSecValueData];
[publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
[publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
[publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];

// Now fetch the SecKeyRef version of the key
SecKeyRef keyRef = nil;
status = SecItemCopyMatching((__bridge CFDictionaryRef)publicKey, (CFTypeRef *)&keyRef);
if(status != noErr){
return nil;
}
return keyRef;
}

 

 
 1 + (NSString *)encryptData:(NSData *)data publicKey:(NSString *)pubKey{
 2 if(!data || !pubKey){
 3 return nil;
 4 }
 5 SecKeyRef keyRef = [RSA addPublicKey:pubKey];
 6 if(!keyRef){
 7 return nil;
 8 }
 9 
10 const uint8_t *srcbuf = (const uint8_t *)[data bytes];
11 size_t srclen = (size_t)data.length;
12 
13 size_t outlen = SecKeyGetBlockSize(keyRef) * sizeof(uint8_t);
14 if(srclen > outlen - 11){
15 CFRelease(keyRef);
16 return nil;
17 }
18 void *outbuf = malloc(outlen);
19 
20 OSStatus status = noErr;
21 status = SecKeyEncrypt(keyRef,
22   kSecPaddingNone, //原作者寫的是kSecPaddingPKCS1,經春哥研究這里寫成kSecPaddingNone才符合我們使用
23   srcbuf,
24   srclen,
25   outbuf,
26   &outlen
27   );
28 NSString *ret = nil;
29 if (status != 0) {
30 //NSLog(@"SecKeyEncrypt fail. Error Code: %ld", status);
31 }else{
32 NSData *data = [NSData dataWithBytes:outbuf length:outlen];
33 ret = base64_encode_data(data);
34 }
35 free(outbuf);
36 CFRelease(keyRef);
37 return ret;
38 }

 

 
還有一篇文章可以參考: http://blog.iamzsx.me/show.html?id=155002
 
 
簽名機制
僅僅加密某個參數是不夠的,還需要保證請求沒有被篡改,所以簽名機制就很有必要。
比較簡單和常用就是MD5簽名:
拿到待簽名的字符串A(比如某個url),將其與服務器約定好的密鑰拼成新的字符串B,對B進行MD5算法得到簽名C,
然后將C作為A的簽名一起發送到服務器。
服務器收到請求后,對A用與客戶端約定好的密鑰進行相同的算法得到C’,如果C==C’,那就說明改請求沒有被篡改過,
否則驗證不通過
 
當然也可以做RSA簽名
這個要比MD5簽名要稍微麻煩一點,因為需要客戶端生成公鑰私鑰對,基本流程也和MD5簽名一樣
拿到待簽名的字符串A(比如某個url),將其用私鑰加密得到的字符串B,然后將B和原數據A還有自己的公鑰一起發送給服務器,
服務器收到請求,用公鑰解密得到B',如果B==B',則說明原數據沒有被篡改過,否則驗證不通過。
 
也有說這里得到B以后,需要再用服務器的公鑰加密一遍得到C,將C和原數據和自己的公鑰一起發送給服務器,
服務器收到之后,現需要用自己的私鑰解密一遍得到C',然后再用客戶端公鑰解密得到B',然后同上。。。
 
RSA簽名及驗證我還沒用到,所以具體怎么實現的還需要研究下,待補充!!!
 
 
HTTPS
https算是對RSA加密的一個典型應用吧,不過這個服務器的公鑰私鑰不是自己生產的,而是CA頒發的。
具體原理網上很多,其中一個: http://jingyan.baidu.com/article/2fb0ba4048e15500f3ec5f7e.html
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM