【问题标题】:Objective c RSA with OAEP padding sha256 prior ios 10Objective c RSA 与 OAEP 填充 sha256 之前的 ios 10
【发布时间】:2018-09-29 00:06:13
【问题描述】:

我正在使用RSA加密方法在iPhone中使用加密方法,到目前为止我可以使用该方法实现获取加密字符串,该字符串已被服务器成功解密。

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 版本中设置算法,我的代码如下

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 的版本中,您可以使用此行设置算法

SecKeyAlgorithm algorithm = kSecKeyAlgorithmRSAEncryptionOAEPSHA256;

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

感谢您的帮助

【问题讨论】:

    标签: ios encryption rsa sha256


    【解决方案1】:

    如果您使用带有SecKeyEncrypt 的OAEP 填充,则只能使用kSecPaddingOAEP,即SHA1。很遗憾,您不能将 OAEP SHA256 与 SecKeyEncrypt 一起使用。

    【讨论】:

    • 在 iOS 8 及更高版本上执行此操作的可能方法是什么?
    猜你喜欢
    • 1970-01-01
    • 2013-06-11
    • 2018-04-05
    • 2023-03-18
    • 2021-08-27
    • 2015-04-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    相关资源
    最近更新 更多