RSA и Цель c - PullRequest
       28

RSA и Цель c

0 голосов
/ 06 июля 2011

В этом примере где (e,n,M)?

 const size_t BUFFER_SIZE = 64;
    const size_t CIPHER_BUFFER_SIZE = 1024;
    const uint32_t PADDING = kSecPaddingNone;
    static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey\0";
    static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey\0";

    SecKeyRef publicKey;
    SecKeyRef privateKey; 

        - (void)encryptWithPublicKey:(uint8_t *)plainBuffer cipherBuffer:(uint8_t *)cipherBuffer
        {
            NSLog(@"== encryptWithPublicKey()");

            OSStatus status = noErr;

            NSLog(@"** original plain text 0: %s", plainBuffer);

            size_t plainBufferSize = strlen((char *)plainBuffer);
            size_t cipherBufferSize = CIPHER_BUFFER_SIZE;
            SecKeyRef key=[self getPublicKeyRef];

            NSLog(@"SecKeyGetBlockSize() public = %d", SecKeyGetBlockSize(key));
            //  Error handling
            // Encrypt using the public.
            status = SecKeyEncrypt([self getPublicKeyRef],
                                   PADDING,
                                   plainBuffer,
                                   plainBufferSize,
                                   &cipherBuffer[0],
                                   &cipherBufferSize
                                   );
            NSLog(@"encryption result code: %d (size: %d)", status, cipherBufferSize);
            NSLog(@"encrypted text: %s", cipherBuffer);
        }


- (SecKeyRef)getPublicKeyRef {
    OSStatus resultCode = noErr;
    SecKeyRef publicKeyReference = NULL;

    if(publicKey == NULL) {
        NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];

        NSData *publicTag = [NSData dataWithBytes:publicKeyIdentifier

                                           length:strlen((const char *)publicKeyIdentifier)]; 

        // Set the public key query dictionary.
        [queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass];

        [queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag];

        [queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];

        [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef];

        // Get the key.
        resultCode = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);
        NSLog(@"getPublicKey: result code: %d", resultCode);

        if(resultCode != noErr)
        {
            publicKeyReference = NULL;
        }

        [queryPublicKey release];
    } else {
        publicKeyReference = publicKey;
    }

    return publicKeyReference;
}

1 Ответ

2 голосов
/ 06 июля 2011

n не в этом примере, поскольку вы шифруете только здесь, и это делается с помощью открытого ключа, а не личного ключа.

м - это plainBuffer.

e находится в publicKey, который извлекается с помощью [self getPublicKeyRef].

...