我试图实现RSA算法,并且我跟着苹果reference。 但我遇到问题将uint8_t转换为NSData到NSString。将uint8_t转换为RSA解密中的NSString
我已经做了在reference
-(void)test{
[self generateKeyPairPlease];
NSData *data = [self encryptWithPublicKey]; //All goes well until here
[self decryptWithPrivateKey:data];
}
定义加密我这样做使far..These功能..
- (NSData *)encryptWithPublicKey
{
OSStatus status = noErr;
size_t cipherBufferSize;
uint8_t *cipherBuffer; // 1
// [cipherBufferSize]
const uint8_t dataToEncrypt[] = "the quick brown fox jumps "
"over the lazy dog\0"; // 2
size_t dataLength = sizeof(dataToEncrypt)/sizeof(dataToEncrypt[0]);
SecKeyRef publicKey = NULL; // 3
NSData * publicTag = [NSData dataWithBytes:publicKeyIdentifier
length:strlen((const char *)publicKeyIdentifier)]; // 4
NSMutableDictionary *queryPublicKey =
[[NSMutableDictionary alloc] init]; // 5
[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];
// 6
status = SecItemCopyMatching
((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKey); // 7
// Allocate a buffer
cipherBufferSize = SecKeyGetBlockSize(publicKey);
cipherBuffer = malloc(cipherBufferSize);
// Error handling
if (cipherBufferSize < sizeof(dataToEncrypt)) {
// Ordinarily, you would split the data up into blocks
// equal to cipherBufferSize, with the last block being
// shorter. For simplicity, this example assumes that
// the data is short enough to fit.
printf("Could not decrypt. Packet too large.\n");
return NULL;
}
// Encrypt using the public.
status = SecKeyEncrypt( publicKey,
kSecPaddingPKCS1,
dataToEncrypt,
(size_t) dataLength,
cipherBuffer,
&cipherBufferSize
); // 8
// Error handling
// Store or transmit the encrypted text
if (publicKey) CFRelease(publicKey);
NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:dataLength];
free(cipherBuffer);
return encryptedData;
}
但decryptWithPrivateKey
我能不能转换uint8_t *plainBuffer
(解密结果)到NSString
。首先我试图把它转换成NSData
打印时用NSLog
它正确显示字节,但是NSData
没有转换成字符串。
- (void)decryptWithPrivateKey: (NSData *)dataToDecrypt
{
OSStatus status = noErr;
size_t cipherBufferSize = [dataToDecrypt length];
uint8_t *cipherBuffer = (uint8_t *)[dataToDecrypt bytes];
size_t plainBufferSize;
uint8_t *plainBuffer;
SecKeyRef privateKey = NULL;
NSData * privateTag = [NSData dataWithBytes:privateKeyIdentifier
length:strlen((const char *)privateKeyIdentifier)];
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];
// 1
status = SecItemCopyMatching
((__bridge CFDictionaryRef)queryPrivateKey, (CFTypeRef *)&privateKey); // 2
// Allocate the buffer
plainBufferSize = SecKeyGetBlockSize(privateKey);
plainBuffer = malloc(plainBufferSize);
if (plainBufferSize < cipherBufferSize) {
// Ordinarily, you would split the data up into blocks
// equal to plainBufferSize, with the last block being
// shorter. For simplicity, this example assumes that
// the data is short enough to fit.
printf("Could not decrypt. Packet too large.\n");
return;
}
// Error handling
status = SecKeyDecrypt( privateKey,
kSecPaddingPKCS1,
cipherBuffer,
cipherBufferSize,
plainBuffer,
&plainBufferSize
); // 3
//*******************************************************************************
// Not able to convert uint8_t *plainBuffer to string
// I also tried to convert it into NSData and then try to convert it into NSString but can't
//What Should i do here to get string back
if(privateKey) CFRelease(privateKey);
}
@end
我想知道如何解密结果uint8_t plainBuffer
转换为NSData
到NSString
或者干脆NSString
,使我得到我的字符串back.For我的加密和密钥生成代码,请参阅本reference。
在此先感谢..
任何人都可以解释为什么这个问题投下来吗?它有什么不对 – codester
我不知道谁低估了它。但我可以告诉你:你的问题没有足够的信息来弄清楚你的问题。我和其他用户都试图从你那里获得更多的信息,但你曾经给出的唯一答案是“我试过了,但它不起作用” - 没有解释你尝试了什么“它”,你是如何尝试的,它以什么方式失败等等。所以,就目前来看,你的问题是不可能调试的。但是如果你只是给我们提供了信息,那么潜在的问题将很容易调试。正如所写的,这意味着一个糟糕的问题。 – abarnert
我非常抱歉。现在我没有转储,但我解决了我的问题。实际上这个例子是直接从苹果refrence和有一些问题。我跟着这个github [代码](https ://github.com/kuapay/iOS-Certificate--Key-and-Trust-Sample-Project)。再次感谢您的回复。 – codester