带有OAEP填充sha256的ios 10之前的目标c RSA [英] Objective c RSA with OAEP padding sha256 prior ios 10

查看:87
本文介绍了带有OAEP填充sha256的ios 10之前的目标c RSA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RSA加密方法在iPhone中使用一种加密方法,到目前为止,我可以使用此方法获得加密字符串,该字符串已由服务器成功解密.

I am working on an encryption method in the iPhone with the RSA encryption method, so far i could achieve getting the encryption string with this method, the string is successfully decrypted by the server.

SecKeyRef keyRef = [self addPublicKey:pubKey];

SecKeyAlgorithm algorithm = kSecKeyAlgorithmRSAEncryptionOAEPSHA256;

if (!keyRef) {
    return nil;
}

BOOL canEncrypt =  SecKeyIsAlgorithmSupported(keyRef, kSecKeyOperationTypeEncrypt, algorithm);

if (canEncrypt) {
    CFErrorRef error = NULL;
    NSData *encryptedData = (NSData *)CFBridgingRelease(
                                                        SecKeyCreateEncryptedData(keyRef, algorithm, (__bridge CFDataRef) content, &error)
    );

    if (encryptedData) {
        return encryptedData;
    }else{
        NSError *err = CFBridgingRelease(error);
        NSLog(@"Ocurrió un error %@", err.localizedDescription);
        return nil;
    }
}

此方法适用于ios 10及更高版本,我需要知道如何在以前的ios版本中设置算法,我的代码如下

This method works for ios 10 and newer, what i need is to know how to set the algorithm in prior ios versions, my code is the following

SecKeyRef keyRef = [self addPublicKey:pubKey];
if (!keyRef) {
    return nil;
}

size_t cipherBufferSize = SecKeyGetBlockSize(keyRef);
uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
memset((void *)cipherBuffer, 0*0, cipherBufferSize);

NSData *plainTextBytes = content;
size_t blockSize = cipherBufferSize - 11;
size_t blockCount = (size_t)ceil([plainTextBytes length] / (double)blockSize);

NSMutableData *encryptedData = [NSMutableData dataWithCapacity:0];

for (int i=0; i<blockCount; i++) {

    int bufferSize = (int)MIN(blockSize,[plainTextBytes length] - i * blockSize);
    NSData *buffer = [plainTextBytes subdataWithRange:NSMakeRange(i * blockSize, bufferSize)];
    OSStatus status = SecKeyEncrypt(keyRef,
                                    kSecPaddingOAEP,
                                    (const uint8_t *)[buffer bytes],
                                    [buffer length],
                                    cipherBuffer,
                                    &cipherBufferSize);

    if (status == noErr){
        NSData *encryptedBytes = [NSData dataWithBytes:(const void *)cipherBuffer length:cipherBufferSize];
        [encryptedData appendData:encryptedBytes];

    }else{

        if (cipherBuffer) {
            free(cipherBuffer);
        }
        return nil;
    }
}
if (cipherBuffer) free(cipherBuffer);

到目前为止,我可以看到在ios 10版本中您可以使用此行设置算法

So far i can see that in the version of ios 10 you can set the algorithm with this line

SecKeyAlgorithm algorithm = kSecKeyAlgorithmRSAEncryptionOAEPSHA256;

我的问题是,如何在ios的早期版本中获得该算法,我发布的第二个代码无法解密.

my question is, how do i get that algorithm in the early version of ios, the second code i post can't be decrypted.

感谢您的帮助

推荐答案

如果您将OAEP填充与 SecKeyEncrypt 一起使用,则只能使用 kSecPaddingOAEP ,即SHA1.不幸的是,您不能将OAEP SHA256与 SecKeyEncrypt 一起使用.

If you are using OAEP padding with SecKeyEncrypt, you can only use kSecPaddingOAEP, which is SHA1. Unfortunately you cannot use OAEP SHA256 with SecKeyEncrypt.

这篇关于带有OAEP填充sha256的ios 10之前的目标c RSA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