Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sxd.yfl;
- import android.util.Base64;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * Created by wujie on 2016/5/7.
- */
- public class ISecurity {
- public static final String APP_ID = Global.APP_ID;
- public static final String APP_KEY = Global.APP_KEY;
- public static String encrypt(String clear, String key, String iv) {
- try {
- byte[] encrypted = encrypt(clear.getBytes(), md5(key), md5(iv));
- return Base64.encodeToString(encrypted, Base64.DEFAULT);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- public static String decrypt(String encrypted, String key, String iv) {
- try {
- byte[] decrypted = decrypt(Base64.decode(encrypted, Base64.DEFAULT), md5(key), md5(iv));
- return new String(decrypted).trim();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- private static byte[] encrypt(byte[] clear, byte[] key, byte[] iv) throws Exception {
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
- IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
- cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParamSpec);
- return cipher.doFinal(clear);
- }
- private static byte[] decrypt(byte[] encrypted, byte[] key, byte[] iv) throws Exception {
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
- IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
- cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParamSpec);
- return cipher.doFinal(encrypted);
- }
- public static byte[] md5(String str) {
- try {
- return md5(str.getBytes("utf-8"));
- } catch (UnsupportedEncodingException e) {
- return md5(str.getBytes());
- }
- }
- public static byte[] md5(byte[] bytes) {
- try {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- md5.update(bytes);
- return md5.digest();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return bytes;
- }
- public static String MD5(String str) {
- try {
- return MD5(str.getBytes("utf-8"));
- } catch (UnsupportedEncodingException e) {
- return MD5(str.getBytes());
- }
- }
- private static String MD5(byte[] bytes) {
- try {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- md5.update(bytes);
- byte[] digested = md5.digest();
- char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'A', 'B', 'C', 'D', 'E', 'F'};
- // byte tmp[] = bytes;// MD5 的计算结果是一个 128 位的长整数,
- // 用字节表示就是 16 个字节
- char str[] = new char[16 * 2];// 每个字节用 16 进制表示的话,使用两个字符, 所以表示成 16
- // 进制需要 32 个字符
- int k = 0;// 表示转换结果中对应的字符位置
- for (int i = 0; i < 16; i++) {// 从第一个字节开始,对 MD5 的每一个字节// 转换成 16
- // 进制字符的转换
- byte byte0 = digested[i];// 取第 i 个字节
- str[k++] = hexDigits[byte0 >>> 4 & 0xf];// 取字节中高 4 位的数字转换,// >>>
- // 为逻辑右移,将符号位一起右移
- str[k++] = hexDigits[byte0 & 0xf];// 取字节中低 4 位的数字转换
- }
- return new String(str);// 换后的结果转换为字符串
- } catch (Exception e) {
- e.printStackTrace();
- }
- return new String(bytes);
- }
- public static String sha1(byte[] bytes) {
- try {
- MessageDigest md5 = MessageDigest.getInstance("SHA-1");
- md5.update(bytes);
- byte[] digested = md5.digest();
- return hexDigits(digested, digested.length);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return new String(bytes);
- }
- private static String hexDigits(byte[] bytes, int length) {
- char hexChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'a', 'b', 'c', 'd', 'e', 'f'};
- char str[] = new char[length * 2];
- int k = 0;
- for (int i = 0; i < length; i++) {
- byte byte0 = bytes[i];
- str[k++] = hexChars[byte0 >>> 4 & 0xf];
- str[k++] = hexChars[byte0 & 0xf];
- }
- return new String(str);
- }
- }
Add Comment
Please, Sign In to add comment