├── RSAEncryptor.h └── RSAEncryptor.m /RSAEncryptor.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | // http://www.cnblogs.com/makemelike/articles/3802518.html 4 | 5 | 6 | 7 | #import 8 | 9 | @interface RSAEncryptor : NSObject 10 | 11 | #pragma mark - Instance Methods 12 | - (void) loadPublicKeyFromFile: (NSString*) derFilePath; 13 | - (void) loadPublicKeyFromData: (NSData*) derData; 14 | 15 | - (void) loadPrivateKeyFromFile: (NSString*) p12FilePath password:(NSString*)p12Password; 16 | - (void) loadPrivateKeyFromData: (NSData*) p12Data password:(NSString*)p12Password; 17 | 18 | - (NSString*) rsaEncryptString:(NSString*)string; 19 | - (NSData*) rsaEncryptData:(NSData*)data ; 20 | 21 | - (NSString*) rsaDecryptString:(NSString*)string; 22 | - (NSData*) rsaDecryptData:(NSData*)data; 23 | 24 | #pragma mark - Class Methods 25 | + (void) setSharedInstance: (RSAEncryptor*)instance; 26 | + (RSAEncryptor*) sharedInstance; 27 | 28 | @end -------------------------------------------------------------------------------- /RSAEncryptor.m: -------------------------------------------------------------------------------- 1 | 2 | // http://www.cnblogs.com/makemelike/articles/3802518.html 3 | 4 | #import "RSAEncryptor.h" 5 | #import 6 | 7 | 8 | @implementation RSAEncryptor{ 9 | SecKeyRef publicKey; 10 | SecKeyRef privateKey; 11 | } 12 | - (void)dealloc{ 13 | CFRelease(publicKey); 14 | CFRelease(privateKey); 15 | } 16 | - (SecKeyRef)getPublicKey { 17 | return publicKey; 18 | } 19 | - (SecKeyRef)getPrivateKey { 20 | return privateKey; 21 | } 22 | - (void)loadPublicKeyFromFile: (NSString*) derFilePath{ 23 | NSData *derData = [[NSData alloc] initWithContentsOfFile:derFilePath]; 24 | [self loadPublicKeyFromData: derData]; 25 | } 26 | - (void)loadPublicKeyFromData: (NSData*) derData{ 27 | publicKey = [self getPublicKeyRefrenceFromeData: derData]; 28 | } 29 | - (void)loadPrivateKeyFromFile: (NSString*) p12FilePath password:(NSString*)p12Password{ 30 | NSData *p12Data = [NSData dataWithContentsOfFile:p12FilePath]; 31 | [self loadPrivateKeyFromData: p12Data password:p12Password]; 32 | } 33 | - (void)loadPrivateKeyFromData: (NSData*) p12Data password:(NSString*)p12Password{ 34 | privateKey = [self getPrivateKeyRefrenceFromData: p12Data password: p12Password]; 35 | } 36 | 37 | #pragma mark - Private Methods 38 | - (SecKeyRef) getPublicKeyRefrenceFromeData: (NSData*)derData{ 39 | SecCertificateRef myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)derData); 40 | SecPolicyRef myPolicy = SecPolicyCreateBasicX509(); 41 | SecTrustRef myTrust; 42 | OSStatus status = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust); 43 | SecTrustResultType trustResult; 44 | if (status == noErr) { 45 | status = SecTrustEvaluate(myTrust, &trustResult); 46 | } 47 | SecKeyRef securityKey = SecTrustCopyPublicKey(myTrust); 48 | CFRelease(myCertificate); 49 | CFRelease(myPolicy); 50 | CFRelease(myTrust); 51 | 52 | return securityKey; 53 | } 54 | - (SecKeyRef) getPrivateKeyRefrenceFromData: (NSData*)p12Data password:(NSString*)password{ 55 | SecKeyRef privateKeyRef = NULL; 56 | NSMutableDictionary * options = [[NSMutableDictionary alloc] init]; 57 | [options setObject: password forKey:(__bridge id)kSecImportExportPassphrase]; 58 | CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); 59 | OSStatus securityError = SecPKCS12Import((__bridge CFDataRef) p12Data, (__bridge CFDictionaryRef)options, &items); 60 | if (securityError == noErr && CFArrayGetCount(items) > 0) { 61 | CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0); 62 | SecIdentityRef identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity); 63 | securityError = SecIdentityCopyPrivateKey(identityApp, &privateKeyRef); 64 | if (securityError != noErr) { 65 | privateKeyRef = NULL; 66 | } 67 | } 68 | CFRelease(items); 69 | 70 | return privateKeyRef; 71 | } 72 | #pragma mark - Encrypt 73 | - (NSString*) rsaEncryptString:(NSString*)string { 74 | NSData* data = [string dataUsingEncoding:NSUTF8StringEncoding]; 75 | NSData* encryptedData = [self rsaEncryptData: data]; 76 | NSString* base64EncryptedString = [encryptedData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; 77 | return base64EncryptedString; 78 | } 79 | 80 | // 加密的大小受限于SecKeyEncrypt函数,SecKeyEncrypt要求明文和密钥的长度一致,如果要加密更长的内容,需要把内容按密钥长度分成多份,然后多次调用SecKeyEncrypt来实现 81 | - (NSData*) rsaEncryptData:(NSData*)data { 82 | SecKeyRef key = [self getPublicKey]; 83 | size_t cipherBufferSize = SecKeyGetBlockSize(key); 84 | uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t)); 85 | size_t blockSize = cipherBufferSize - 11; // 分段加密 86 | size_t blockCount = (size_t)ceil([data length] / (double)blockSize); 87 | NSMutableData *encryptedData = [[NSMutableData alloc] init] ; 88 | for (int i=0; i