Encryption tools and App Process Tools for app development processes
Starting from this record, the record is a dry product, and it is a daily accumulation of programming.
I suggest you add basic tools to the project first, and the development efficiency will increase exponentially in the future. If you find that a large number of commonly used tools are missing during feature development, it will not only interrupt your thinking, but also slow down your development pace.
Of course, at the beginning of each project, it is impossible to fully prepare all the tools. You can only evaluate the tools that need to be prepared in advance based on your personal experience.
A good craftsman must have a good toolbox and constantly optimize it.
Slightly expand the project architecture.
1. Add the "Utilities" folder for the project in the base directory as the toolkit, create the "Utilities. h" header file, and reference this header file in PrefixHeader. pch.
2. Create three NSObject sub-classes "StringHelper", "ImageHelper", and "EncryptionHelper" as three tool classes and add them to the reference list of Utilities. h.
3. Add the "Categories" and "Libraries" structures in the base directory to store Categories and class Libraries respectively.
4. I further split the class library into a third-party class library and a self-created class library.
Encryption tool class EncryptionHelper
Currently, base64 encoding and decoding are recorded. MD2, MD4, MD5, SHA1, shaloud, SHA256, SHA384, SHA512 encryption, AES, and DES encryption and decryption are performed.
1. base64 encoding and decoding
Because Google's GTMBase64 class library is used, this library is added to the third-party library directory.
Autorelease is used in the code of this library to add objects to the Auto Release pool. In the ARC Project, such code can be cleared.
You can also find GTMBase64.m in the Target project> Build Phases> Compile Sources, double-click it, and enter-fno-objc-arc in the input box on the right to remove the ARC Management file.
After you reference GTMBase64.h in EncryptionHelper. m, you can implement the following methods:
+ (NSString *)base64EncodeWithString:(NSString *)string{ NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSString *result = [GTMBase64 stringByEncodingData:data]; return result;}+ (NSString *)decodeBase64WithString:(NSString *)string{ NSData *data = [GTMBase64 decodeString:string]; NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return result;}
2. MD5 Encryption
Because it is irreversible (probably can be understood as a binary equation with numerous multiple solutions), only encryption algorithms can record
+ (NSString *) MD5SumWithString :( NSString *) string {NSData * data = [string dataUsingEncoding: NSUTF8StringEncoding]; uint8_t buffer [CC_MD5_DIGEST_LENGTH]; CC_MD5 (data. bytes, (CC_LONG) data. length, buffer); // The following annotation code is equivalent to the preceding encryption code/data = [data MD5Sum]; // Byte * buffer = (Byte *) data. bytes; NSMutableString * result = [NSMutableString stringWithCapacity: CC_MD5_DIGEST_LENGTH * 2]; for (int I = 0; I <CC_MD5_DIGEST_LENGTH; I ++) {[result appendFormat: @ "% 02x", buffer [I];} return result ;}
Note:
A. Note that many developers use the following method to convert NSString to NSData:
Const char * cstr = [string cStringUsingEncoding: NSUTF8StringEncoding];
NSData * data = [NSData dataWithBytes: cstr length: string. length];
I do not recommend using the above Code. After testing, when the string is only English letters, numbers, and symbols, the results are the same; but when the string contains Chinese characters, string. the length obtained by length is incorrect. Because the length of a Chinese character converted to NSData is greater than 1, the above method will cause content loss after the Chinese character is converted!
We recommend that you use the following method: NSData * data = [string dataUsingEncoding: NSUTF8StringEncoding];
B. The MD2, MD4, SHA1, shaloud, SHA256, SHA384, and SHA512 encryption algorithms only need to replace the buffer length CC_MD5_DIGEST_LENGTH and the encryption method CC_MD5 in the sample code with the corresponding name.
C. The comment code serves the same purpose as the first two lines. However, NSData + CommonCrypto. h must be referenced.
D. data. bytes can be strongly converted to (Byte *) and (char *). refer to the following code:
E. the reserved length of the result string is CC_MD5_DIGEST_LENGTH * 2. Because the element type in the buffer array is uint8_t (that is, unsigned char), two digits are displayed in hexadecimal format during string formatting, so the length is 2 times
F. % 02x placeholder indicates that two digits are displayed in hexadecimal notation, and 0 is added to the left when the number of digits is less
3. AES and DES encryption and decryption
AES and DES encryption process: string-> data-> AES/DES encrypt-> base64 encode-> string; decryption process is reverse
+ (NSString *)AES256EncryptedString:(NSString *)string usingKey:(NSString *)key{ NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSData *encryptedData = [data AES256EncryptedDataUsingKey:key error:nil]; NSData *base64Data = [GTMBase64 encodeData:encryptedData]; NSString *result = [[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding]; // const char *buffer = (char *)base64Data.bytes;// result = [[NSString alloc] initWithCString:buffer encoding:NSUTF8StringEncoding];// result = [[NSString alloc] initWithBytes:base64Data.bytes length:base64Data.length encoding:NSUTF8StringEncoding]; return result;}+ (NSString *)decryptedAES256String:(NSString *)string usingKey:(NSString *)key{ NSData *base64Data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSData *decryptedData = [GTMBase64 decodeData:base64Data]; NSData *data = [decryptedData decryptedAES256DataUsingKey:key error:nil]; NSString* result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return result;}
A classification NSData + CommonCrypto is added here, which is referenced in EncryptionHelper. m and provides AES and DES encryption and decryption algorithms. If you are interested, you can take a closer look.
Github address: https://github.com/ALongWay/AESCrypt-ObjC
Note:
A. In the AES encryption method, the result string is generated and three methods are listed.
B. During the DES encryption process, you only need to replace AES256EncryptedDataUsingKey with the DESEncryptedDataUsingKey method.
C. During the DES decryption process, you only need to replace decryptedAES256DataUsingKey with the decryptedDESDataUsingKey Method
List the output results of the test code:
NSString * message = @ "test various encryption and decryption Methods abc123 + =/"; NSString * key = @ "xyz123 this is the key"; NSString * base64Msg = [EncryptionHelper base64EncodeWithString: message]; NSString * decodeMsg = [EncryptionHelper listener: base64Msg]; NSString * md5Msg = [EncryptionHelper MD5SumWithString: message]; NSString * sha1Msg = [EncryptionHelper SHA1HashWithString: message]; NSString * sha256Msg = [encryption: message]; NSString * aes256Msg = [EncryptionHelper encryption: message usingKey: key]; NSString * encryption = [EncryptionHelper encryption: aes256Msg usingKey: key]; NSString * desMsg = [EncryptionHelper DESEncryptedString: message usingKey: key]; NSString * decryptedDESMsg = [EncryptionHelper decryptedDESString: desMsg usingKey: key];
17:46:43. 505 base [7128: 9029057] message: test various encryption and decryption Methods abc123 + =/; key: xyz123 this is key2016-09-08 17:46:43. 506 base [7128: 9029057] base64Msg: 5rWL6K + V5ZCE56eN5Yqg5a + G6Kej5a + G5pa55rOVYWJjMTIzKz0v2016-09-08 17:46:43. 506 base [7128: 9029057] decodeMsg: test various encryption and decryption Methods abc123 + =/17:46:43. 506 base [7128: 9029057] md5Msg: 55d86b31f8cf9b3007a303664237fc8d2016-09-08 17:46:43. 506 base [7128: 9029057] sha1Msg: 1465c756f0eb64c7cccf175022a84ecd82f8faae2016-09-08 17:46:43. 506 base [7128: 9029057] sha256Msg: timeout 17:46:43. 506 base [7128: 9029057] aes256Msg: seDPZ6RLHavklYxJOP9uMc7QoOZXZ8DASp6YHoe40B + 6QkZ9Xhtn + Ecx5tPqakor2016-09-08 17:46:43. 506 base [7128: 9029057] decryptedAESMsg: test various encryption and decryption Methods abc123 + =/17:46:43. 506 base [7128: 9029057] desMsg: uXGaL4ChwSJt5HoD + gU9T/9L8K4eCVFEg5MyzivJRQxBRhAvfTou2Q = 17:46:43. 507 base [7128: 9029057] decryptedDESMsg: test various encryption and decryption Methods abc123 + =/