iOS 原生RSA加解密 及生成密鑰對

iOS 不僅提供了RSA加解密,簽名驗(yàn)簽的功能,同時(shí)還提供了生成密鑰對的方法。

SecKeyEncrypt (解密)
SecKeyDecrypt(解密)
SecKeyGeneratePair(生成密鑰對)

關(guān)于分段加密
因?yàn)镽SA是需要分段加密的,每一段的長度不能大于密鑰的長度SecKeyGetBlockSize(keyRef),一般為128(1024bit的密鑰)字節(jié)。
但是由于RSA加密會(huì)設(shè)置填充模式,常用的模式為RSA_PKCS1_PADDING,在這種模式下,每次加密的明文長度需要再減少11個(gè)字節(jié),所以在分段的時(shí)候每段的長度為 128 - 11 = 117 ;

size_t src_block_size = block_size - 11;

iOS 原生生成RSA密鑰對的類型為SecKeyRef類型,也可以將 SecKeyRef 類型轉(zhuǎn)換為NSData。這里生成的數(shù)據(jù)只包含了公鑰的信息,不是標(biāo)準(zhǔn)的PEM格式的文件,如果需要PEM格式需要添加PEM頭信息

- (NSData *)getKeyBitsWithKeyIdentifier:(NSString *)keyIdentifier {

    if (!keyIdentifier) {
        return nil;
    }
    NSData *keyBits = nil;
    OSStatus sanityCheck = noErr;
    
    NSData * peerTag = [keyIdentifier dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary * queryAttributes = [[NSMutableDictionary alloc] init];
    [queryAttributes setObject:(id)kSecClassKey forKey:(id)kSecClass];
    [queryAttributes setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
    [queryAttributes setObject:peerTag forKey:(id)kSecAttrApplicationTag];
    [queryAttributes setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];
    
    CFTypeRef result = NULL;
    sanityCheck = SecItemCopyMatching((CFDictionaryRef) queryAttributes, &result);
    if (sanityCheck == noErr || sanityCheck == errSecDuplicateItem) {
        keyBits = CFBridgingRelease(result);
        return keyBits;
    }
    return nil;
}

在設(shè)置queryAttributes 的參數(shù)中,下面這行

[queryAttributes setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];

key有三種類型可選

kSecReturnData
kSecReturnRef
kSecReturnPersistentRef

如果選kSecReturnPersistentRef類型,可用下面的方法轉(zhuǎn)換

- (SecKeyRef)getKeyRefWithPersistentKeyRef:(CFTypeRef)persistentRef {

    OSStatus sanityCheck = noErr;
    SecKeyRef keyRef = NULL;
    if (persistentRef == NULL) {
        //@"persistentRef object cannot be NULL."
        return nil;
    }
    
    NSMutableDictionary * queryKey = [[NSMutableDictionary alloc] init];
    
    // Set the SecKeyRef query dictionary.
    [queryKey setObject:(__bridge id)persistentRef forKey:(id)kSecValuePersistentRef];
    [queryKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];
    
    // Get the persistent key reference.
    sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryKey, (CFTypeRef *)&keyRef);
    
    return keyRef;
}

demo一份
官方代碼地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 嘟噥嘟噥:最近接到一個(gè)任務(wù):在客戶端動(dòng)態(tài)生成RSA密鑰對,然后向服務(wù)器發(fā)送這個(gè)密鑰對中的公鑰字符串,由服務(wù)器進(jìn)行公...
    TimmyR閱讀 8,354評(píng)論 19 21
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,596評(píng)論 19 139
  • 本文主要介紹移動(dòng)端的加解密算法的分類、其優(yōu)缺點(diǎn)特性及應(yīng)用,幫助讀者由淺入深地了解和選擇加解密算法。文中會(huì)包含算法的...
    蘋果粉閱讀 11,679評(píng)論 5 29
  • 海洋臨河里門市切位信息(經(jīng)理帶隊(duì)) 2月25日 騰沖瑞麗芒市第六套 雙飛5日 (19+1精品小團(tuán))原件3560元/...
    2173a787ca3b閱讀 384評(píng)論 0 0
  • 2017年4月27日 1.感恩爸媽幫助照顧孩子。 2.感恩兒子讓我享受到幸福。 3.感恩先生為家努力付出。 4.感...
    馮梓源閱讀 196評(píng)論 0 0

友情鏈接更多精彩內(nèi)容