RSA 和 Objective c

发布于 2024-11-18 19:20:01 字数 2684 浏览 0 评论 0原文

在这个例子中(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;
}

In this exemple where is (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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-11-25 19:20:01

n 不在示例中,因为您仅在此处进行加密,并且这是使用公钥而不是私钥完成的。

mplainBuffer

e 位于使用 [self getPublicKeyRef] 检索的 publicKey 中。

n is not in the example, since you only encrypt here, and this is done with the public key, not the private key.

m is plainBuffer.

e is in publicKey which is retrieved with [self getPublicKeyRef].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文