zoukankan      html  css  js  c++  java
  • ios Base62加密解密

    最近的项目用到Base62的加密解谜方法,本来还打算去网上找找呢,结果没有objective-c版本的,就自己写了个, 因为Base62加密数值用的,所以作为NSString的Category写的。下面放上源码: 头文件: // // NSString+Base62.h // GameHouse // // Created by wanyakun on 13-3-19. // Copyright (c) 2013年 the9. All rights reserved. // #import @interface NSString (Base62) +(NSString *)base62Encode:(int)number; +(int) base62Decode:(NSString *)string; @end 实现文件: // // NSString+Base62.m // GameHouse // // Created by wanyakun on 13-3-19. // Copyright (c) 2013年 the9. All rights reserved. // #import "NSString+Base62.h" @implementation NSString (Base62) +(NSString *)base62Encode:(int)number { NSArray *array = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"]; NSString *base62 = [NSString stringWithFormat:@""]; do { base62 = [[array objectAtIndex:number%62] stringByAppendingString:base62]; number /= 62; } while (number >= 1); return base62; } +(int)base62Decode:(NSString *)string { if (string == nil || [string isEqualToString:@""]) { return 0; } NSArray *array = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"]; int base10 = 0; int length = [string length]; for (int i = 0; i < length; i++) { int number = [array indexOfObject:[string substringWithRange:NSMakeRange(i, 1)]]; base10 += number * powf(62, length-i-1); } return base10; } @end 再把两个文件的压缩包放出来,可以直接使用: NSString+Base62
  • 相关阅读:
    Use the Microsoft C++ toolset from the command line
    libeay32.dll 和 ssleay32.dll 在1.1.0之后没有了
    libeay32.dll 1.0.2j crash
    VHD VHDX 区别
    char * 转 wchar *
    C++ 0xC0000094: Integer division by zero
    爬取人人网
    伪造请求头向url传递参数爬取百度默认翻译
    爬取百度贴吧
    urllib爬取实例
  • 原文地址:https://www.cnblogs.com/wanyakun/p/3403333.html
Copyright © 2011-2022 走看看