iOS實現幾種會用到的加密方式
這些加密方法都是封裝好的,只需要.h實現一下呼叫即可
1. MD5加密
傳入需要加密的字串
/**
MD5加密方法
*/
+ (NSString *) md5:(NSString *) input {
const char *cStr = [input UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
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;
}
2.Sha1加密
這個相對來說用的不多,不過還是有的
/**
sha1加密方式
*/
+ (NSString *) sha1:(NSString *)input{
const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.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.Base64
64相對來說就比較常用了
@interface Base64()
+(int)char2Int:(char)c;
@end
@implementation Base64
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//加密
+(NSString *)encode:(NSData *)data{
if (data.length == 0)
return nil;
char *characters = malloc(data.length * 3 / 2);
if (characters == NULL)
return nil;
NSInteger end = data.length - 3;
int index = 0;
int charCount = 0;
int n = 0;
while (index <= end) {
int d = (((int)(((char *)[data bytes])[index]) & 0x0ff) << 16)
| (((int)(((char *)[data bytes])[index + 1]) & 0x0ff) << 8)
| ((int)(((char *)[data bytes])[index + 2]) & 0x0ff);
characters[charCount++] = encodingTable[(d >> 18) & 63];
characters[charCount++] = encodingTable[(d >> 12) & 63];
characters[charCount++] = encodingTable[(d >> 6) & 63];
characters[charCount++] = encodingTable[d & 63];
index += 3;
if(n++ >= 14)
{
n = 0;
characters[charCount++] = ' ';
}
}
if(index == data.length - 2)
{
int d = (((int)(((char *)[data bytes])[index]) & 0x0ff) << 16)
| (((int)(((char *)[data bytes])[index + 1]) & 255) << 8);
characters[charCount++] = encodingTable[(d >> 18) & 63];
characters[charCount++] = encodingTable[(d >> 12) & 63];
characters[charCount++] = encodingTable[(d >> 6) & 63];
characters[charCount++] = '=';
}
else if(index == data.length - 1)
{
int d = ((int)(((char *)[data bytes])[index]) & 0x0ff) << 16;
characters[charCount++] = encodingTable[(d >> 18) & 63];
characters[charCount++] = encodingTable[(d >> 12) & 63];
characters[charCount++] = '=';
characters[charCount++] = '=';
}
NSString * rtnStr = [[NSString alloc] initWithBytesNoCopy:characters length:charCount encoding:NSUTF8StringEncoding freeWhenDone:YES];
return rtnStr;
}
//解密
+(NSData *)decode:(NSString *)data{
if(data == nil || data.length <= 0) {
return nil;
}
NSMutableData *rtnData = [[NSMutableData alloc]init];
NSInteger slen = data.length;
int index = 0;
while (true) {
while (index < slen && [data characterAtIndex:index] <= ' ') {
index++;
}
if (index >= slen || index + 3 >= slen) {
break;
}
int byte = ([self char2Int:[data characterAtIndex:index]] << 18) + ([self char2Int:[data characterAtIndex:index + 1]] << 12) + ([self char2Int:[data characterAtIndex:index + 2]] << 6) + [self char2Int:[data characterAtIndex:index + 3]];
Byte temp1 = (byte >> 16) & 255;
[rtnData appendBytes:&temp1 length:1];
if([data characterAtIndex:index + 2] == '=') {
break;
}
Byte temp2 = (byte >> 8) & 255;
[rtnData appendBytes:&temp2 length:1];
if([data characterAtIndex:index + 3] == '=') {
break;
}
Byte temp3 = byte & 255;
[rtnData appendBytes:&temp3 length:1];
index += 4;
}
return rtnData;
}
+(int)char2Int:(char)c{
if (c >= 'A' && c <= 'Z') {
return c - 65;
} else if (c >= 'a' && c <= 'z') {
return c - 97 + 26;
} else if (c >= '0' && c <= '9') {
return c - 48 + 26 + 26;
} else {
switch(c) {
case '+':
return 62;
case '/':
return 63;
case '=':
return 0;
default:
return -1;
}
}
}
@end
4.DES加密
這個需要傳入一個加密的key
//加密
+(NSString *)decryptUseDES:(NSString *)cipherText key:(NSString *)key{
NSString *plaintext = nil;
NSInteger textLenght = cipherText.length;
// NSData *cipherdata = [Base64 decode:cipherText];
NSData *cipherdata = [GTMBase64 decodeString:cipherText];
// const Byte iv[] = {1,2,3,4,5,6,7,8};
unsigned char buffer[textLenght];//原來為1024後來改為3072,由於有時資料量太大
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding,//| kCCOptionECBMode
[key UTF8String], kCCKeySizeDES,
(Byte *)[[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
[cipherdata bytes], [cipherdata length],
buffer, textLenght,//原來為1024後來改為3072,由於有時資料量太大
&numBytesDecrypted);
if(cryptStatus == kCCSuccess) {
NSData *plaindata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
plaintext = [[NSString alloc]initWithData:plaindata encoding:NSUTF8StringEncoding];
}
return plaintext;
}
//解密
+(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key{
// const Byte iv[] = {1,2,3,4,5,6,7,8};
NSString *ciphertext = nil;
NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger dataLength = [textData length];
unsigned char buffer[3072];//原來為1024後來改為3072,由於有時資料量太大
memset(buffer, 0, sizeof(char));
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding, //| kCCOptionECBMode
[key UTF8String], kCCKeySizeDES,
(Byte *)[[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
[textData bytes], dataLength,
buffer, 3072,//原來為1024後來改為3072,由於有時資料量太大
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
ciphertext = [GTMBase64 encodeBase64Data:data];
}
//
NSMutableString *resultStr = [NSMutableString stringWithString:ciphertext];
if (resultStr.length > 77) {
[resultStr insertString:@"\n" atIndex:76];
}
NSString *returnStr = [NSString stringWithFormat:@"%@\n", resultStr];
return returnStr;
}
DES加密需要匯入GTMBase64支援,自行搜尋下載即可
5.RC4加密
傳入key進行加密解密
//加密
+(NSString *)encrypt_Rc4:(NSString *)string withKey:(NSString *)key{
// NSArray *r_key = [NSArray array];
NSMutableArray *r_sBox = [NSMutableArray array];
r_sBox = [[CiphertextManage frameSBox:key] mutableCopy];
unichar code[string.length];
int i = 0;
int j = 0;
for (int n = 0; n < string.length; n++) {
i = (i + 1) % SBOX_LENGTH;
j = (j + [[r_sBox objectAtIndex:i]integerValue]) % SBOX_LENGTH;
[r_sBox exchangeObjectAtIndex:i withObjectAtIndex:j];
NSInteger index=([r_sBox[i] integerValue]+[r_sBox[j] integerValue]);
NSInteger rand=([r_sBox[(index%SBOX_LENGTH)] integerValue]);
code[n]=(rand ^ (int)[string characterAtIndex:n]);
}
const unichar* buffer;
buffer = code;
return [NSString stringWithCharacters:buffer length:string.length];
}
//解密
+ (NSString*)decrypt_Rc4:(NSString*)string withKey:(NSString*)key{
return [CiphertextManage encrypt_Rc4:string withKey:key];
}
+(NSArray *)frameSBox:(NSString *)keyValue{
NSMutableArray *sBox = [[NSMutableArray alloc] initWithCapacity:SBOX_LENGTH];
int j = 0;
for (int i = 0; i < SBOX_LENGTH; i++) {
[sBox addObject:[NSNumber numberWithInteger:i]];
}
for (int i = 0; i < SBOX_LENGTH; i++) {
j = (j + [sBox[i] integerValue] + [keyValue characterAtIndex:(i % keyValue.length)]) % SBOX_LENGTH;
[sBox exchangeObjectAtIndex:i withObjectAtIndex:j];
}
return [NSArray arrayWithArray:sBox];
}
以上就是可能會遇到的幾種加密方式,如有問題歡迎諮詢。
相關文章
- Java實際工作裡用到的幾種加密方式Java加密
- iOS 幾種加密方法iOS加密
- 實現 JavaScript 沙箱的幾種方式JavaScript
- Nagios 快速實現資料視覺化的幾種方式iOS視覺化
- AOP 有幾種實現方式?
- iOS常見的幾種加密方法iOS加密
- 實現登入態的幾種方式
- 互動投影的幾種實現方式
- 分散式鎖的幾種實現方式分散式
- js實現繼承的幾種方式JS繼承
- 分散式鎖的幾種實現方式~分散式
- 淺析malloc()的幾種實現方式
- java幾種代理模式的實現方式Java模式
- java md5加密的幾種方式Java加密
- Spring 例項化方式有幾種?為什麼會用到 Cglib?SpringCGLib
- css實現水平垂直居中的幾種方式CSS
- 分散式事務的幾種實現方式分散式
- Android 截圖實現的幾種方式Android
- javascript實現的繼承的幾種常用方式JavaScript繼承
- 幾種實現延時任務的方式(一)
- 三欄式佈局的幾種實現方式
- 實現繼承的幾種方式及工作原理繼承
- java定時任務實現的幾種方式Java
- 【開發經驗】幾種常見的加密方式加密
- css3實現動畫有幾種方式?CSSS3動畫
- 淺談iOS常用的幾種傳值方式iOS
- 小結:二叉樹的幾種實現方式二叉樹
- web前端換行程式碼的幾種實現方式!Web前端行程
- Java建立多執行緒的幾種方式實現Java執行緒
- js實現繼承的幾種方式和對比JS繼承
- 網路安全中常用的幾種加密方式都有哪些?加密
- css實現圖片自適應容器的幾種方式CSS
- React 提供了幾種方式來實現條件渲染React
- C++中實現回撥機制的幾種方式[轉]C++
- iOS應用資料儲存的幾種常用方式iOS
- C#委託的幾種表現方式C#
- 關於常用到的幾個排序,php的實現排序PHP
- NIO實現的幾種模型模型