3

我已经为我的 iOS 项目编译并构建了 openssl,

但是在objective-c中苦苦挣扎,为此命令行编写了等效代码:

openssl rsautl -encrypt -inkey publicKey.pem -pubin -in textfile.txt -out encrypted.bin

我怎样才能做到这一点?

4

2 回答 2

5

嗨,我遇到了同样的问题,最后我找到了我正在寻找的东西。我需要像 CodeInChaos 所说的是我的自签名证书。有了它,我的代码可以正常工作。为此,我使用以下命令:

openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650

我发现这篇文章非常有用:

http://blog.iamzsx.me/show.html?id=155002

它回答了很多问题。不是英文的,但谷歌翻译得很好,所以这不是一个大问题。

我做了这个小功能,用我找到的代码和我自己的代码来加密数据。我的包中有我的公钥,我以 base64 编码的 NSDaa 返回消息以将其发送到服务器:

+ (NSString *)encryptWithPublicKeyMessage:(NSString *) message
{
NSLog(@"encrypting...");
NSData *inputData = [message dataUsingEncoding:NSUTF8StringEncoding];
const void *bytes = [inputData bytes];
int length = [inputData length];
uint8_t *plainText = malloc(length);
memcpy(plainText, bytes, length);

/* Open and parse the cert*/
NSData *certData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"der"]];
SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)certData);
SecPolicyRef policy = SecPolicyCreateBasicX509();
SecTrustRef trust;
OSStatus status = SecTrustCreateWithCertificates(cert, policy, &trust);

/* You can ignore the SecTrustResultType, but you have to run SecTrustEvaluate
 * before you can get the public key */
SecTrustResultType trustResult;
if (status == noErr) {
    status = SecTrustEvaluate(trust, &trustResult);
}

/* Now grab the public key from the cert */
SecKeyRef publicKey = SecTrustCopyPublicKey(trust);

/* allocate a buffer to hold the cipher text */
size_t cipherBufferSize;
uint8_t *cipherBuffer; 
cipherBufferSize = SecKeyGetBlockSize(publicKey);
cipherBuffer = malloc(cipherBufferSize);

/* encrypt!! */
SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plainText, length, cipherBuffer, &cipherBufferSize);


 NSData *d = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];

/* Free the Security Framework Five! */
CFRelease(cert);
CFRelease(policy);
CFRelease(trust);
CFRelease(publicKey);
free(cipherBuffer);
NSLog(@"encrypted");
return [d encodeBase64ForData];
}

我希望它对我有帮助 我需要一段时间才能找到正确的代码

于 2012-04-09T15:31:08.703 回答
2

所以我解决了我的问题,这是我的问题的函数,它加密了一个 NSString:

我从这个问题的代码中修改了代码:将 RSA public key to iphone and use it to encrypt

(同样在我的代码末尾,我使用QSUtilities将消息编码为 base64)

#pragma mark Encryption using OpenSSL
+ (NSString *)EncryptMessage:(NSString *)message {
NSString *path = [[NSBundle mainBundle] pathForResource:@"pubkey" ofType:@"pem"];
FILE *pubkey = fopen([path cStringUsingEncoding:1], "r");
if (pubkey == NULL) {
    NSLog(@"duh: %@", [path stringByAppendingString:@" not found"]);
    return NULL;
}

RSA *rsa = PEM_read_RSA_PUBKEY(pubkey, NULL, NULL, NULL);
if (rsa == NULL) {
    NSLog(@"Error reading RSA public key.");
    return NULL;
}

const char *msgInChar = [message UTF8String];
unsigned char *encrypted = (unsigned char *) malloc(512); //I'm not so sure about this size
int bufferSize = RSA_public_encrypt(strlen(msgInChar), (unsigned char *)msgInChar, encrypted, rsa, RSA_PKCS1_PADDING);
if (bufferSize == -1) {
    NSLog(@"Encryption failed");
    return NULL;
}

NSData *data = [NSData dataWithBytes:(const void *)encrypted length:512]; //I'm not so sure about this length
NSString *result = [QSStrings encodeBase64WithData:data];

free(rsa);
fclose(pubkey);
free(encrypted);

return result;
于 2012-04-18T06:20:49.387 回答