RNCryptor / RNCryptor

CCCryptor (AES encryption) wrappers for iOS and Mac in Swift. -- For ObjC, see RNCryptor/RNCryptor-objc
https://groups.google.com/forum/#!forum/rncryptor
MIT License
3.36k stars 523 forks source link

This library is supported for all platforms #273

Closed smilydronavalli closed 6 years ago

smilydronavalli commented 6 years ago

Hi

I am encrypted image file in objectiveC using the below code

int blockSize = 32 * 1024;

NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle pathForResource:@"download" ofType:@"jpeg"];
NSInputStream *cryptedStream = [NSInputStream inputStreamWithFileAtPath:path];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* newDirPath = [documentsDirectory stringByAppendingPathComponent:@"C++.sd"];

NSOutputStream *decryptedStream = [NSOutputStream outputStreamToFileAtPath:newDirPath append:NO];

[cryptedStream open];
[decryptedStream open];
decryptor=nil;
// We don't need to keep making new NSData objects. We can just use one repeatedly.
data = [NSMutableData dataWithLength:blockSize];

dispatch_block_t readStreamBlock = ^{
    [self->data setLength:blockSize];
    NSInteger bytesRead = [cryptedStream read:[self->data mutableBytes] maxLength:blockSize];
    if (bytesRead < 0) {
        // Throw an error
    }
    else if (bytesRead == 0) {
        [self->decryptor finish];
    }
    else {
        [self->data setLength:bytesRead];
        [self->decryptor addData:self->data];
        NSLog(@"Sent %ld bytes to decryptor", (unsigned long)bytesRead);
    }
};

decryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
                                         password:@"blah"
                                          handler:^(RNCryptor *cryptor, NSData *data) {
                                              NSLog(@"Decryptor recevied %ld bytes", (unsigned long)data.length);
                                              [decryptedStream write:data.bytes maxLength:data.length];
                                              if (cryptor.isFinished) {
                                                  [decryptedStream close];
                                                  // call my delegate that I'm finished with decrypting
                                              }
                                              else {
                                                  // Might want to put this in a dispatch_async(), but I don't think you need it.
                                                  readStreamBlock();
                                              }
                                          }];

// Read the first block to kick things off
readStreamBlock();

I sent the above encrypted image file to C# for decryption but the image file is not decrypted same as I try to C# encrypted image file to decrypted in ObjectiveC But it is not happened

In C#

aesMode = AesMode.CTR; options = Options.V0; hmac_includesHeader = false; hmac_includesPadding = true; hmac_algorithm = HmacAlgorithm.SHA1;

Praveen, 20:29 private byte[] encryptAesEcbNoPadding (byte[] plaintext, byte[] key) { byte[] encrypted;

        var aes = Aes.Create();
        aes.Mode = CipherMode.ECB;
        aes.Padding = PaddingMode.None;
        var encryptor = aes.CreateEncryptor(key, null);

        using (var ms = new MemoryStream())
        {
            using (var cs1 = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
                cs1.Write(plaintext, 0, plaintext.Length);
            }
            encrypted = ms.ToArray();
        }
        return encrypted;
    }

Thanks inadvance

smilydronavalli commented 6 years ago

Hi,

Kindly give me reply of the above query.Here we are waiting for the above query reply.It's very important for us.

Thanks InAdvance

rnapier commented 6 years ago

RNCryptor is a specific encryption format. It is not a general-purpose library for handling custom formats like your C#. If you plan to use the RNCryptor format on both platforms, see RNCryptor-cs for a C# implementation.

If you need the C# format as given, then you'll need to reimplement in Objective-C. I can implement this for you on a consulting basis if you need this service.