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. WEB UI做TREE

    效果图: 原本的普通搜索帮助,改成上面这样层级的搜索帮助.这里只做了两级. 一,新建一个TREE节点 1.新建tree结构:ZGRTEXT 2.新建树叶节点处理类: 修改超类为CL_BSP_WD_TR ...

  2. volatile关键字的作用

    引言:以前只是看过介绍volatile的文章,对其的理解也只是停留在理论的层面上,由于最近在项目当中用到了关于并发方面的技术,所以下定决心深入研究一下java并发方面的知识.网上关于volatile的 ...

  3. django1

    * <pre>━━━━━━神兽出没━━━━━━ * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓ * ┃ 王 ┃ * ┃   ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┃ * ┃ ┻ ┃ * ┃ ┃ * ...

  4. doxygen

    //commndline: doxygen Doxyfile /**comment /* /** time diff@pre precondition@post endcondition@throw ...

  5. lumion的基本操作,天气系统,景观系统。5.25

    1.打开场景,按住鼠标右键可以选择方向. 2.向前移动:W,向后移动S,亦可以用鼠标滚轮向前或者向后滚.向左A向右D, 3.Q提升视角,E下降视角. 4.鼠标滚轮点下去,进行提升和下降视角.左右移动, ...

  6. Android 音视频深入 十二 FFmpeg视频替换声音(附源码下载)

    项目地址,求starhttps://github.com/979451341/AudioVideoStudyCodeTwo/tree/master/FFmpeg%E7%BB%99%E8%A7%86%E ...

  7. 从本地新建项目到提交到github

    1.我是在windows下操作的,所以需要下载个msysgit,这个是git的windows版本. 2.在项目(假设项目为store)根目录下,鼠标右键,点击git bash here 3.将项目从本 ...

  8. SpringMVC 搭建遇到的坑

    1. Caused by: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 60; cvc-complex-type.2.4.c ...

  9. redis不重启,切换RDB备份到AOF备份

    确保redis版本在2.2以上 [root@pyyuc /data ::]#redis-server -v Redis server v= sha=: malloc=jemalloc- bits= b ...

  10. 基于lnmp环境安装Discuz

    安装环境 Linux:CentOS Linux release 7.5.1804 (Core) nginx:1.14.2 php-fpm:5.4.16 mariadb-server:5.5.60 基本 ...