我的代码:
SecKeyRef oPublicKey = [self getPublicKeyRef];
SecKeyRef oPrivateKey = [self getPrivateKeyRef];
CFDictionaryRef myDictionary;
CFTypeRef keys[2];
CFTypeRef values[2];
// Initialize dictionary of key params
keys[0] = kSecAttrKeyType;
values[0] = kSecAttrKeyTypeRSA;
keys[1] = kSecAttrKeySizeInBits;
int iByteSize = 1024;
values[1] = CFNumberCreate( NULL, kCFNumberIntType, &iByteSize );
myDictionary = CFDictionaryCreate( NULL, keys, values, sizeof(keys) / sizeof(keys[0]), NULL, NULL );
// Generate keys
OSStatus status = SecKeyGeneratePair( myDictionary, &oPublicKey, &oPrivateKey );
if ( status != 0 )
NSLog( @"SecKeyGeneratePair failed" );
// Encrypt some data
uint8_t* pPlainText = (uint8_t*)"6921";
uint8_t aCipherText[1024];
size_t iCipherLength = 1024;
status = SecKeyEncrypt( oPublicKey, kSecPaddingPKCS1, pPlainText, strlen( (char*)pPlainText ) + 1, &aCipherText[0], &iCipherLength );
if ( status != 0 )
NSLog( @"SecKeyEncrypt failed" );
NSMutableData *data12=[[NSMutableData alloc] init];
[data12 appendBytes:aCipherText length:strlen( (char*)aCipherText ) + 1];
NSString *string1 = [[NSString alloc]initWithData:data12 encoding:NSASCIIStringEncoding];
NSLog(@"Encrypted Text:::%@",string1);
// Decrypt the data
uint8_t aPlainText[1024];
size_t iPlainLength = 1024;
status = SecKeyDecrypt( oPrivateKey, kSecPaddingPKCS1, &aCipherText[0], iCipherLength, &aPlainText[0], &iPlainLength );
if ( status != 0 )
NSLog( @"SecKeyDecrypt failed" );
NSLog(@"FINAL decrypted text: %s", aPlainText);
我正在使用此代码进行加密,但我们得到的输出如下:
N$) : )¥= wÞ¢#4+ 'MO, œÝÕ' #2< xÕ ™'Hþ a  f¦ šU;f£Æì ø¤VØ´ÿ ÷ påcx ¨¢¯Ô)Õ#k Ë« : çÑ5 õ°7\Û ‰z~“9GX>M¼êe뿆w¥ro¬ ›gP4s2µ)9; p
但是如何获取 NSString 格式?我的代码中是否有任何错误步骤?