#import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonCryptor.h> //AES加密 - (NSData *)AES256EncryptWithKey:(NSString *)key{ char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [self bytes], dataLength, buffer, bufferSize, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); return nil; } //AES解密 - (NSData *)AES256DecryptWithKey:(NSString *)key{ char keyPtr[kCCKeySizeAES256+1]; bzero(keyPtr, sizeof(keyPtr)); [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [self bytes], dataLength, buffer, bufferSize, &numBytesDecrypted); if (cryptStatus == kCCSuccess) { return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); return nil; }
RSA:
#import <Foundation/Foundation.h> typedef void (^GenerateSuccessBlock)(void); @interface RSA : NSObject{ @private NSData * publicTag; NSData * privateTag; NSOperationQueue * cryptoQueue; GenerateSuccessBlock success; } @property (nonatomic,readonly) SecKeyRef publicKeyRef; @property (nonatomic,readonly) SecKeyRef privateKeyRef; @property (nonatomic,readonly) NSData *publicKeyBits; @property (nonatomic,readonly) NSData *privateKeyBits; @property (nonatomic,readonly) NSString *publicKeyString; + (id)shareInstance; - (void)generateKeyPairRSACompleteBlock:(GenerateSuccessBlock)_success; - (NSData *)RSA_EncryptUsingPublicKeyWithData:(NSData *)data; - (NSData *)RSA_EncryptUsingPrivateKeyWithData:(NSData*)data; - (NSData *)RSA_DecryptUsingPublicKeyWithData:(NSData *)data; - (NSData *)RSA_DecryptUsingPrivateKeyWithData:(NSData*)data; @end
#import "RSA.h" uint8_t *plainBuffer; uint8_t *cipherBuffer; uint8_t *decryptedBuffer; const size_t BUFFER_SIZE = 64; const size_t CIPHER_BUFFER_SIZE = 1024; const uint32_t PADDING = kSecPaddingPKCS1; const size_t kSecAttrKeySizeInBitsLength = 2024; static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey222\0"; static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey111\0"; #if DEBUG #define LOGGING_FACILITY(X, Y) \ NSAssert(X, Y); #define LOGGING_FACILITY1(X, Y, Z) \ NSAssert1(X, Y, Z); #else #define LOGGING_FACILITY(X, Y) \ if (!(X)) { \ NSLog(Y); \ } #define LOGGING_FACILITY1(X, Y, Z) \ if (!(X)) { \ NSLog(Y, Z); \ } #endif @interface RSA () - (void)deleteAsymmetricKeys; @end @implementation RSA @synthesize publicKeyRef,privateKeyRef; @synthesize publicKeyBits,privateKeyBits; #pragma mark - init - (id)init{ if (self = [super init]) { cryptoQueue = [[NSOperationQueue alloc] init]; // Tag data to search for keys. privateTag = [[NSData alloc] initWithBytes:privateKeyIdentifier length:sizeof(privateKeyIdentifier)]; publicTag = [[NSData alloc] initWithBytes:publicKeyIdentifier length:sizeof(publicKeyIdentifier)]; }return self; } + (id)shareInstance{ static RSA *_rsa = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _rsa = [[self alloc] init]; }); return _rsa; } #pragma mark - getter - (SecKeyRef)getPublicKeyRef { OSStatus resultCode = noErr; SecKeyRef publicKeyReference = NULL; if(publicKeyRef == NULL) { NSMutableDictionary * queryPublicKey = [NSMutableDictionary dictionaryWithCapacity:0]; // Set the public key query dictionary. [queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass]; [queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag]; [queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef]; // Get the key. resultCode = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference); //NSLog(@"getPublicKey: result code: %ld", resultCode); if(resultCode != noErr) { publicKeyReference = NULL; } queryPublicKey =nil; } else { //NSLog(@"no use SecItemCopyMatching\n"); publicKeyReference = publicKeyRef; } return publicKeyReference; } - (SecKeyRef)getPrivateKeyRef { OSStatus resultCode = noErr; SecKeyRef privateKeyReference = NULL; if(privateKeyRef == NULL) { NSMutableDictionary * queryPrivateKey = [[NSMutableDictionary alloc] init]; // Set the private key query dictionary. [queryPrivateKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass]; [queryPrivateKey setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag]; [queryPrivateKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; [queryPrivateKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef]; // Get the key. resultCode = SecItemCopyMatching((__bridge CFDictionaryRef)queryPrivateKey, (CFTypeRef *)&privateKeyReference); //NSLog(@"getPrivateKey: result code: %ld", resultCode); if(resultCode != noErr) { privateKeyReference = NULL; } queryPrivateKey = nil; } else { //NSLog(@"no use SecItemCopyMatching\n"); privateKeyReference = privateKeyRef; } return privateKeyReference; } - (NSData *)publicKeyBits { OSStatus sanityCheck = noErr; CFTypeRef _publicKeyBitsReference = NULL; NSMutableDictionary * queryPublicKey = [NSMutableDictionary dictionaryWithCapacity:0]; // Set the public key query dictionary. [queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass]; [queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag]; [queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnData]; // Get the key bits. sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&_publicKeyBitsReference); if (sanityCheck != noErr) { _publicKeyBitsReference = NULL; } return (__bridge NSData*)_publicKeyBitsReference; } - (NSData *)privateKeyBits { OSStatus sanityCheck = noErr; CFTypeRef _privateKeyBitsReference = NULL; NSMutableDictionary * queryPublicKey = [NSMutableDictionary dictionaryWithCapacity:0]; // Set the public key query dictionary. [queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass]; [queryPublicKey setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag]; [queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnData]; // Get the key bits. sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&_privateKeyBitsReference); if (sanityCheck != noErr) { _privateKeyBitsReference = NULL; } return (__bridge NSData*)_privateKeyBitsReference; } - (NSString *)publicKeyString { return [[self publicKeyBits] base64Encoding]; } #pragma mark - generate rsa key pair - (void)generateKeyPairRSACompleteBlock:(GenerateSuccessBlock)_success { NSInvocationOperation * genOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(generateKeyPairOperation) object:nil]; [cryptoQueue addOperation:genOp]; success = _success; } - (void)generateKeyPairOperation{ @autoreleasepool { // Generate the asymmetric key (public and private) [self generateKeyPairRSA]; [self performSelectorOnMainThread:@selector(generateKeyPairCompleted) withObject:nil waitUntilDone:NO]; } } - (void)generateKeyPairCompleted{ if (success) { success(); } } - (void)generateKeyPairRSA { OSStatus sanityCheck = noErr; publicKeyRef = NULL; privateKeyRef = NULL; // First delete current keys. [self deleteAsymmetricKeys]; // Container dictionaries. NSMutableDictionary * privateKeyAttr = [NSMutableDictionary dictionaryWithCapacity:0]; NSMutableDictionary * publicKeyAttr = [NSMutableDictionary dictionaryWithCapacity:0]; NSMutableDictionary * keyPairAttr = [NSMutableDictionary dictionaryWithCapacity:0]; // Set top level dictionary for the keypair. [keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; [keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:kSecAttrKeySizeInBitsLength] forKey:(__bridge id)kSecAttrKeySizeInBits]; // Set the private key dictionary. [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent]; [privateKeyAttr setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag]; // See SecKey.h to set other flag values. // Set the public key dictionary. [publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent]; [publicKeyAttr setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag]; // See SecKey.h to set other flag values. // Set attributes to top level dictionary. [keyPairAttr setObject:privateKeyAttr forKey:(__bridge id)kSecPrivateKeyAttrs]; [keyPairAttr setObject:publicKeyAttr forKey:(__bridge id)kSecPublicKeyAttrs]; // SecKeyGeneratePair returns the SecKeyRefs just for educational purposes. sanityCheck = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &publicKeyRef, &privateKeyRef); LOGGING_FACILITY( sanityCheck == noErr && publicKeyRef != NULL && privateKeyRef != NULL, @"Something really bad went wrong with generating the key pair." ); } - (void)deleteAsymmetricKeys { OSStatus sanityCheck = noErr; NSMutableDictionary * queryPublicKey = [NSMutableDictionary dictionaryWithCapacity:0]; NSMutableDictionary * queryPrivateKey = [NSMutableDictionary dictionaryWithCapacity:0]; // Set the public key query dictionary. [queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass]; [queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag]; [queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; // Set the private key query dictionary. [queryPrivateKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass]; [queryPrivateKey setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag]; [queryPrivateKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; // Delete the private key. sanityCheck = SecItemDelete((__bridge CFDictionaryRef)queryPrivateKey); LOGGING_FACILITY1( sanityCheck == noErr || sanityCheck == errSecItemNotFound, @"Error removing private key, OSStatus == %ld.", sanityCheck ); // Delete the public key. sanityCheck = SecItemDelete((__bridge CFDictionaryRef)queryPublicKey); LOGGING_FACILITY1( sanityCheck == noErr || sanityCheck == errSecItemNotFound, @"Error removing public key, OSStatus == %ld.", sanityCheck ); if (publicKeyRef) CFRelease(publicKeyRef); if (privateKeyRef) CFRelease(privateKeyRef); } #pragma mark - encrypt/decrypt - (NSData*)rsaEncryptWithData:(NSData*)data usingPublicKey:(BOOL)yes{ SecKeyRef key = yes?self.publicKeyRef:self.privateKeyRef; size_t cipherBufferSize = SecKeyGetBlockSize(key); uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t)); memset((void *)cipherBuffer, 0*0, cipherBufferSize); NSData *plainTextBytes = data; size_t blockSize = cipherBufferSize - 11; size_t blockCount = (size_t)ceil([plainTextBytes length] / (double)blockSize); NSMutableData *encryptedData = [NSMutableData dataWithCapacity:0]; for (int i=0; i<blockCount; i++) { int bufferSize = MIN(blockSize,[plainTextBytes length] - i * blockSize); NSData *buffer = [plainTextBytes subdataWithRange:NSMakeRange(i * blockSize, bufferSize)]; OSStatus status = SecKeyEncrypt(key, kSecPaddingPKCS1, (const uint8_t *)[buffer bytes], [buffer length], cipherBuffer, &cipherBufferSize); if (status == noErr){ NSData *encryptedBytes = [NSData dataWithBytes:(const void *)cipherBuffer length:cipherBufferSize]; [encryptedData appendData:encryptedBytes]; }else{ if (cipherBuffer) { free(cipherBuffer); } return nil; } } if (cipherBuffer) free(cipherBuffer); // NSLog(@"Encrypted text (%d bytes): %@", [encryptedData length], [encryptedData description]); // NSLog(@"Encrypted text base64: %@", [Base64 encode:encryptedData]); return encryptedData; } - (NSData*)rsaDecryptWithData:(NSData*)data usingPublicKey:(BOOL)yes{ NSData *wrappedSymmetricKey = data; SecKeyRef key = yes?self.publicKeyRef:self.privateKeyRef; size_t cipherBufferSize = SecKeyGetBlockSize(key); size_t keyBufferSize = [wrappedSymmetricKey length]; NSMutableData *bits = [NSMutableData dataWithLength:keyBufferSize]; OSStatus sanityCheck = SecKeyDecrypt(key, kSecPaddingPKCS1, (const uint8_t *) [wrappedSymmetricKey bytes], cipherBufferSize, [bits mutableBytes], &keyBufferSize); NSAssert(sanityCheck == noErr, @"Error decrypting, OSStatus == %ld.", sanityCheck); [bits setLength:keyBufferSize]; return bits; } - (NSData *) RSA_EncryptUsingPublicKeyWithData:(NSData *)data{ return [self rsaEncryptWithData:data usingPublicKey:YES]; } - (NSData *) RSA_EncryptUsingPrivateKeyWithData:(NSData*)data{ return [self rsaEncryptWithData:data usingPublicKey:NO]; } - (NSData *) RSA_DecryptUsingPublicKeyWithData:(NSData *)data{ return [self rsaDecryptWithData:data usingPublicKey:YES]; } - (NSData *) RSA_DecryptUsingPrivateKeyWithData:(NSData*)data{ return [self rsaDecryptWithData:data usingPublicKey:NO]; } @end
转载于:https://www.cnblogs.com/tracy-e/archive/2010/11/15/1877351.html
相关资源:数据结构—成绩单生成器