我已经编译并构建了openssl到我的iOS项目,
但在objective-c中苦苦挣扎,在此命令行中编写了一个等效的代码:
openssl rsautl -encrypt -inkey publicKey.pem -pubin -in textfile.txt -out encrypted.bin
我怎样才能做到这一点?
答案 0 :(得分: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];
}
我希望它对我有所帮助我需要一段时间才能找到正确的代码
答案 1 :(得分:2)
所以我解决了我的问题,这是我的问题加密NSString的函数:
我修改了此问题中代码的代码:send 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;