看到這篇文章的同學可幸福了,當時在做RSA加密與簽名的時候網(wǎng)上的資料簡直不要太老,做完后實在是忍受不下去了,這篇文章我會詳細講解iOS如何實現(xiàn)RSA加密與簽名,并且與Java完全同步,這是我的第二篇博客,若有什么不足之處還請大家指教。
基礎知識
什么是RSA?
答:RSA是一種非對稱加密算法,常用來對傳輸數(shù)據(jù)進行加密,配合上數(shù)字摘要算法,也可以進行文字簽名。
RSA加密中padding?
答:padding即填充方式,由于RSA加密算法中要加密的明文是要比模數(shù)小的,padding就是通過一些填充方式來限制明文的長度。后面會詳細介紹padding的幾種模式以及分段加密。
加密和加簽有什么區(qū)別?
答:加密:公鑰放在客戶端,并使用公鑰對數(shù)據(jù)進行加密,服務端拿到數(shù)據(jù)后用私鑰進行解密;
加簽:私鑰放在客戶端,并使用私鑰對數(shù)據(jù)進行加簽,服務端拿到數(shù)據(jù)后用公鑰進行驗簽。
前者完全為了加密;后者主要是為了防惡意攻擊,防止別人模擬我們的客戶端對我們的服務器進行攻擊,導致服務器癱瘓。
基本原理
RSA使用“密鑰對”對數(shù)據(jù)進行加密解密,在加密解密前需要先生存公鑰(Public Key)和私鑰(Private Key)。
公鑰(Public key): 用于加密數(shù)據(jù). 用于公開, 一般存放在數(shù)據(jù)提供方, 例如iOS客戶端。
私鑰(Private key): 用于解密數(shù)據(jù). 必須保密, 私鑰泄露會造成安全問題。
iOS中的Security.framework提供了對RSA算法的支持,這種方式需要對密匙對進行處理, 根據(jù)public key生成證書, 通過private key生成p12格式的密匙。想想jave直接用字符串進行加密解密簡單多了。(⊙o⊙)…
實戰(zhàn)
證書生成
RSA加密這塊公鑰、私鑰必不可少的。Apple是不支持直接使用字符串進行加密解密的,推薦使用p12文件。這邊教大家去生成在加密中使用到的所有文件,并提供給Java使用,想當年這個公鑰私鑰搞了半天了。 %>_<%
生成模長為1024bit的私鑰
openssl genrsa -out private_key.pem 1024
生成certification require file
openssl req -new -key private_key.pem -out rsaCertReq.csr
生成certification 并指定過期時間
openssl x509 -req -days3650-in rsaCertReq.csr -signkey private_key.pem -out rsaCert.crt
生成公鑰供iOS使用
openssl x509 -outform der -in rsaCert.crt -out public_key.der
生成私鑰供iOS使用 這邊會讓你輸入密碼,后期用到在生成secKeyRef的時候會用到這個密碼
openssl pkcs12 -export -out private_key.p12 -inkey private_key.pem -in rsaCert.crt
生成pem結(jié)尾的公鑰供Java使用
openssl rsa -in private_key.pem -out rsa_public_key.pem -pubout
生成pem結(jié)尾的私鑰供Java使用openssl pkcs8 -topk8 -in private_key.pem -out pkcs8_private_key.pem -nocrypt
以上所有的步驟都是在終端下完成的哦 (^__^)
生成公鑰和私鑰的secKeyRef
//根據(jù)你的p12文件生成私鑰對應的SecKeyRef 這邊返回若是nil 請檢查你p12文件的生成步驟
- (SecKeyRef)getPrivateKeyRefrenceFromData:(NSData*)p12Data password:(NSString*)password {
SecKeyRef privateKeyRef = NULL;
NSMutableDictionary * options = [[NSMutableDictionary alloc] init];
[options setObject: password forKey:(__bridge id)kSecImportExportPassphrase];
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
OSStatus securityError = SecPKCS12Import((__bridge CFDataRef) p12Data, (__bridge CFDictionaryRef)options, &items);
if (securityError == noErr && CFArrayGetCount(items) > 0) {
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
SecIdentityRef identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity);
securityError = SecIdentityCopyPrivateKey(identityApp, &privateKeyRef);
if (securityError != noErr) {
privateKeyRef = NULL;
}
}
CFRelease(items);
return privateKeyRef;
}
-
//根據(jù)你的der文件公鑰對應的SecKeyRef
- (SecKeyRef)getPublicKeyRefrenceFromeData:? ? (NSData*)derData {
SecCertificateRef myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)derData);
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
SecTrustRef myTrust;
OSStatus status = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust);
SecTrustResultType trustResult;
if (status == noErr) {
status = SecTrustEvaluate(myTrust, &trustResult);
}
SecKeyRef securityKey = SecTrustCopyPublicKey(myTrust);
CFRelease(myCertificate);
CFRelease(myPolicy);
CFRelease(myTrust);
return securityKey;
}
加密與解密
- (NSData*)rsaEncryptData:(NSData*)data {
SecKeyRef key = [self getPublicKey];
size_t cipherBufferSize = SecKeyGetBlockSize(key);
uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
size_t blockSize = cipherBufferSize - 11;
size_t blockCount = (size_t)ceil([data length] / (double)blockSize);
NSMutableData *encryptedData = [[NSMutableData alloc] init];
for (int i=0; i