Android 各种加密
来源:互联网 发布:unix环境高级编程mobi 编辑:程序博客网 时间:2024/05/18 02:42
AES 128 加密
// 加密 AES 128 ECB PKCS7 public static String AES128_Encrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位// if (sKey.length() != 16) {// System.out.print("Key长度不是16位");// return null;// } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");//"算法/模式/补码方式" cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); return new String(Base64.encode(encrypted, Base64.NO_WRAP),"utf-8");// return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。 } // 解密 public static String AES128_Decrypt(String sSrc, String sKey) throws Exception { try { // 判断Key是否正确 if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位// if (sKey.length() != 16) {// System.out.print("Key长度不是16位");// return null;// } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec);// byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密 byte[] encrypted1 = Base64.decode(sSrc, Base64.NO_WRAP); try { byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original,"utf-8"); return originalString; } catch (Exception e) { System.out.println(e.toString()); return null; } } catch (Exception ex) { System.out.println(ex.toString()); return null; } }
MD5加密
public static String MD5Encrypt(String plaintext) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { byte[] btInput = plaintext.getBytes(); // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 mdInst.update(btInput); // 获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { return null; } }
阅读全文
0 0
- Android 各种加密
- Android中的各种加密和MD5摘要
- 各种加密
- 各种加密方案分析
- 各种无线加密方式
- 各种加密工具类
- iOS各种加密总结
- 各种加密方法
- 【android】android中使用commons-codec框架处理各种加密解密
- 窥探各种加密锁核心加密技术
- c# 各种加密解密方法
- iOS-各种加密/解密算法
- iOS开发各种加密方式
- Android Wifi --自动连接指定SSID(各种加密方式均可)
- Android加密
- android 加密
- Android 加密
- android 加密
- Hibernate的注解方式:多对多双向关系基于外键关联
- redis使用之利用jedis实现redis消息队列
- win10下vs2015配置Opencv3.3.1过程详解
- 对于数组指针—指针数组;函数指针—函数指针数组,指向函数指针数组的指针,的理解
- 读懂Hinton的提出的Capsulet架构并用TensorFlow实现
- Android 各种加密
- 热部署原理解析
- 对contentView的点击事件的理解
- 总裁、董事长、CEO有什么区别?
- 深度摄像头测距原理简介
- 15.4 处理事件(监视器)
- jquery 判断array中是否包含指定字符串
- MFC(vs2013)+opencv+海康SDK_显示视频教程
- Nexus Repository Manager OSS 3的好处