import java.security.Provider;
import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; /**
* Created by free宇 2017/7/27. 11:44
* author:free宇
* describe: AES加密算法
*/ public class AesUtils {
/**
* @param
* @return AES加密算法加密
* @throws Exception
*/
public static String encrypt(String seed, String key)
throws Exception {
byte[] rawKey = getRawKey(key.getBytes());
byte[] result = encrypt(seed.getBytes("utf-8"), rawKey);
return toHex(result);
} public static byte[] encryptByte(String seed, String key)
throws Exception {
byte[] rawKey = getRawKey(key.getBytes());
return encrypt(seed.getBytes("utf-8"), rawKey);
} public static String decryptString(byte[] byteData, byte[] byteKey) throws Exception {
byte[] rawKey = getRawKey(byteKey);
byte[] result = decrypt(byteData, rawKey);
return new String(result, "UTF8");
} /***
* AES加密算法加密
* @param byteData 数据
* @param byteKey key
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] byteData, byte[] byteKey) throws Exception {
return Ase(byteData, byteKey, Cipher.ENCRYPT_MODE);
} /***
* AES加密算法解密
* @param byteData 数据
* @param byteKey key
* @return
* @throws Exception
*/
private static byte[] decrypt(byte[] byteData, byte[] byteKey) throws Exception {
return Ase(byteData, byteKey, Cipher.DECRYPT_MODE);
} /***
*
* @param byteData
* @param byteKey
* @param opmode
* @return
* @throws Exception
*/
private static byte[] Ase(byte[] byteData, byte[] byteKey, int opmode) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec skeySpec = new SecretKeySpec(byteKey, "AES");
cipher.init(opmode, skeySpec);
byte[] decrypted = cipher.doFinal(byteData);
return decrypted;
} private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = null;
int sdk_version = android.os.Build.VERSION.SDK_INT;
if (sdk_version > 23) { // Android 6.0 以上
sr = SecureRandom.getInstance("SHA1PRNG", new CryptoProvider());
} else if (sdk_version >= 17) {
sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
sr = SecureRandom.getInstance("SHA1PRNG");
}
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
} public static class CryptoProvider extends Provider {
/**
* Creates a Provider and puts parameters
*/
public CryptoProvider() {
super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)");
put("SecureRandom.SHA1PRNG",
"org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
}
} private static String toHex(byte[] buf) {
final String HEX = "0123456789ABCDEF";
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
result.append(HEX.charAt((buf[i] >> 4) & 0x0f)).append(
HEX.charAt(buf[i] & 0x0f));
}
return result.toString();
} private static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream; public class Base64 {
private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
.toCharArray(); public static String encode(byte[] data) {
int start = 0;
int len = data.length;
StringBuffer buf = new StringBuffer(data.length * 3 / 2); int end = len - 3;
int i = start;
int n = 0; while (i <= end) {
int d = ((((int) data[i]) & 0x0ff) << 16)
| ((((int) data[i + 1]) & 0x0ff) << 8)
| (((int) data[i + 2]) & 0x0ff); buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append(legalChars[(d >> 6) & 63]);
buf.append(legalChars[d & 63]); i += 3; if (n++ >= 14) {
n = 0;
buf.append(" ");
}
} if (i == start + len - 2) {
int d = ((((int) data[i]) & 0x0ff) << 16)
| ((((int) data[i + 1]) & 255) << 8); buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append(legalChars[(d >> 6) & 63]);
buf.append("=");
} else if (i == start + len - 1) {
int d = (((int) data[i]) & 0x0ff) << 16; buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append("==");
} return buf.toString();
} private static int decode(char c) {
if (c >= 'A' && c <= 'Z')
return ((int) c) - 65;
else if (c >= 'a' && c <= 'z')
return ((int) c) - 97 + 26;
else if (c >= '0' && c <= '9')
return ((int) c) - 48 + 26 + 26;
else
switch (c) {
case '+':
return 62;
case '/':
return 63;
case '=':
return 0;
default:
throw new RuntimeException("unexpected code: " + c);
}
} /**
* Decodes the given Base64 encoded String to a new byte array. The byte
* array holding the decoded data is returned.
*/ public static byte[] decode(String s) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
decode(s, bos);
} catch (IOException e) {
throw new RuntimeException();
}
byte[] decodedBytes = bos.toByteArray();
try {
bos.close();
bos = null;
} catch (IOException ex) {
System.err.println("Error while decoding BASE64: " + ex.toString());
}
return decodedBytes;
} private static void decode(String s, OutputStream os) throws IOException {
int i = 0; int len = s.length(); while (true) {
while (i < len && s.charAt(i) <= ' ')
i++; if (i == len)
break; int tri = (decode(s.charAt(i)) << 18)
+ (decode(s.charAt(i + 1)) << 12)
+ (decode(s.charAt(i + 2)) << 6)
+ (decode(s.charAt(i + 3))); os.write((tri >> 16) & 255);
if (s.charAt(i + 2) == '=')
break;
os.write((tri >> 8) & 255);
if (s.charAt(i + 3) == '=')
break;
os.write(tri & 255); i += 4;
}
}
}

