AES Encryption for an NSString on the iPhone

前端 未结 5 1596
陌清茗
陌清茗 2020-11-22 05:56

Can anybody point me in the right direction to be able to encrypt a string, returning another string with the encrypted data? (I\'ve been trying with AES256 encryption.) I w

5条回答
  •  借酒劲吻你
    2020-11-22 06:35

    I have put together a collection of categories for NSData and NSString which uses solutions found on Jeff LaMarche's blog and some hints by Quinn Taylor here on Stack Overflow.

    It uses categories to extend NSData to provide AES256 encryption and also offers an extension of NSString to BASE64-encode encrypted data safely to strings.

    Here's an example to show the usage for encrypting strings:

    NSString *plainString = @"This string will be encrypted";
    NSString *key = @"YourEncryptionKey"; // should be provided by a user
    
    NSLog( @"Original String: %@", plainString );
    
    NSString *encryptedString = [plainString AES256EncryptWithKey:key];
    NSLog( @"Encrypted String: %@", encryptedString );
    
    NSLog( @"Decrypted String: %@", [encryptedString AES256DecryptWithKey:key] );
    

    Get the full source code here:

    https://gist.github.com/838614

    Thanks for all the helpful hints!

    -- Michael

提交回复
热议问题