2

RSA 暗号化の 2 つのシナリオがあります。

  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