iOS development BASE64 encoding and encryption-related learning

Source: Internet
Author: User
Tags md5 encryption asymmetric encryption



One:. Base64 Supplement



"' OBJC



1.base64 Simple Explanation



Description: Base64 can be a cornerstone of cryptography and is very important.



Features: Arbitrary binary data can be BASE64 encoded



Result: All data can be encoded as a text file that can be represented with only 65 characters.



65 characters: A~z a~z 0~9 +/=



File data changes after Base64 encoding of the file: encoded data ~= 4/3 of the data before encoding, will be about 1/3.






2. Command line for BASE64 encoding and decoding



Code: Base64 123.png-o 123.txt



Decoding: Base64 123.txt-o test.png-d






2.BASE64 Coding principle



1) Convert all characters into ASCII code;



2) Convert ASCII code to 8-bit binary;



3) The binary 3 into a group (less than 3 in the back of 0) a total of 24, and then split into 4 groups, 6 bits per group;



4) Unified in 6-bit binary before two 0 to fill 8 bits;



5) Convert the binary into decimal after 0.



6) Obtain the decimal corresponding BASE64 code from the BASE64 encoding table;






Description of the processing process:



A. When converting, the data of three byte is placed in a 24bit buffer successively, and the first byte occupies a high position.



B. If the data is less than 3byte, the remaining bits in the buffer are filled with 0. Then, each time you take out 6 bit, select the corresponding character as the encoded output according to the value of the check table.



C. Continuous, until all input data conversion is complete.



D. If the last two input data is left, add 1 "=" after the coded result;



E. If the last input data is left, add 2 "=" after the coding result;



F. If there is no data left, do not add anything, so as to ensure the correctness of the data restoration.






3. Implement



A. Description:



1) from iOS7.0 onwards, Apple provides Base64 encoding and decoding support



2) If you are an older project, you will also see a third-party framework for Base64 encoding and decoding, and it is recommended to replace if the following versions of iOS7.0 are not currently supported.






B. Related code:



Given a string, Base64 encodes the string and returns the encoded result



-(NSString *) base64encodestring: (NSString *) string



{



1. Convert the string to binary data first



NSData *data = [string datausingencoding:nsutf8stringencoding];






2. base64 encoding of binary data, returns the encoded string



return [data base64encodedstringwithoptions:0];



}






Decoding a Base64 encoded string



-(NSString *) base64decodestring: (NSString *) string



{



1. "Decode" the Base64 encoded string into binary data



NSData *data = [[NSData alloc]initwithbase64encodedstring:string options:0];






2. Converting binary data to string return



return [[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding];



}






C. Terminal Test commands



$ echo-n A | Base64



$ echo-n qq== |base64-d



```



II: Cryptographic Learning:



Encryption related



"' OBJC



Principles of Web Application data:






1. "Do not allow" to transmit "clear text" of user's privacy data on the network



2. "Do not allow" to save "clear text" of user's privacy data on-premises






Encryption related






1. Base64 encoding Format



2. Cryptography Evolution "Secret book"-->rsa






RSA Simple Explanation: The encryption algorithm algorithm is public, the encryption method is as follows:






-"Public key" encryption, "private key" decryption



-"Private key" encryption, "public key" decryption






The current popular encryption method:



---------------



-Hashing (hash) function



-MD5



-SHA1



-SHA256






-Symmetric encryption algorithm



-DES



-3DES



-AES (Advanced password standard, used by U.S. National Security Agency)






-Asymmetric Encryption Algorithm (RSA)






hash function:



---------------



Characteristics:



-The algorithm is public



-"Encrypt the same data and get the same results"



-For different data encryption, the result is fixed length, MD5 the different data encryption, the result is 32 characters length of the string



-Information Digest, information "fingerprint", is used to do data recognition!



-it can't be reversed.






Use:



-Password, the server does not need to know the user's real password!



-Search



Miss Zhang Yang teacher old division



Mr. Cang teacher Zhang Teacher Yang






Miss Zhang 1bdf605991920db11cbdf8508204c4eb



Teacher Yang 2d97fbce49977313c2aae15ea77fec0f



Ms. Cang 692e92669c0ca340eff4fdcef32896ee






How to judge: the search for each keyword to three columns, to get the result of a scatter, the result of a bitwise addition if it is the same, the search content is the same!



-Copyright



Copyright protection, document recognition.






Crack:



-Http://www.cmd5.com records more than 24 trillion, a total of 160T hard drive password data, through the massive data search results!






Improved MD5 encryption security with two solutions



1. Add "salt" (seasoning)



2. HMAC: Given a "secret key", encrypt the plaintext and do "two hashes"! The resulting result, or 32 characters











#import "ViewController.h"
#import "NSString+Hash.h"

//sufficiently long + salty enough + complicated enough
#define salt @"shdcskjfcbskfnslfhs.kfsfvmsf8348390(*^^6R%@@IJEKHRKWKFGKF"

@interface ViewController ()

@end

@implementation ViewController
/**
 * 1: MD5 encryption is irreversible, generally need: plaintext + salt + out of order: relatively easy to crack
 *
 */
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    //(plain text + salt) MD5
    
    NSLog(@"%@",[@"520it" md5String]);
    NSLog(@"%@",[[@"520it" stringByAppendingString:salt] md5String]);
    
    / / First encryption + out of order
    //cb0fe21bfcc4c2625469d8ec6f3d710d--->12345
    
    NSLog(@"%@",[@"520it" hmacMD5StringWithKey:@"xiaomage"]);
    
// NSLog(@"%@",[self base64EncodeString:@"A"]);
// NSLog(@"%@",[self base64DecodeString:@"QQ=="]);
}

/ / Base64 encoding a string, and return
-(NSString *)base64EncodeString:(NSString *)string
{
    //1. Convert to binary data first
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    //2. Base64 encoding binary data, returning string after completion
    Return [data base64EncodedStringWithOptions:0];
}

/ / Decode the string after base64 encoding, and return
-(NSString *)base64DecodeString:(NSString *)string
{
    / / Note: the string is a base64 encoded string
    //1. Convert to binary data (complete the decoding process)
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string options:0];
    
    //2. Convert binary data to a string
    Return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
@end  







iOS development BASE64 encoding and encryption-related learning


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.