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. Netty构建分布式消息队列(AvatarMQ)设计指南之架构篇

    目前业界流行的分布式消息队列系统(或者可以叫做消息中间件)种类繁多,比如,基于Erlang的RabbitMQ.基于Java的ActiveMQ/Apache Kafka.基于C/C++的ZeroMQ等等 ...

  2. 对一致性Hash算法,Java代码实现的深入研究

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

  3. JavaScript面向对象

    理解对象 对象这个词如雷贯耳,同样出名的一句话:XXX语言中一切皆为对象! 对象究竟是什么?什么叫面向对象编程? 对象(object),台湾译作物件,是面向对象(Object Oriented)中的术 ...

  4. direction和unicode-bidi

    在做多语言页面,接触过阿利伯语.希伯来语的同学肯定了解书写方向的重要性,包括我们五四运动前的书写顺序也是从右到左的.css中 unicode-bidi和direction属性决定了HTML或XML文字 ...

  5. Spring的前期配置

    1创建一个java项目,鼠标单击项目右键新建一个名为lib的文件夹 2在lib文件夹中考入Spring需要的配置文件(俗称jar包) 3 按Shift选中这些jar右键添加至构建路径 4选中src目录 ...

  6. 解决 SpringBoot 没有主清单属性

    问题:SpringBoot打包成jar后运行提示没有主清单属性 解决:补全maven中的bulid信息 <plugin> <groupId>org.springframewor ...

  7. Compile FreeCAD on Windows

    Compile FreeCAD on Windows eryar@163.com 1.Introduction FreeCAD是一个参数化的三维造型软件,主要用于任意大小的实际模型的设计.参数化的建模 ...

  8. ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出

    系列目录 昨天文章太过仓促没有补充导出的示例源码,在者当时弄到到很晚没时间做出导出功能,对阅读理解造成影响,现补充一份示例源码,顺便补充导出的功能说明,望理解 示例代码下载   https://yun ...

  9. [.NET] WebApi 生成帮助文档及顺便自动创建简单的测试工具

    ==========最终的效果图========== ==========下面开始干活:生成帮助文档========== 一.创建 WebApi 项目 二.找到 HelpPageConfig.cs 并 ...

  10. 当master down掉后,pt-heartbeat不断重试会导致内存缓慢增长

    最近同事反映,在使用pt-heartbeat监控主从复制延迟的过程中,如果master down掉了,则pt-heartbeat则会连接失败,但会不断重试. 重试本无可厚非,毕竟从使用者的角度来说,希 ...