ios常见加密算法

1、MD5
//因为是使用category,所以木有参数传入啦

-(NSString *) stringFromMD5 {
    if(self == nil || [self length] == 0) {
        return nil;
    }
    const char *value = [self UTF8String];
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(value, strlen(value), outputBuffer);
    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
        [outputString appendFormat:@"%02x",outputBuffer[count]];
    }
    return [outputString autorelease];
}

2、Base64

+ (NSString *) base64EncodeData: (NSData *) objData {
const unsigned char * objRawData = [objData bytes];
char * objPointer;
char * strResult;

// Get the Raw Data length and ensure we actually have data
int intLength = [objData length];
if (intLength == 0) return nil;

// Setup the String-based Result placeholder and pointer within that placeholder
strResult = (char *)calloc(((intLength + 2) / 3) * 4, sizeof(char));
objPointer = strResult;

// Iterate through everything
while (intLength > 2) { // keep going until we have less than 24 bits
*objPointer++ = _base64EncodingTable[objRawData[0] >> 2];
*objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
*objPointer++ = _base64EncodingTable[((objRawData[1] & 0x0f) << 2) + (objRawData[2] >> 6)];
*objPointer++ = _base64EncodingTable[objRawData[2] & 0x3f];

// we just handled 3 octets (24 bits) of data
objRawData += 3;
intLength -= 3;
}

// now deal with the tail end of things
if (intLength != 0) {
*objPointer++ = _base64EncodingTable[objRawData[0] >> 2];
if (intLength > 1) {
*objPointer++ = _base64EncodingTable[((objRawData[0] & 0x03) << 4) + (objRawData[1] >> 4)];
*objPointer++ = _base64EncodingTable[(objRawData[1] & 0x0f) << 2];
*objPointer++ = ‘=‘;
} else {
*objPointer++ = _base64EncodingTable[(objRawData[0] & 0x03) << 4];
*objPointer++ = ‘=‘;
*objPointer++ = ‘=‘;
}
}

// Terminate the string-based result
*objPointer = ‘\0‘;

    NSString *rstStr = [NSString stringWithCString:strResult encoding:NSASCIIStringEncoding];
    free(objPointer);
    return rstStr;
}

3、AES
-(NSData*) EncryptAES: (NSString *) key {
    char keyPtr[kCCKeySizeAES256+1];
    bzero(keyPtr, sizeof(keyPtr));

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          keyPtr, kCCBlockSizeAES128,
                                          NULL,
                                          [self bytes], dataLength,
                                          buffer, bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer);
    return nil;
}

4、RSA

- (NSData *) encryptWithData:(NSData *)content {
    size_t plainLen = [content length];
    if (plainLen > maxPlainLen) {
        NSLog(@"content(%ld) is too long, must < %ld", plainLen, maxPlainLen);
        return nil;
    }

    void *plain = malloc(plainLen);
    [content getBytes:plain
               length:plainLen];

    size_t cipherLen = 128; // currently RSA key length is set to 128 bytes
    void *cipher = malloc(cipherLen);

    OSStatus returnCode = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plain,
                                        plainLen, cipher, &cipherLen);

    NSData *result = nil;
    if (returnCode != 0) {
        NSLog(@"SecKeyEncrypt fail. Error Code: %ld", returnCode);
    }
    else {
        result = [NSData dataWithBytes:cipher
                                length:cipherLen];
    }

    free(plain);
    free(cipher);

    return result;
}

好了,这遍文章就到了,如果想了解更多,可以关注新浪博客:blog.sina.com/zhongxiaoqiu
时间: 2025-01-14 18:52:20

ios常见加密算法的相关文章

常见加密算法概述

1.1 对称加密对称加密就是加密和解密使用同一个密钥,通常称之为“Session Key ”.这种加密技术目前被广泛采用,如美国政府所采用的DES加密标准就是一种典型的“对称式”加密法,它的“Session Key”长度为56 Bits.对称加密算法的优点在于加解密的高速度和使用长密钥时的难破解性.假设两个用户需要使用对称加密方法加密然后交换数据,则用户最少需要2个密钥并交换使用.如果企业内用户有n个,则整个企业共需要n×(n-1) 个密钥,密钥的生成和分发将成为企业信息部门的恶梦.对称加密算法

