iOS的各种加密方法使用简介
来源:互联网 发布:sql express 安装失败 编辑:程序博客网 时间:2024/05/17 03:34
1.base64加密:
[objc] view plain copy
- <span style="font-size:14px;">//
- // base64Test.m
- // base64test
- //
- // Created by apple on 15/6/30.
- // Copyright (c) 2015年 meishidiandian. All rights reserved.
- //
- #import "base64Test.h"
- //引入IOS自带密码库
- #import <CommonCrypto/CommonCryptor.h>
- //空字符串
- #define LocalStr_None @""
- static const char encodingTable[] = "";
- @implementation base64Test
- + (NSString *)base64StringFromText:(NSString *)text
- {
- if (text && ![text isEqualToString:LocalStr_None]) {
- //取项目的bundleIdentifier作为KEY 改动了此处
- //NSString *key = [[NSBundle mainBundle] bundleIdentifier];
- NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
- //IOS 自带DES加密 Begin 改动了此处
- //data = [self DESEncrypt:data WithKey:key];
- //IOS 自带DES加密 End
- return [self base64EncodedStringFrom:data];
- }
- else {
- return LocalStr_None;
- }
- }
- + (NSString *)textFromBase64String:(NSString *)base64
- {
- if (base64 && ![base64 isEqualToString:LocalStr_None]) {
- //取项目的bundleIdentifier作为KEY 改动了此处
- //NSString *key = [[NSBundle mainBundle] bundleIdentifier];
- NSData *data = [self dataWithBase64EncodedString:base64];
- //IOS 自带DES解密 Begin 改动了此处
- //data = [self DESDecrypt:data WithKey:key];
- //IOS 自带DES加密 End
- return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- }
- else {
- return LocalStr_None;
- }
- }
- /******************************************************************************
- 函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
- 函数描述 : 文本数据进行DES加密
- 输入参数 : (NSData *)data
- (NSString *)key
- 输出参数 : N/A
- 返回参数 : (NSData *)
- 备注信息 : 此函数不可用于过长文本
- ******************************************************************************/
- + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
- {
- char keyPtr[kCCKeySizeAES256+1];
- bzero(keyPtr, sizeof(keyPtr));
- [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
- NSUInteger dataLength = [data length];
- size_t bufferSize = dataLength + kCCBlockSizeAES128;
- voidvoid *buffer = malloc(bufferSize);
- size_t numBytesEncrypted = 0;
- CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
- kCCOptionPKCS7Padding | kCCOptionECBMode,
- keyPtr, kCCBlockSizeDES,
- NULL,
- [data bytes], dataLength,
- buffer, bufferSize,
- &numBytesEncrypted);
- if (cryptStatus == kCCSuccess) {
- return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
- }
- free(buffer);
- return nil;
- }
- /******************************************************************************
- 函数名称 : + (NSData *)DESEncrypt:(NSData *)data WithKey:(NSString *)key
- 函数描述 : 文本数据进行DES解密
- 输入参数 : (NSData *)data
- (NSString *)key
- 输出参数 : N/A
- 返回参数 : (NSData *)
- 备注信息 : 此函数不可用于过长文本
- ******************************************************************************/
- + (NSData *)DESDecrypt:(NSData *)data WithKey:(NSString *)key
- {
- char keyPtr[kCCKeySizeAES256+1];
- bzero(keyPtr, sizeof(keyPtr));
- [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
- NSUInteger dataLength = [data length];
- size_t bufferSize = dataLength + kCCBlockSizeAES128;
- voidvoid *buffer = malloc(bufferSize);
- size_t numBytesDecrypted = 0;
- CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
- kCCOptionPKCS7Padding | kCCOptionECBMode,
- keyPtr, kCCBlockSizeDES,
- NULL,
- [data bytes], dataLength,
- buffer, bufferSize,
- &numBytesDecrypted);
- if (cryptStatus == kCCSuccess) {
- return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
- }
- free(buffer);
- return nil;
- }
- /******************************************************************************
- 函数名称 : + (NSData *)dataWithBase64EncodedString:(NSString *)string
- 函数描述 : base64格式字符串转换为文本数据
- 输入参数 : (NSString *)string
- 输出参数 : N/A
- 返回参数 : (NSData *)
- 备注信息 :
- ******************************************************************************/
- + (NSData *)dataWithBase64EncodedString:(NSString *)string
- {
- if (string == nil)
- [NSException raise:NSInvalidArgumentException format:nil];
- if ([string length] == 0)
- return [NSData data];
- static charchar *decodingTable = NULL;
- if (decodingTable == NULL)
- {
- decodingTable = malloc(256);
- if (decodingTable == NULL)
- return nil;
- memset(decodingTable, CHAR_MAX, 256);
- NSUInteger i;
- for (i = 0; i < 64; i++)
- decodingTable[(short)encodingTable[i]] = i;
- }
- const charchar *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
- if (characters == NULL) // Not an ASCII string!
- return nil;
- charchar *bytes = malloc((([string length] + 3) / 4) * 3);
- if (bytes == NULL)
- return nil;
- NSUInteger length = 0;
- NSUInteger i = 0;
- while (YES)
- {
- char buffer[4];
- short bufferLength;
- for (bufferLength = 0; bufferLength < 4; i++)
- {
- if (characters[i] == '\0')
- break;
- if (isspace(characters[i]) || characters[i] == '=')
- continue;
- buffer[bufferLength] = decodingTable[(short)characters[i]];
- if (buffer[bufferLength++] == CHAR_MAX) // Illegal character!
- {
- free(bytes);
- return nil;
- }
- }
- if (bufferLength == 0)
- break;
- if (bufferLength == 1) // At least two characters are needed to produce one byte!
- {
- free(bytes);
- return nil;
- }
- // Decode the characters in the buffer to bytes.
- bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
- if (bufferLength > 2)
- bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
- if (bufferLength > 3)
- bytes[length++] = (buffer[2] << 6) | buffer[3];
- }
- bytes = realloc(bytes, length);
- return [NSData dataWithBytesNoCopy:bytes length:length];
- }
- /******************************************************************************
- 函数名称 : + (NSString *)base64EncodedStringFrom:(NSData *)data
- 函数描述 : 文本数据转换为base64格式字符串
- 输入参数 : (NSData *)data
- 输出参数 : N/A
- 返回参数 : (NSString *)
- 备注信息 :
- ******************************************************************************/
- + (NSString *)base64EncodedStringFrom:(NSData *)data
- {
- if ([data length] == 0)
- return @"";
- charchar *characters = malloc((([data length] + 2) / 3) * 4);
- if (characters == NULL)
- return nil;
- NSUInteger length = 0;
- NSUInteger i = 0;
- while (i < [data length])
- {
- char buffer[3] = {0,0,0};
- short bufferLength = 0;
- while (bufferLength < 3 && i < [data length])
- buffer[bufferLength++] = ((charchar *)[data bytes])[i++];
- // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
- characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
- characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
- if (bufferLength > 1)
- characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
- else characters[length++] = '=';
- if (bufferLength > 2)
- characters[length++] = encodingTable[buffer[2] & 0x3F];
- else characters[length++] = '=';
- }
- return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
- }
- @end</span><span style="font-size: 18px;">
- </span>
2.md5+sha1加密:
[objc] view plain copy
- //md5 encode
- +(NSString *) md5:(NSString *)str
- {
- const charchar *cStr = [str UTF8String];
- unsigned char digest[CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, (unsigned int)strlen(cStr), digest );
- NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
- for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02X", digest[i]];
- return output;
- }
- //sha1 encode
- +(NSString*) sha1:(NSString *)str
- {
- const charchar *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
- NSData *data = [NSData dataWithBytes:cstr length:str.length];
- uint8_t digest[CC_SHA1_DIGEST_LENGTH];
- CC_SHA1(data.bytes, (unsigned int)data.length, digest);
- NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
- for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02x", digest[i]];
- return output;
- }
3.aes256位 加密和解密:
[objc] view plain copy
- #import "NSData+AES256.h"
- #define PASSWORD @"feng5.cn12345678"
- static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- const NSUInteger kAlgorithmKeySize = kCCKeySizeAES256;
- const NSUInteger kPBKDFRounds = 10000; // ~80ms on an iPhone 4
- static Byte saltBuff[] = {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF};
- static Byte ivBuff[] = {0xA,1,0xB,5,4,0xF,7,9,0x17,3,1,6,8,0xC,0xD,91};
- @implementation NSData (AES256)
- + (NSData *)AESKeyForPassword:(NSString *)password{ //Derive a key from a text password/passphrase
- NSMutableData *derivedKey = [NSMutableData dataWithLength:kAlgorithmKeySize];
- NSData *salt = [NSData dataWithBytes:saltBuff length:kCCKeySizeAES128];
- int result = CCKeyDerivationPBKDF(kCCPBKDF2, // algorithm算法
- password.UTF8String, // password密码
- password.length, // passwordLength密码的长度
- salt.bytes, // salt内容
- salt.length, // saltLen长度
- kCCPRFHmacAlgSHA1, // PRF
- kPBKDFRounds, // rounds循环次数
- derivedKey.mutableBytes, // derivedKey
- derivedKey.length); // derivedKeyLen derive:出自
- NSAssert(result == kCCSuccess,
- @"Unable to create AES key for spassword: %d", result);
- return derivedKey;
- }
- /*加密方法*/
- + (NSString *)AES256EncryptWithPlainText:(NSString *)plain {
- NSData *plainText = [plain dataUsingEncoding:NSUTF8StringEncoding];
- // 'key' should be 32 bytes for AES256, will be null-padded otherwise
- char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
- bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
- NSUInteger dataLength = [plainText length];
- size_t bufferSize = dataLength + kCCBlockSizeAES128;
- voidvoid *buffer = malloc(bufferSize);
- bzero(buffer, sizeof(buffer));
- size_t numBytesEncrypted = 0;
- CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding,
- [[NSData AESKeyForPassword:PASSWORD] bytes], kCCKeySizeAES256,
- NULL /* initialization vector (optional) */,
- [plainText bytes], dataLength, /* input */
- buffer, bufferSize, /* output */
- &numBytesEncrypted);
- if (cryptStatus == kCCSuccess) {
- NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
- return [encryptData base64EncodedStringWithOptions:0];
- }
- free(buffer); //free the buffer;
- return nil;
- }
- /*解密方法*/
- + (NSString *)AES256DecryptWithCiphertext:(NSString *)ciphertexts{
- NSData *cipherData = [NSData dataWithBase64EncodedString:ciphertexts];
- // 'key' should be 32 bytes for AES256, will be null-padded otherwise
- char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
- bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
- NSUInteger dataLength = [cipherData length];
- size_t bufferSize = dataLength + kCCBlockSizeAES128;
- voidvoid *buffer = malloc(bufferSize);
- size_t numBytesDecrypted = 0;
- CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
- [[NSData AESKeyForPassword:PASSWORD] bytes], kCCKeySizeAES256,
- NULL ,/* initialization vector (optional) */
- [cipherData bytes], dataLength, /* input */
- buffer, bufferSize, /* output */
- &numBytesDecrypted);
- if (cryptStatus == kCCSuccess) {
- NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
- return [[NSString alloc] initWithData:encryptData encoding:NSUTF8StringEncoding] ;
- }
- free(buffer); //free the buffer;
- return nil;
- }
- + (id)dataWithBase64EncodedString:(NSString *)string;
- {
- if (string == nil)
- [NSException raise:NSInvalidArgumentException format:nil];
- if ([string length] == 0)
- return [NSData data];
- static charchar *decodingTable = NULL;
- if (decodingTable == NULL)
- {
- decodingTable = malloc(256);
- if (decodingTable == NULL)
- return nil;
- memset(decodingTable, CHAR_MAX, 256);
- NSUInteger i;
- for (i = 0; i < 64; i++)
- decodingTable[(short)encodingTable[i]] = i;
- }
- const charchar *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
- if (characters == NULL) // Not an ASCII string!
- return nil;
- charchar *bytes = malloc((([string length] + 3) / 4) * 3);
- if (bytes == NULL)
- return nil;
- NSUInteger length = 0;
- NSUInteger i = 0;
- while (YES)
- {
- char buffer[4];
- short bufferLength;
- for (bufferLength = 0; bufferLength < 4; i++)
- {
- if (characters[i] == '\0')
- break;
- if (isspace(characters[i]) || characters[i] == '=')
- continue;
- buffer[bufferLength] = decodingTable[(short)characters[i]];
- if (buffer[bufferLength++] == CHAR_MAX) // Illegal character!
- {
- free(bytes);
- return nil;
- }
- }
- if (bufferLength == 0)
- break;
- if (bufferLength == 1) // At least two characters are needed to produce one byte!
- {
- free(bytes);
- return nil;
- }
- // Decode the characters in the buffer to bytes.
- bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
- if (bufferLength > 2)
- bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
- if (bufferLength > 3)
- bytes[length++] = (buffer[2] << 6) | buffer[3];
- }
- bytes = realloc(bytes, length);
- return [NSData dataWithBytesNoCopy:bytes length:length];
- }
0 0
- iOS的各种加密方法使用简介
- iOS的各种加密方法使用简介
- iOS 常见信息编码 加密的各种方法
- iOS各种多线程方法的使用总结
- iOS各种加密总结
- RSA算法在iOS 客户端的使用 加密方法代码
- 破解各种加密软件的方法
- 破解各种加密软件的方法
- ios-王云鹤 UIlabel的各种属性与方法的使用
- iOS 常用的加密方法
- iOS 常见的加密方法
- 各种加密方法
- iOS中加密的使用
- Android中popupWindow的各种方法简介
- IOS各种手势的使用
- iOS-各种加密/解密算法
- iOS开发各种加密方式
- 各种方法简介~
- 理解OpenGLSuperbible7-窗口和GLContext
- 15个Android开源框架
- arm-linux-gcc4.4.4编译busybox-1.25.1
- Java中BigDecimal的8种舍入模式
- 求大神指教!!!
- iOS的各种加密方法使用简介
- 标准ie8下,出现滚动条,但是没有滚动块的解决方法
- .Net中Remoting通信机制简单实例
- TextView文本框
- 【转】Laravel 5.1 LTS 速查表
- git应用的几点总结
- 随机梯度下降
- spring 3.2 MVC 406 Not Acceptable 解决方法
- 7.游标