在RSA解密中将uint8_t转换为NSString

cod*_*ter 0 iphone objective-c ios ios5

我正在尝试实施RSA算法,我遵循苹果参考.但我有问题将uint8_t转换为NSData到NSString.

到目前为止我已经完成了这个.这个函数在参考中定义

-(void)test{
    [self generateKeyPairPlease];
    NSData *data = [self encryptWithPublicKey]; //All goes well until here
    [self decryptWithPrivateKey:data]; 

}
Run Code Online (Sandbox Code Playgroud)

对于加密我做了..

- (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;
}
Run Code Online (Sandbox Code Playgroud)

但在decryptWithPrivateKey我无法转换 uint8_t *plainBuffer(解密结果)到NSString.First我试图将其转换为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
Run Code Online (Sandbox Code Playgroud)

我想知道如何解密的结果转换uint8_t plainBufferNSDataNSString或干脆NSString让我得到我的字符串back.For我的加密和密钥生成代码,请参阅本参考.

提前致谢..

Cat*_*ivo 6

我知道这个问题已经过时并标记为已解决,但我遇到了类似的问题.我发现的是,苹果文档中的加密方法似乎存在错误

NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:dataLength];
Run Code Online (Sandbox Code Playgroud)

datalength是错误的变量.我用cipherbuffersize替换它

NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
Run Code Online (Sandbox Code Playgroud)

而且每件事现在都适合我.我希望它对我以外的其他人有用.