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?
- -(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];
- }
-(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?
- -(NSString *) getpwd
- {
- Nsuserdefaults * settings = [Nsuserdefaults standarduserdefaults];
- NSString * temp = [Settings objectforkey:@"Password"];
- return [aescrypt decrypt:temp password:@"pwd"];
- }
-(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?
- nsstring *pwdkey = @ "fresh air for Waves";
- nsstring *password = @
-   
- nsstring *encryptedpwd = [aescrypt encrypt:password password:pwdkey];
- nsstring *decryptedpwd = [aescrypt decrypt:encryptedpwd password:pwdkey];
-
- 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?
- + (NSString *) Encrypt: (NSString *) message password: (NSString *) password;
- + (NSString *) Decrypt: (NSString *) base64encodedstring Password: (NSString *) password;
Reprint Annotated Source: http://blog.csdn.net/duxinfeng2010
IOS Security---AES encryption