GithubHelp home page GithubHelp logo

Comments (1)

dschlaba avatar dschlaba commented on August 24, 2024

If compiling for iOS 7 and above, you no longer need the NSData+Base64 files.

Here is an updated m file that compiles in iOS10

http://pastebin.com/CjAEDsgb

Now to test:

`+ (void)test{

StringEncryption *crypt = [[StringEncryption alloc] init];

NSString *secret = @"This is the text to be encrypted"; // this is the text that you want to encrypt.

NSString *key = [[StringEncryption alloc] sha256:@"my secret key" length:32]; // this is very important, 32 bytes = 256 bit

NSString *iv = [crypt generateRandomIV:16]; // Here we are generating random initialization vector (iv).
                                            // Length of this vector = 16 bytes = 128 bits

// Now that we have input text, hashed key and random IV, we are all set for encryption:
NSData *encryptedData = [crypt encrypt:[secret dataUsingEncoding:NSUTF8StringEncoding] key:key iv:iv];

// Here is your encrypted string to pass.
NSString *encryptedString = [encryptedData base64EncodedStringWithOptions:0];

//--- SOME ACTION ---//
//--- Save to a database, pass to another device, etc. ---//

// Here is how you convert your passed string back into an NSData object.
NSData *encryptedDataSent = [[NSData alloc] initWithBase64EncodedString:encryptedString options:NSDataBase64DecodingIgnoreUnknownCharacters];

// 2. Decryption
//      Decrypt the data and then convert it back into a string.
//      You will have to use the same IV and key which was used for encryption.
NSData *decryptedData = [[StringEncryption alloc] decrypt:encryptedDataSent key:key iv:iv];
NSString *decryptedText = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];

NSLog(@"iv=%@", iv);
NSLog(@"key=%@", key);
NSLog(@"Plain text=%@", secret);
NSLog(@"encrypted=%@", encryptedString);
NSLog(@"decrypted=%@", decryptedText);

}
`

Remember, the RandomIV has to be the same on both sides so you will need to figure out how to pass it to whatever device is doing the decrypt.

from cross-platform-aes-encryption.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.