iOS常见crash问题及crash日志分析

第一.iOS常见crash问题 1.插入空元素 对于NSMutableArray.NSMutableDictionary.NSMutableSet等可变的对象,插入空元素时crash 正确做法是加判断,sample code如下所示: NSDictionary *dic; NSMutableArray *arr=[NSMutableArray new]; if (dic) { [arr addObject:dic]; } NSLog(@"%@",arr); 加非空的判断条件: if(d

实现ios常见菜单效果的思路

目前见过的实现边侧菜单的效果,比较流行的有以下三种:(效果图) 1.菜单栏覆盖在部分主视图上 附上实现该效果的一个不错的源码地址: http://code4app.com/ios/RNFrostedSidebar/524399706803fa3c33000001 (1)最开始要实现这个效果,我想最简单的方式就是:添加UIView,加上一个self.view大小的子视图,菜单列表以外的区域设为透明灰色.后来发现,如果当前的控制器有显示导航栏或者工具栏,这个子视图就无法遮盖住导航栏或者工具栏上面的按

IOS常见错误分析解决(一直更新) 你值得收藏-综合贴

-来自收藏总结 综合了好多的常见错误 1:clang failed with exit code 254 一:检测代码中 是否 有 NSLog 打印了 返回 void 的值. 2:Verify exit code of build task with internal identifier 'CopyPNGFile 123.png' 一:将出错的png,用PhotoShop重新转换一次,  如果PhotoShop打不开,改后缀为Jpg 试试. 转换时,请使用 :存储为Web或设备所使用的格式格式

iOS 常见知识点(三):Lock

iOS 常见知识点(一):Runtime iOS 常见知识点(二):RunLoop 锁是最常用的同步工具.一段代码段在同一个时间只能允许被有限个线程访问,比如一个线程 A 进入需要保护代码之前添加简单的互斥锁,另一个线程 B 就无法访问,只有等待前一个线程 A 执行完被保护的代码后解锁,B 线程才能访问被保护代码. iOS 中的八大锁 NSLock @protocol NSLocking - (void)lock; - (void)unlock; @end @interface NSLock :

转: 常见加密算法分,用途,原理以及比较

常见加密算法分,用途,原理以及比较 标签: 算法加密解密encryption破解algorithm 2012-05-11 13:28 3533人阅读 评论(0) 收藏 举报  分类: 数据结构与算法(3)    密码学简介 据记载,公元前400年,古希腊人发明了置换密码.1881年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用“恩尼格玛”密码机,密码学在战争中起着非常重要的作用. 随着信息化和数字化社会的发展,人们对信息安全和保密的重要性认识不断提高,于是在1997年,美国国

ios常见错误之 Failed to instantiate the default view controller for UIMainStoryboardFile &#39;Main&#39; - perhaps the designated entry point is not set?

Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set? 这是因为给你的StoryBoard没有设置默认显示的controller, 解决方法: from:http://my.oschina.net/u/936286/blog/316565 ios常见错误之 Failed to instanti

IOS常见的三种回调方法介绍

认识下三种IOS常见的回调模式. 代理模式作为IOS中最常见的通讯模式,代理几乎无处不在. 这里有一个数组,我们首先通过代理的方式将数组传递到其他方法中去. 设置协议及方法 @protocol CallBackDelegate; @interface ViewController : UIViewController @property (weak, nonatomic) id<CallBackDelegate> delegate; @end @protocol CallBackDelegat

iOS常见算法以及应用

算法比较 关键词 二分 递归 分治 回溯 冒泡排序 思想:两次循环,外层进行循环次数的控制,内层循环,进行数据之间的比较,大的数据上浮(下沉) 12345678910111213141516171819202122232425262728293031323334353637383940 #pragma mark - Objective-C//冒泡排序- (void)bubbleSort:(id)array{ if (!([array isKindOfClass:[NSArray class]]