1

My task is to encrypt a file(.png, .txt, any..)

In order to achieve it what I doing is

Encryption:

  1. Read a file and store it into NSData.
  2. Convert NSData to NSString.
  3. Encrypt the NSString with help of AESCrypt
  4. Store the NSString in a file

Decryption

  1. Read the encrypted string
  2. Decrypt it with the help of AESCrypt
  3. Convert it back to NSData
  4. Save it back to some location

Below is the code that I am doing in order convert a file to NSString:

NSString* sourceFile = @"/Users/Vikas/Desktop/theHulk.png";

NSData *data = [[NSFileManager defaultManager] contentsAtPat
h:sourceFile];

NSString *dataAsString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Problem:

The above code is able to read and store the file to NSData however when I am converting the NSData to NSString, the value I am getting is nil

Research

StackOver Flow 1

StackOver Flow 2

S.P: If you have better suggestion for file encryption then please let me know as I am newbie.

Community
  • 1
  • 1
Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100
  • 1
    Can you encrypt the data without converting to string? Generally, encryption works on bytes, not just strings – Carl Veazey Sep 30 '15 at 07:23
  • Actually, the library(AESCrypt) I am using is able to encrypt strings so I thought the if I want to encrypt a file then I need to convert it to NSString. It would be great if you could give me a hit about how to en/de-crypt NSData @CarlVeazey – Vikas Bansal Sep 30 '15 at 07:26
  • 2
    Dont use that library then. Use common crypto directly or a diff abstraction of it – Carl Veazey Sep 30 '15 at 07:29
  • @CarlVeazey could you give me a reference link of what you are suggesting. Please do not mind as I have mentioned I am 1-2 month new to this language and cocoa :) – Vikas Bansal Sep 30 '15 at 07:35
  • 1
    http://stackoverflow.com/questions/2579453/nsdata-aes-class-encryption-decryption-in-cocoa got that from searching encrypt nsdata, I believe you may be able to make similar searches to learn more ;) – Carl Veazey Sep 30 '15 at 07:37
  • Many Thanks @CarlVeazey "Stay Hungry, Stay Foolish" – Vikas Bansal Sep 30 '15 at 07:48
  • 1
    Not all data can be converted to any particular string encoding, in particular UTF-8. For tat reason if a string representation is needed the general solution is either to use Base64 or hexadecimal encoding. – zaph Sep 30 '15 at 11:50
  • You can use Common Crypto directly but that is not advised unless you are well steeped in cryptography. A better solution is to use a well vetted project such as [RNCryptor](https://github.com/RNCryptor/RNCryptor) which also has support for many other languages. RNCryptor handles the necessary details of using AES encryption securely. – zaph Sep 30 '15 at 11:53
  • @zaph many thanks :) I'll go through the RNCryptor. God bless you and "Stay hungry, Stay foolish" – Vikas Bansal Sep 30 '15 at 11:55

1 Answers1

0

According to this blog the string to data conversion forces a trailing \0 byte, which you might have to remove. This can be done as follows:

data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];

Try see if that works for you.

bilo-io
  • 597
  • 3
  • 17
  • thank you for you answer. I am now directly encrypting NSData instead of converting it to NSString first. However, I have tried your way and it is still not working and still NSData is not getting converted to NSString. What's happening: I get NSData of an image in to a NSData variable named "data" then I used your line of code and then I tried to convert it to the NSString however, it is not working and the NSString is still nil. – Vikas Bansal Oct 01 '15 at 06:59