RSA 和 Objective c
在这个例子中(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
n 不在示例中,因为您仅在此处进行加密,并且这是使用公钥而不是私钥完成的。
m 是
plainBuffer
。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]
.