iOS client learns AES encryption and decryption

Source: Internet
Author: User
Tags decrypt

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 Plaincopy
  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. }

[CPP]View Plaincopy
    1. -(NSString *) getpwd
    2. {
    3. Nsuserdefaults * settings = [Nsuserdefaults standarduserdefaults];
    4. NSString * temp = [Settings objectforkey:@"Password"];
    5. return  [Aescrypt decrypt:temp password:@"pwd"];
    6. }

from the method name of the above two methods to know the method function, 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, save the user name and password is to place the user name and password in a local sandbox, Obtained when the encrypted file is read directly from the local, decrypted and the user data on the server to compare.


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 Plaincopy
    1. nsstring *pwdkey = @ "Fresh air for Waves"  ;
    2. NSString *password = @"hello123456";
    3. NSString *encryptedpwd = [Aescrypt encrypt:password Password:pwdkey];
    4. NSString *decryptedpwd = [Aescrypt decrypt:encryptedpwd Password:pwdkey];
    5. NSLog (@"Encrypted password:%@ after decryption password:%@", encryptedpwd,decryptedpwd);


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


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 Plaincopy
    1. + (NSString *) Encrypt: (NSString *) message password: (NSString *) password;
    2. + (NSString *) Decrypt: (NSString *) base64encodedstring Password: (NSString *) password;

iOS client learns AES encryption and decryption

Related Article

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.