Some commonly used encryption introduction

Data security is very important in iOS development (and I should say in all development). The following is a simple way to say about data encryption.

Recommended reading:IOS interview salary, advanced will you these? (Updated)

MD2, MD3, MD4, MD5 (MD6) message-digest Algorithm (MESSage-digest Algorithm) MD2, MD3, MD4, MD5 (MD6) message-digest Algorithm (MESSage-digest Algorithm) For a more detailed description of the algorithm, visit Google.

SHA1, SHA224, SHA256, SHA384, and SHA512 Secure Hash algorithms. A Hash is an extraction of information. The length of a Hash is usually much smaller than that of the information and is a fixed length. A highly encrypted hash must be irreversible, which means that no part of the original information can be deduced from the hash result. Any change in the input, even by one bit, will result in a significant change in the hash result, known as the avalanche effect. Hashes should also be conflict-proof, meaning that no two pieces of information can be found with the same hash result. Hash results with these properties can then be used to verify that information has been modified. See more introduction

HmacMD5, HmacSHA1, HmacSHA224, HmacSHA256, HmacSHA384, HmacSHA512 these encryption algorithms and the above comparison, the difference is that these need a secret key to input and generate message digest as output. These encryptions are suitable for server authentication clients:

Received the client sends a request, the server returns a random number, save the random number in the session at the same time, the client will return to the information and random Numbers using these algorithms signature sent to the server, server just record database of random Numbers and to read relevant information (such as user name password) using the same hash algorithm to get their signature information encryption and the client, To verify whether the client is legitimate (intercepted tampering, etc.). More introduction

RSA Public-key encryption algorithm. The encryption party uses the public key for encryption, and the decryptor uses the private key for decryption. The public key and private key are uniquely matched, but the private key cannot be calculated if the public key is used. The encryption algorithm can expose the encryption algorithm and public key. It’s also nice to use the server to validate the client.

All the above encryption algorithms except RSA are irreversible encryption.

DES, 3DES, AES, RC2, RC4, RC2, CAST, Blowfish. Through the secret key and initialization vector using the encryption mode. The encryption modes are as follows:

  • The ECB mode is relatively simple and easy to implement. The same plaintext produces the same ciphertext, so the security is relatively low. In this mode, the initialization vector is ignored.
  • CBC mode, need to initialize vector, error will be transmitted, higher security than ECB mode.
  • In CFB mode, vectors need to be initialized. The plaintext mode is hidden, which is prone to error propagation and reduces the encryption rate.
  • OFB mode is not conducive to parallel processing and overcomes the error transmission problem.

The above algorithm in iOS implementation

Fortunately, the above algorithm is implemented by Apple for us, we only need to call the corresponding interface. Here we have written an interface, a bit of encryption algorithm encapsulation, more conducive to the use of the project (mainly the system C function N parameters, look tired ah!) .

First we enumerate the encryption methods

typedef NS_ENUM(NSInteger, HCDStringEncryptType) { HCDStringEncryptTypeMD2 = 0, HCDStringEncryptTypeMD4, HCDStringEncryptTypeMD5, HCDStringEncryptTypeSHA1, HCDStringEncryptTypeSHA224, HCDStringEncryptTypeSHA256, HCDStringEncryptTypeSHA384, HCDStringEncryptTypeSHA512, HCDStringEncryptTypeHmacMD5, / / there can be a key HCDStringEncryptTypeHmacSHA1, / / can have a key HCDStringEncryptTypeHmacSHA256, / / there can be a key HCDStringEncryptTypeHmacSHA384, / / there can be a key HCDStringEncryptTypeHmacSHA512, / / can have a key HCDStringEncryptTypeHmacSHA224, / / there can be a key HCDStringEncryptTypeRC2 = 100, // /*****************************/ HCDStringEncryptTypeRC4, // /*****************************/ HCDStringEncryptTypeAES, // /*****************************/ HCDStringEncryptTypeAES128, // *** using default ECB mode **/ /AES currently only supports AES, AES128 HCDStringEncryptTypeDES, / / / * * * * * initialization vector iv is ignored * * * * / HCDStringEncryptType3DES, / / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / HCDStringEncryptTypeCAST, // /*****************************/ HCDStringEncryptTypeBlowfish, // /*****************************/ };Copy the code

And then we define this interface

@interface HCDStringEncryptObject : NSObject * (nullable HCDStringEncryptObject *)initWithOriginString:(nullable NSString *)originString keyString:(nullable  NSString *)keyString encryptType:(HCDStringEncryptType)encryptType isBase64:(BOOL)base64; * (void)base64 NS_AVAILABLE(10_9, 7_0); * (void)base64Decode NS_AVAILABLE(10_9, 7_0); /** * Decryption method for reversible encryption */ * (void)decode; @property (nonatomic,readonly,getter=isBase64) BOOL base64; @property (nonatomic,readonly) HCDStringEncryptType encryptType; @property (strong, nonatomic, nullable,readonly) NSString *keyString; @property (strong, nonatomic, nullable,readonly) NSString *originString; @property (strong, nonatomic, nullable,readonly) NSString *encryptedString; @property (strong, nonatomic, Nullable,readonly) NSData *encryptedData; // After decryption, NSData @end of originStringCopy the code

In this way, we can get the encryption way, the encrypted data and the encrypted string, but also can choose whether to carry out base64 encoding is very convenient, as for the implementation, mainly call the system encryption implementation, and then the whole. Must remember to import the # import < CommonCrypto/CommonCrypto. H > oh

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS communication group: 642363427. No matter you are small white or big bull, welcome to enter.

Article from the network, such as infringement please contact xiaobian to delete