iOS Development MD5 and Base64

Source: Internet
Author: User
Tags md5 encryption md5 hash asymmetric encryption


1.MD5

MD5 is message-digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure complete and consistent information transmission. is one of the widely used hashing algorithms (also translation digest algorithm, hashing algorithm), mainstream programming language has been widely MD5 implemented. The calculation of data (such as Chinese characters) as another fixed length value is the basic principle of the hashing algorithm, and the predecessor of MD5 is MD2, MD3 and MD4. the MD5 algorithm has the following characteristics:1, compressibility: Any length of data, calculated the length of the MD5 value is fixed. 2, easy to calculate: It is easy to calculate the MD5 value from the original data. 3, anti-modification: Any changes to the original data, even if only 1 bytes modified, the resulting MD5 value is very different. 4, strong anti-collision: known raw data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, falsification of data). MD5 's role is to allow bulk information to be "compressed" into a confidential format before signing a private key with a digital signature software (that is, converting an arbitrary-length byte string into a long hexadecimal string). In addition to MD5, among them the more famous are sha-1, Ripemd and Haval and so on. Simply put, MD5 is one of the network encryption technology, that is, an arbitrary character composed of ordinary password, through the MD5 algorithm, replaced by a certain long hexadecimal string. For example, a password is: 123456 through MD5 encryption has become: General MD5 encryption ($pass) 32-bit MD5 uppercase: e10adc3949ba59abbe56e057f20f883e32 bit MD5 lowercase: e10adc3949ba59abbe5 6e057f20f883e16 bit MD5 capital: 49ba59abbe56e05716 bit MD5 lowercase: 49ba59abbe56e057


-(NSString *) MD5 {


const char *STR = [self utf8string];


unsigned char digest[cc_md5_digest_length];


CC_MD5 (str, (Cc_long) strlen (str), digest);


nsmutablestring *result = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];


for (int i = 0; i < cc_md5_digest_length; i++)


[Result appendformat:@ "%02x", Digest[i]];


return result;


}



-(nsstring*) MD5


{


Create pointer to the string as UTF8


const char *ptr = [self utf8string];


Create byte array of unsigned chars

unsigned char md5buffer[cc_md5_digest_length];


Create A byte MD5 hash value, store in buffer



Cc_md5 (PTR, (cc_long) strlen (PTR), md5buffer);


Convert MD5 value in the buffer to NSString of hex values


nsmutablestring *output = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];


for (int i = 0; i < cc_md5_digest_length; i++)


[Output appendformat:@ "%02x", Md5buffer[i]];


return output;


}






2.base64


BASE64 Coding principle


BASE64 encoding is called BASE64 because it uses 64 characters to encode arbitrary data, with the same Base32, BASE16 encoding.


These 64 characters are a subset of the characters used in a variety of character encodings (such as ASCII encoding), Basic, and printable. The only thing that is special is the last two characters, because of the choice of the last two characters, there are many variants of Base64 encoding, such as Base64 URL encoding.



BASE64 encoding is essentially a scheme for turning binary data into textual data. For non-binary data, it is first converted to binary form, and then each successive 6 bits (2 of the 6 square =64) calculates its decimal value, according to the value in the above index table to find the corresponding character, and finally get a text string.



By NSSTRING+BASE64 Classification to achieve


#import <Foundation/Foundation.h>
@interface NSString (Base64)
/**
  * Convert to Base64 encoding
  */
  - (NSString *)base64EncodedString;
  /**
  * Restore Base64 encoding
  */
  - (NSString *)base64DecodedString;
  @end

 
#import "NSString+Base64.h" @implementation NSString (Base64)

- (NSString *)base64EncodedString;
{ NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding]; return [data base64EncodedStringWithOptions:0];
}

- (NSString *)base64DecodedString
{ NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0]; return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
} @end
Someone may misuse it for data encryption or data verification without understanding Base64 encoding.

BASE64 is a data encoding method that allows data to conform to the requirements of a transport protocol. Standard BASE64 encoding decoding without additional information is completely reversible, even if your own custom character set design a class BASE64 encoding for data encryption, which is easier to crack in most scenarios.



For data encryption, you should use a special encryption algorithm that is not yet effective to quickly crack. For example: Symmetric encryption algorithm AES-128-CBC, symmetric encryption requires a key, as long as the key is not disclosed, it is often difficult to crack, you can also use asymmetric encryption algorithm, such as RSA, using the maximum integer factor decomposition of the high computational capacity, so that the use of public key encrypted data, only use the private key to quickly decrypt.



For data validation, you should also use a special message authentication code generation algorithm, such as HMAC-a method of constructing a message authentication code using a one-way hash function, whose process is irreversible, uniquely deterministic, and uses a key to generate the authentication code, which is designed to prevent the data from being tampered with or forged during transmission. The original data is transmitted with the authentication code, the data receiving end uses the same key and the same algorithm to generate the authentication code again, compare with the original authentication code, and verify the legitimacy of the data.



The Base64 combines the character set size and the encoded data length, and can flexibly replace the last two characters of the character set to accommodate a wide range of requirements and scenarios.



iOS Development MD5 and Base64


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.