gpt4 book ai didi

ios - 使用现有公钥的 RSA 加密

转载 作者:行者123 更新时间:2023-11-29 10:46:24 25 4
gpt4 key购买 nike

我正在尝试使用以下方法加密 NSData:

- (NSData *) encryptWithData:(NSData *)content {

size_t plainLen = [content length];

void *plain = malloc(plainLen);
[content getBytes:plain
length:plainLen];

size_t cipherLen = 256;
void *cipher = malloc(cipherLen);

OSStatus returnCode = SecKeyEncrypt("PUBLIC KEY HERE", kSecPaddingPKCS1, plain,
plainLen, cipher, &cipherLen);

NSData *result = nil;
if (returnCode != 0) {
NSLog(@"SecKeyEncrypt fail. Error Code: %ld", returnCode);
}
else {
result = [NSData dataWithBytes:cipher
length:cipherLen];
}

free(plain);
free(cipher);

return result;

写在“这里是公钥”的地方我想加载一个我已经复制到我的包中的现有公钥。我该怎么做?

最佳答案

例如使用包含公钥的证书文件:

NSData *certificateData = [NSData dataWithContentsOfURL:certificateURL options:0 error:&error];
if (certificateData) {
SecCertificateRef certificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)(certificateData));
// ...
SecKeyRef publicKey;
SecCertificateCopyPublicKey(certificate, &publicKey);
// ...
}

从包中加载数据:

NSArray *certificateURLs = [[NSBundle mainBundle] URLsForResourcesWithExtension:@"cer" subdirectory:@"myCertificates"];
for (NSURL *certificateURL in certificateURLs) {
NSData *certificateData = [NSData dataWithContentsOfURL:certificateURL options:0 error:&error];
// ...
}

关于ios - 使用现有公钥的 RSA 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22218508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com