1.MD5算法

不可逆

128位或者64位串,byte数字长度就是16和8,一般表示是使用16进制来表示的话,1个byte转换成2个16bit,分别表示高地位,所以生成的字符串是16位或者是32位的,16位其实是从32位中的中间部分抽出来的。

我们所说的密码多少位,是表示多少bit,转换成byte数组的话,就是除以8,但是如果输出16进制的话就是除以4,因为"1111 1111"="FF";

举例来说:256位 byte数组或者NSData的length就是256/8=32 输出16进制就是32*2=64位

MD5算法 Java 代码:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; public class EncrypMD5 { /**
* TODO(description of this method)
* @param args
* @author 丶贰九 2015-4-29 下午5:33:52
* @since v1.0
*/
public static void main(String[] args) throws NoSuchAlgorithmException{
String msg = "丶贰九";
EncrypMD5 md5 = new EncrypMD5();
byte[] resultBytes = md5.eccrypt(msg);
System.out.println("明文是:" + msg);
System.out.println("密文是:" + EncrypMD5.hexString(resultBytes));
}
//byte字节转换成16进制的字符串MD5Utils.hexString
public static String hexString(byte[] bytes){
StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < bytes.length; i++) {
int val = ((int) bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
} public byte[] eccrypt(String info) throws NoSuchAlgorithmException{
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] srcBytes = info.getBytes();
//使用srcBytes更新摘要
md5.update(srcBytes);
//完成哈希计算,得到result
byte[] resultBytes = md5.digest();
return resultBytes;
}
}

MD5  iOS  Objective-C代码:

//md5加密
- (NSString *)md5:(NSString *)str
{
const char *cStrValue = [str UTF8String];
unsigned char theResult[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStrValue, (unsigned)strlen(cStrValue), theResult);
return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
theResult[], theResult[], theResult[], theResult[],
theResult[], theResult[], theResult[], theResult[],
theResult[], theResult[], theResult[], theResult[],
theResult[], theResult[], theResult[], theResult[]];
}

最后结果是:

明文是:丶贰九
密文是:203ecebd64a8366e58acf19bbb3148dd

2.SHA算法

不可逆

SHA1,SHA256,SHA384,SHA512 分别对应160位,256位import java.security.MessageDigest;

