1 //需要导入 #import <CommonCrypto/CommonCryptor.h> 2 3 ==============MD5加密============ 4 5 NSString *str = @"加密的内容"; 6 7 //转换成C语言的字符串 8 9 const char *cStr=[str UTF8String]; 10 11 //MD5加密的结果是128位,需要开辟一个16字节的空间 12 13 unsigned char result[16]; 14 15 //调用加密函数 16 17 CC_MD5(cStr, (unsigned int)strlen(cStr), result); 18 19 //通过上面这个方法获取的MD5是一个16个字符的数组,需要转换成32位的MD5值 20 21 NSMutableString *string = [NSMutableString stringWithCapacity:10]; 22 23 for (int i=; i<16; i++) { 24 25 [string appendString:[NSString stringWithFormat:@"%02x",result[i]]]; 26 27 } 28 29 NSLog(@"MD5加密:%@",string); 30 31 32 33 ===============base64加密解密============= 34 35 //ios7 以后提供了base64的转码方式 36 37 //加密 38 39 NSString *pass=@"加密的内容"; 40 41 NSData *passData=[pass dataUsingEncoding:NSUTF8StringEncoding]; 42 43 NSString *result = [passData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; 44 45 NSLog(@"base64加密:%@",result); 46 47 48 49 //解密 50 51 NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:result options:]; 52 53 NSString *decodeStr = [[NSString alloc]initWithData:decodeData encoding:NSUTF8StringEncoding]; 54 55 NSLog(@"base64解密:%@",decodeStr); 56 57 58 59 ================AES加密解密=============== 60 61 //新建一个NSData类,写入一下两个方法 62 63 //加密方法 64 65 - (NSData*)AES256EncryptWithKey:(NSString*)key { 66 67 68 69 char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) 70 71 bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 72 73 74 75 [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 76 77 78 79 NSUInteger dataLength = [self length]; 80 81 82 83 size_t bufferSize = dataLength + kCCBlockSizeAES128; 84 85 void* buffer = malloc(bufferSize); 86 87 88 89 size_t numBytesEncrypted = ; 90 91 CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 92 93 keyPtr, kCCKeySizeAES256, 94 95 NULL /* initialization vector (optional) */, 96 97 [self bytes], dataLength, /* input */ 98 99 buffer, bufferSize, /* output */ 100 101 &numBytesEncrypted); 102 103 104 105 if (cryptStatus == kCCSuccess) { 106 107 return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; 108 109 } 110 111 112 113 free(buffer); 114 115 return nil; 116 117 } 118 119 //解密方法 120 121 - (NSData*)AES256DecryptWithKey:(NSString*)key { 122 123 124 125 char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) 126 127 bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 128 129 130 131 // fetch key data 132 133 [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 134 135 136 137 NSUInteger dataLength = [self length]; 138 139 140 141 size_t bufferSize = dataLength + kCCBlockSizeAES128; 142 143 void* buffer = malloc(bufferSize); 144 145 146 147 size_t numBytesDecrypted = ; 148 149 CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 150 151 keyPtr, kCCKeySizeAES256, 152 153 NULL /* initialization vector (optional) */, 154 155 [self bytes], dataLength, /* input */ 156 157 buffer, bufferSize, /* output */ 158 159 &numBytesDecrypted); 160 161 162 163 if (cryptStatus == kCCSuccess) { 164 165 return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; 166 167 } 168 169 170 171 free(buffer); //free the buffer; 172 173 174 175 在另外的类里面调用上面NSData里面的方法 176 177 //==========AES加密解密============= 178 179 NSString *key = @"mykey";//钥匙 180 181 NSString *secret = @"加密内容";//准备加密的内容 182 183 NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding]; 184 185 NSData *ciper = [plain AES256EncryptWithKey:key]; 186 187 NSLog(@"AES加密%@",ciper); 188 189 //解密 190 191 plain = [ciper AES256DecryptWithKey:key]; 192 193 NSLog(@"AES解密:%@",[[NSString alloc]initWithData:plain encoding:NSUTF8StringEncoding]);