Search code examples
objective-ccocoaencryptionrncryptor

Cannot decrypt a text file via RNCryptor


i have successfully encrypted a text file via RNcryptor with this code

NSFileManager *filemgr;
NSData *databuffer;

filemgr = [NSFileManager defaultManager];
NSString *destPath = @"Users/DivyaDinesh/Downloads/abcde";
databuffer = [filemgr contentsAtPath: @"Users/DivyaDinesh/Downloads/abcd" ];

NSError *error;
NSData *encryptedData = [RNEncryptor encryptData:databuffer
                                withSettings:kRNCryptorAES256Settings
                                        password:@"ABC123"
                                           error:&error];
[encryptedData writeToFile:destPath atomically:YES];

Then i tried to decrypt the same using this code

NSString *destPath1 = @"Users/DivyaDinesh/Downloads/abcdg";
NSData *encryptedData = [NSData dataWithContentsOfFile:@"Users/DivyaDinesh/Downloads/abcde"];


NSError *error;

NSData *decryptedData = [RNDecryptor decryptData:encryptedData  withPassword:@"ABC123" error:&error];
[decryptedData writeToFile:destPath1 atomically:YES];

i get a blank text file as output, what am i doing wrong here? please help

why? how to fix this can anyone give a solution


Solution

  • ok, if that is the case, then i think your file is corrupted or system is not fetching it. do one thing, try to encrypt and decrypt another file, try if it works or not.

    it sometimes happen in mac when u try to create a text file in mac in textedit and when u save it as say somefile.txt a warning comes and says "cannot save this document with extension txt to the end of the name" like that, note that it happens when you try to save it with a ".txt" extention to the end of its name.

    i created a file in textedit and when i encryped like u said in the question, i got its size as 82bytes, then i created 2 more file in text edit and encrypted them they also contain the same size 82. And i was not able to decrypt the same files, the decrypted file size is shown as 0bytes, with no contents at all. I dont know if u r getting the decrypted file as 0 bytes.

    then i gave a file which does not exist in my mac book as source file to encrypt. to my wonder, i got an encrypted file with the name i provided, and that too with the same 82bytes size!!

    Then for the solution create a textfile in textedit and saved as rtf, then go to the place where u stored it in ur system, and rename the file like this.

    if your file name is somefile then rename it as somefile.txt.

    Now try to do the encrypting and decrypting again, it will work :-)