SHA算法 Java 代码:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; public class EncrypSHA { /**
* TODO(description of this method)
*
* @param args
* @author丶贰九 2015-4-29 下午5:12:17
* @since v1.0
*/ //byte字节转换成16进制的字符串MD5Utils.hexString
public byte[] eccrypt(String info, String shaType) throws NoSuchAlgorithmException {
MessageDigest sha = MessageDigest.getInstance(shaType);
byte[] srcBytes = info.getBytes();
// 使用srcBytes更新摘要
sha.update(srcBytes);
// 完成哈希计算,得到result
byte[] resultBytes = sha.digest();
return resultBytes;
} public byte[] eccryptSHA1(String info) throws NoSuchAlgorithmException {
return eccrypt(info, "SHA1");
} public byte[] eccryptSHA256(String info) throws NoSuchAlgorithmException {
return eccrypt(info, "SHA-256");
} public byte[] eccryptSHA384(String info) throws NoSuchAlgorithmException {
return eccrypt(info, "SHA-384");
} public byte[] eccryptSHA512(String info) throws NoSuchAlgorithmException {
return eccrypt(info, "SHA-512");
} public static void main(String[] args) throws NoSuchAlgorithmException {
String msg = "丶贰九";
EncrypSHA sha = new EncrypSHA();
String sha1=sha.hexString(sha.eccryptSHA1(msg));
System.out.println("明文:"+msg);
System.out.println("密文:"+sha1);
} public static String hexString(byte[] bytes){
StringBuffer hexValue = new StringBuffer(); for (int i = ; i < bytes.length; i++) {
int val = ((int) bytes[i]) & 0xff;
if (val < )
hexValue.append("");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
}

SHA 算法 iOS  Objective-C代码:

//sha1加密
- (NSString *)sha1:(NSString *)str
{
const char *cstr = [str UTF8String];
//使用对应的CC_SHA1,CC_SHA256,CC_SHA384,CC_SHA512的长度分别是20,32,48,64
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
//使用对应的CC_SHA256,CC_SHA384,CC_SHA512
CC_SHA1(cstr, strlen(cstr), digest);
NSMutableString* result = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * ];
for(int i = ; i < CC_SHA1_DIGEST_LENGTH; i++) {
[result appendFormat:@"%02x", digest[i]];
}
return result;
}

明文:丶贰九
密文:600c7ca56a913a86a501d683846752113ed65824

整理常用加密 iOS 与 Android 加密 MD5-SHA1的更多相关文章

  1. 超全!整理常用的iOS第三方资源(转)

    超全!整理常用的iOS第三方资源 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地 ...

  2. 整理常用的iOS第三方资源

    一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...

  3. 【转】超全!整理常用的iOS第三方资源 -- 不错

    原文网址:http://www.cocoachina.com/ios/20160121/14988.html 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ ...

  4. 超全!整理常用的iOS第三方资源

    一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...

  5. IOS常见的加密方法,常用的MD5和Base64

    iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...

  6. iOS常用加密方法(aes、md5、base64)

    1.代码 iOS常用加密方法(aes.md5.base64) .AES加密 NSData+AES.h文件 // // NSData-AES.h // Smile // // Created by 周 ...

  7. .NET/android/java/iOS AES通用加密解密(修正安卓)

    移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如.NET和android或者iOS的打交道.为了让数据交互更安全,我们需要对数据进行加密传输.今天研究了一下,把几种语言的加密都 ...

  8. IOS中把字符串加密/IOS中怎么样MD5加密/IOS中NSString分类的实现

    看完过后,你会学到: 1学习IOS开发中的分类实现, 2以及类方法的书写, 3以及字符串的MD5加密/解密. ---------------------------wolfhous---------- ...

  9. wemall app商城源码android开发MD5加密工具类

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发MD5加密工具类主要代码,供 ...

随机推荐

  1. 兼容Mono的下一代云环境Web开发框架ASP.NET vNext

    微软在2014年5月12日的TechEd大会上宣布将会发布下一代ASP.NET框架ASP.NET vNext的预览.此次发布的ASP.NET框架与以前相比发生了根本性的变化,凸显了微软“云优先”(cl ...

  2. Lesson 15 Good news

    Text The secretary told me that Mr. Harmsworth would see me. I felt very nervous when I went into hi ...

  3. 【翻译】用AIML实现的Python人工智能聊天机器人

    前言 用python的AIML包很容易就能写一个人工智能聊天机器人. AIML是Artificial Intelligence Markup Language的简写, 但它只是一个简单的XML. 下面 ...

  4. Python初学者之网络爬虫

    声明:本文内容和涉及到的代码仅限于个人学习,任何人不得作为商业用途. 本文将介绍我最近在学习Python过程中写的一个爬虫程序,将力争做到不需要有任何Python基础的程序员都能读懂.读者也可以先跳到 ...

  5. 【.NET深呼吸】基础:自定义类型转换

    照例,老周在开始吹牛之前,先讲讲小故事,这是朋友提出的建议,老TMD写技术有什么了不起的,人人都会写.后来老周想想,也确实,代码谁不会写,能写到有品位有感悟,就不容易做到.于是,老周接受了该朋友的建议 ...

  6. .NET平台开源项目速览(1)SharpConfig配置文件读写组件

    在.NET平台日常开发中,读取配置文件是一个很常见的需求.以前都是使用System.Configuration.ConfigurationSettings来操作,这个说实话,搞起来比较费劲.不知道大家 ...

  7. url 编码(percentcode 百分号编码)(转载)

    原文地址:http://www.cnblogs.com/leaven/archive/2012/07/12/2588746.html   http://www.imkevinyang.com/2009 ...

  8. 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(3)

    作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...

  9. RHEL6.4 + Oracle 11g DG测试环境快速搭建参考

    环境现状: 两台虚拟主机A和B: 1. A机器已安装ASM存储的Oracle 11g 实例      参考:http://www.cnblogs.com/jyzhao/p/4332410.html 2 ...

  10. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...