使用rsa私钥签名数据

use*_*777 5 encryption rsa objective-c ios

我知道是圣诞节,但我有一个我需要解决的巨大问题,我在这里寻找我的圣诞奇迹......

我已经阅读了apple文档,并且只有如何从证书创建RSA公钥和私钥的指南.就我而言,我在.pem文件中只有RSA私钥.所以我的问题是他:我应该如何使用该密钥签署数据?我不想使用openssl.我试过没有运气,我认为可以通过使用苹果API来签署RSA数据.

这就是我的密钥的样子:

-----BEGIN RSA PRIVATE KEY-----
..............................
-----END RSA PRIVATE KEY-----
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所做的:

-(NSString *)signing:(NSString *)dataString {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"PrestaMobilekey" ofType:@"pem"];
    NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];

    SecKeyRef privateKey = (__bridge SecKeyRef)(data);

    uint8_t *signedHashBytes = NULL;
    // calculate private key size
    size_t signedHashBytesSize = SecKeyGetBlockSize(privateKey);

    // create space to put signature
    signedHashBytes = (uint8_t *)malloc(signedHashBytesSize * sizeof(uint8_t));
    memset((void *)signedHashBytes, 0x0, signedHashBytesSize);

    OSStatus status = NULL;

    // sign data
    status = SecKeyRawSign(privateKey,
                           kSecPaddingPKCS1SHA1,
                           [[[dataString dataUsingEncoding:NSUTF8StringEncoding] SHA1] bytes],
                           CC_SHA1_DIGEST_LENGTH,
                           signedHashBytes,
                           &signedHashBytesSize);

    if (privateKey) {
        CFRelease(privateKey);
    }

    // get signature hash
    NSData *signedHash = [NSData dataWithBytes:(const void *)signedHashBytes length:(NSUInteger)signedHashBytesSize];

    // release created space
    if (signedHashBytes) {
        free(signedHashBytes);
    }

    if (status != errSecSuccess) {
        return @"";
    }

    // return Base64 encoded signature string
    return [Base64 encode:signedHash];
}
Run Code Online (Sandbox Code Playgroud)

我真的希望有人会帮助我,提供一些好的信息和答案.

谢谢.

use*_*777 1

好的,我自己找到了解决这个问题的方法。我希望这对其他人有帮助...对我有帮助。我以为我可以在没有 openssl 的情况下做到这一点,但我错了。但是,通过像那篇文章中那样做,您的项目中将不需要额外的库。使用终端