2

我有两种 RSA 加密场景:

  1. 由 c# 应用程序生成的密钥对。发送到 iOS 的公钥。iOS 应用程序使用密钥加密数据并将其发送回。c# 应用程序使用私钥解密。--- 完全实施和工作。

  2. ios 应用程序生成的密钥对。发送到 c# 应用程序的公钥。c# 应用程序对数据进行加密,base 64 对其进行编码并将其发送到 ios 应用程序。然后 ios 应用程序使用私钥解密数据。--- 不工作。

如果我使用在 ios 上生成的公钥和私钥对进行加密,则一切正常,但是一旦我用 c# 中的 base 64 编码字符串替换它,它就会失败。我已经尝试了所有填充组合。

代码如下:

    SecPadding padding = kSecPaddingPKCS1;
NSMutableArray *keys = [self generateRSAKeyWithKeySizeInBits:1024 publicKeyTag:@"RSA Public Key" privateKeyTag:@"RSA Private Key"];
SecKeyRef test_publicKey = (__bridge SecKeyRef)(keys[0]);
SecKeyRef test_privateKey = (__bridge SecKeyRef)(keys[1]);


const char *sampled_utf8 = "VnWBW/xRyJB48Uxjdl99apczCuS07zhnLvIjnqyZIQqbI4F7kyAezfD1MNlgeTefkHuCRuzogaQTamk2XRwXoBoGy3Agj4ocPK2Wa7vWNGip8X3FAo1eJL+xKoVoqre/ipDjnZNfEUbX91Ru+IqWkbZXD2POlFfuMaTatCl50+U=";

NSString *sampled = [[NSString alloc] initWithUTF8String:sampled_utf8];

Byte sampled_inputData [[sampled lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];//prepare a Byte[]
[[sampled dataUsingEncoding:NSUTF8StringEncoding] getBytes:sampled_inputData];//get the pointer of the data
size_t sampled_inputDataSize = (size_t)[sampled length];
size_t sampled_outputDataSize = EstimateBas64DecodedDataSize(sampled_inputDataSize);//calculate the decoded data size
Byte sampled_outputData[sampled_outputDataSize];//prepare a Byte[] for the decoded data
Base64DecodeData(sampled_inputData, sampled_inputDataSize, sampled_outputData, &sampled_outputDataSize);//decode the data
NSData *sampled_Data = [[NSData alloc] initWithBytes:sampled_outputData length:sampled_outputDataSize];//create a NSData


NSData *test_encrypted_Data = [self encryptString:encoded_toEncrypt RSAPublicKey:test_publicKey padding:padding];

size_t test_encrypted_inputDataSize = (size_t)[test_encrypted_Data length];
Byte *test_encrypted_inputData = (Byte*) malloc(test_encrypted_inputDataSize);
memcpy(test_encrypted_inputData,[test_encrypted_Data bytes],test_encrypted_inputDataSize);

size_t test_encrypted_outputDataSize = EstimateBas64EncodedDataSize(test_encrypted_inputDataSize);//calculate the encoded data size
char* test_encrypted_outputData[test_encrypted_outputDataSize];//prepare a char for the encoded data
Base64EncodeData(test_encrypted_inputData, test_encrypted_inputDataSize, test_encrypted_outputData, &test_encrypted_outputDataSize,false);//encode the data
NSData *test_encrypted_Encoded = [[NSData alloc] initWithBytes:test_encrypted_outputData length:test_encrypted_outputDataSize];//create a NSData object from the decoded data


size_t input_Size = (size_t)[test_encrypted_Encoded length];
Byte *input = (Byte*) malloc(input_Size);
memcpy(input,[test_encrypted_Encoded bytes],input_Size);
size_t output_Size = EstimateBas64DecodedDataSize(input_Size);
char* output[output_Size];
Base64DecodeData(input, input_Size, output, &output_Size);
NSData *res = [[NSData alloc] initWithBytes:output length:output_Size];

NSData *test_decryptedData = [self decryptString:sampled_Data RSAPrivateKey:test_privateKey padding:padding];


-(NSData*)decryptString:(NSData*)original RSAPrivateKey:(SecKeyRef)privateKey padding:  (SecPadding)padding
 {
 @try
 {

    const unsigned char* original_String = (unsigned char *)[original bytes];
    size_t decryptedLength = SecKeyGetBlockSize(privateKey);
    uint8_t decrypted[decryptedLength];

    OSStatus status = SecKeyDecrypt(privateKey,
                                    padding,
                                    original_String,
                                    [original length],
                                    decrypted,
                                    &decryptedLength);

    NSLog(@"result = %@", [self fetchStatus:status]);

    if(status == noErr)
    {
        NSData* decryptedData = [[NSData alloc] initWithBytes:(const void*)decrypted length:decryptedLength];
        return decryptedData;
    }
    else
        return nil;
}
@catch (NSException * e)
{
    //do nothing
    NSLog(@"exception: %@", [e reason]);
}
return nil;
}


- (NSData*)encryptString:(NSString*)original RSAPublicKey:(SecKeyRef)publicKey1 padding:(SecPadding)padding
{
 @try
 {


    Byte encrypt_inputData [[original lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];//prepare a Byte[]
    [[original dataUsingEncoding:NSUTF8StringEncoding] getBytes:encrypt_inputData];//get the pointer of the data
    size_t encrypt_inputDataSize = (size_t)[original length];
    size_t encrypt_outputDataSize = EstimateBas64DecodedDataSize(encrypt_inputDataSize);//calculate the decoded data size
    Byte encrypt_outputData[encrypt_outputDataSize];//prepare a Byte[] for the decoded data
    Base64DecodeData(encrypt_inputData, encrypt_inputDataSize, encrypt_outputData, &encrypt_outputDataSize);//decode the data
    NSData *encryption_Data = [[NSData alloc] initWithBytes:encrypt_outputData length:encrypt_outputDataSize];//create a NSData object from the decoded data




    size_t encryptedLength = SecKeyGetBlockSize(publicKey1);

    uint8_t* sample = (uint8_t*)[encryption_Data bytes];
    size_t text_Size = [encryption_Data length];
    uint8_t *encrypted_Data_Bytes;
    encrypted_Data_Bytes = malloc(sizeof(uint8_t)*encryptedLength);
    memset(encrypted_Data_Bytes,0,encryptedLength);
    OSStatus status = SecKeyEncrypt(publicKey1,
                                    padding,
                                    sample,
                                    text_Size,
                                    &encrypted_Data_Bytes[0],
                                    &encryptedLength);


    if(status == noErr)
    {
        NSData* encryptedData = [[NSData alloc] initWithBytes:(const void*)encrypted_Data_Bytes length:encryptedLength];
        return encryptedData;
    }
    else
        return nil;
}
@catch (NSException * e)
{
    //do nothing
    NSLog(@"exception: %@", [e reason]);
}
return nil;
}
4

0 回答 0