也可以用android 自带的base64工具类

android.util.Base64.encode(byteMi,android.util.Base64.DEFAULT); //编码
android.util.Base64.decode(miData,android.util.Base64.DEFAULT); //解码

项目要求接口普通接口用base64 重要接口用base64(aes(data,key)) 进行加密

android AES 加密解密的更多相关文章

  1. android -------- AES加密解密算法

    AES加密标准又称为高级加密标准Rijndael加密法,是美国国家标准技术研究所NIST旨在取代DES的21世纪的加密标准.AES的基本要求是,采用对称分组密码体制,密钥长度可以为128.192或25 ...

  2. openssl与cryptoAPI交互AES加密解密

    继上次只有CryptoAPI的加密后,这次要实现openssl的了 动机:利用CryptoAPI制作windows的IE,火狐和chrome加密控件后,这次得加上与android的加密信息交互 先前有 ...

  3. android&php 加密解密

    from://http://blog.csdn.net/hecker385/article/details/6717647 android&php 加密解密 分类: Php Android20 ...

  4. 非对称技术栈实现AES加密解密

    非对称技术栈实现AES加密解密 正如前面的一篇文章所述,https协议的SSL层是实现在传输层之上,应用层之下,也就是说在应用层上看到的请求还是明码的,对于某些场景下要求这些http请求参数是非可读的 ...

  5. C#中使用DES和AES加密解密

    C#中使用DES和AES加密解密 2008-01-12 09:37 using System;using System.Text;using System.Security.Cryptography; ...

  6. C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现[转载]

    原文:http://outofmemory.cn/code-snippet/35524/AES-with-javascript-java-csharp-python-or-php c#里面的AES加密 ...

  7. ruby AES加密解密

    最近和京东合作做一个项目,在接口对接传递参数时,参数需要通过AES加密解密. 本来想到用gem 'aescrypt'处理,但是aescrypt的编码方式用的base64,而京东那边用的是16进制.所以 ...

  8. C#/IOS/Android通用加密解密方法

    原文:C#/IOS/Android通用加密解密方法 公司在做移动端ios/android,服务器提供接口使用的.net,用到加密解密这一块,也在网上找了一些方法,有些是.net加密了android解密 ...

  9. java使用AES加密解密 AES-128-ECB加密

    java使用AES加密解密 AES-128-ECB加密 import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; impo ...

随机推荐

  1. Redis for linux安装配置之—-源码安装

    一‘redis单实例安装配置1.下载redis源码压缩包,并将其上传至服务器/usr/local2.解压redis源码压缩包  # tar -xzvf redis-3.2.12.tar.gz3.进入r ...

  2. Java servlet 实现的简易购物车

    首页 2.购买页 3.购物车页 1. 首页代码 发送一个post请求 <!DOCTYPE html><html lang="en"><head> ...

  3. vuex-Mutation(同步)

    更改 Vuex 的 store 中的状态的唯一方法是提交 mutation.Vuex 中的 mutation 非常类似于事件: 每个 mutation 都有一个字符串的 事件类型 (type) 和 一 ...

  4. SqlServer查找字段中带空值的项

    select * from  lianxia where sex is null

  5. MySQL5.7 GTID学习笔记

    GTID(global transaction identifier)是对于一个已提交事务的全局唯一编号,前一部分是server_uuid,后面一部分是执行事务的唯一标志,通常是自增的. 下表整理了G ...

  6. 18-09-20 关于Xlrd和Xlwt的初步学习

    #一关于利用xlrd 打开Excel 读取数据的简单介绍import xlrd """ #1 xlrd 基础的用法:读取,获取sheet,获取内容,行数,列数def re ...

  7. oracle 11g安装过程

    1,工具:Oracle_win64_11gR2_database(64位oracle 11g数据库).iso安装文件,win10系统 2,右键,解压后,点击setup.exe,系统会检测本机的环境,如 ...

  8. python爬虫出现的状态码

    1.200 --- 一切正常访问 2.301 --- 重定向新的url,永久性的 3.302 --- 重定向到临时url 4.304 --- 请求的资源未更新 5.400 --- 非法请求 6.401 ...

  9. Linux删除软链接

    首先我们先来创建一个文件 #mkdir test_chk #touch test_chk/test.txt #vim test_chk/test.txt (这一步随便在这个test.txt里写点东东即 ...

  10. Oracle(二)在 Mysql 的基础上学习 Oracle

    毕竟我是先学的mysql,对数据库的一切认知都会有一个先入为主的思想在里面,如果不搞清楚其中的异同,我感觉Oracle我是学不会 了,甚至会把它们混淆.那么,不会mysql的没必要往下看了. 下边第一 ...