IOS Security---AES encryption

Source: Internet
Author: User

Data encryption plays an important role in the software development process, and some companies may have their own internal design algorithms when encrypting, and in this regard do not want to waste too much effort to consider using third-party provided encryption algorithm, such as AES encryption algorithm, This article describes the open source Chinese iOS client using the ASE algorithm encryption password;

AES GitHub Https://github.com/Gurpartap/AESCrypt-ObjC

For a larger project we may not know where a class library or method is used, but the smart Xcode provides us with a global search function that we can search for in the whole project. For example, I don't know where AES is used, but if you use this class library It will definitely refer to its header file, we search for Aescrypt

Then in addition to the class library itself only the Config class contains AESCrpt.h, only two methods used, jump to config.m in the two methods

[CPP]View Plaincopyprint?
  1. -(void) Saveusernameandpwd: (NSString *) userName andpwd: (NSString *) pwd
  2. {
  3. Nsuserdefaults * settings = [Nsuserdefaults standarduserdefaults];
  4. [Settings removeobjectforkey:@"UserName"];
  5. [Settings removeobjectforkey:@"Password"];
  6. [Settings Setobject:username forkey:@"UserName"];
  7. PWD = [Aescrypt encrypt:pwd password:@"pwd"];
  8. [Settings setobject:pwd forkey:@"Password"];
  9. [Settings synchronize];
  10. }
-(void) Saveusernameandpwd: (NSString *) userName andpwd: (NSString *) pwd{    nsuserdefaults * settings = [ Nsuserdefaults Standarduserdefaults];    [Settings removeobjectforkey:@ "UserName"];    [Settings removeobjectforkey:@ "Password"];    [Settings Setobject:username forkey:@ "UserName"];        PWD = [Aescrypt encrypt:pwd password:@ "pwd"];        [Settings setobject:pwd forkey:@ "Password"];    [Settings synchronize];}
[CPP]View Plaincopyprint?
    1. -(NSString *) getpwd
    2. {
    3. Nsuserdefaults * settings = [Nsuserdefaults standarduserdefaults];
    4. NSString * temp = [Settings objectforkey:@"Password"];
    5. return [aescrypt decrypt:temp password:@"pwd"];
    6. }
-(NSString *) getpwd{    nsuserdefaults * settings = [nsuserdefaults standarduserdefaults];    NSString * temp = [Settings objectforkey:@ "Password"];    return [Aescrypt decrypt:temp password:@ "pwd"];}

From the method name of the above two methods to know the role of the method, one is to save the user name and password, the password using AES encryption, the other is to decrypt the password and then return this password, the user name and password is stored in a local sandbox only, get the time to read the encrypted file directly from the local, After decryption and user data on the server to compare (want to know the open source Chinese iOS client user login process, please follow the next blog "Open source China iOS Client Learning-(12) User Login", will be launched);

As the official sample usage, AES is very simple to use, first to add a header file #import "AESCrypt.h", using the example

[CPP]View Plaincopyprint?
    1. nsstring *pwdkey = @ "fresh air for Waves";  
    2. nsstring *password = @
    3.   
    4. nsstring *encryptedpwd = [aescrypt  encrypt:password password:pwdkey];  
    5. nsstring *decryptedpwd =  [aescrypt decrypt:encryptedpwd password:pwdkey];  
    6.   
    7. nslog (@ "encrypted after password:%@   decrypted password:  %@", encryptedpwd,decryptedpwd);   
    NSString *pwdkey = @ "Fresh air for waves";    NSString *password = @ "duxinfeng123456";        NSString *encryptedpwd = [Aescrypt encrypt:password password:pwdkey];    NSString *decryptedpwd = [Aescrypt decrypt:encryptedpwd password:pwdkey];        NSLog (@ "Encrypted password:%@  after decryption password:%@", encryptedpwd,decryptedpwd);    

Printing results: encrypted password:/ottra5qz5+xjhb809apla== after decryption password: duxinfeng123456

Cryptographic decryption method function prototype, the two parameters passed the first is the encrypted data, the second is the key to encrypt the data, the decryption time also need this key to decrypt the encrypted data;

[CPP]View Plaincopyprint?
    1. + (NSString *) Encrypt: (NSString *) message password: (NSString *) password;
    2. + (NSString *) Decrypt: (NSString *) base64encodedstring Password: (NSString *) password;

Reprint Annotated Source: http://blog.csdn.net/duxinfeng2010

IOS Security---AES encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.