博客> 加密的小demo
加密的小demo
2019-12-14 18:09 评论:0 阅读:632 逐小白
MD5 加密

// 32位加密

  • (NSString )MD5_32WithMobileRegister:(NSString )myString { NSData *utf16data = [myString dataUsingEncoding:NSUTF8StringEncoding]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5([utf16data bytes], [utf16data length], result);

    NSMutableString *resultStr = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];

    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [resultStr appendFormat:@"X", result[i]]; } return [resultStr lowercaseString]; } // 32位加密

  • (NSString )MD5_32:(NSString )myString { NSData *utf16data = [myString dataUsingEncoding:NSUTF16LittleEndianStringEncoding]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5([utf16data bytes], [utf16data length], result);

    NSMutableString *resultStr = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];

    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [resultStr appendFormat:@"X", result[i]]; } return [resultStr lowercaseString]; }

// 16位加密

  • (NSString )MD5_16:(NSString )myString { const char *cStr = [myString UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(cStr, strlen(cStr), result);

    NSMutableString *resultStr = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];

    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [resultStr appendFormat:@"X", result[i]]; }

    // 从32位中截取出中间的16位加密字符串 NSString *result_16_Str = [resultStr substringWithRange:NSMakeRange(8,16)];

    return [result_16_Str lowercaseString]; }

收藏
0
sina weixin mail 回到顶部