安卓客户端与服务端对称加密传输demo
APP端传输一些敏感到后台服务端的时候,我们一般都是需要加密传输的。至于使用对称加密还是非对称加密的话,就得看你对数据有多负责了。
下面是我在开发中用到的一个使用AES对称加密传输的demo(亲测有用),希望这个demo对你有一定参考价值。对称加密非对称加密的原理这里就不废话了,直接上代码
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* 对称加解密工具类
* created by 一头小菜鸡
*/
public class AesUtil {
/*
* 加密
* 1.构造密钥生成器
* 6.返回字符串
*/
private static final String IV_STRING = "16-Bytes--String"; //这里限制了加密规则只能使16个字符长的字符串
public static String AESEncode(String encodeRules,String content) throws InvalidAlgorithmParameterException {
try {
byte[] keyFormat = encodeRules.getBytes("UTF-8");
byte[] contentFormat = content.getBytes("UTF-8");
SecretKeySpec secretKeySpec = new SecretKeySpec(keyFormat,"AES");
byte[] initParam = IV_STRING.getBytes("UTF-8");
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
//初始化密码器
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec,ivParameterSpec);
byte [] byte_AES=cipher.doFinal(contentFormat);
return parseByte2HexStr(byte_AES);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//如果有错就大胆抛空
return null;
}
/*
* 解密
* 过程和加密基本类似
*/
public static String AESDncode(String encodeRules,String content) throws InvalidAlgorithmParameterException {
try {
byte[] keyFormat = encodeRules.getBytes("UTF-8");
byte[] contentFormat = parseHexStr2Byte(content);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyFormat,"AES");
byte[] initParam = IV_STRING.getBytes("UTF-8");
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
//初始化密码器
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec,ivParameterSpec);
byte [] byte_AES=cipher.doFinal(contentFormat);
return new String(byte_AES);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
//如果有错就大胆抛空
return null;
}
/**
* 将二进制转换成16进制
* @method parseByte2HexStr
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]){
StringBuffer sb = new StringBuffer();
for(int i = 0; i < buf.length; i++){
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制转换为二进制
* @method parseHexStr2Byte
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr){
if(hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
public static void main(String[] args) throws InvalidAlgorithmParameterException {
/*
* 加密
*/
String encodeRules="thisisencoderule";
System.out.println("加密规则为:" + encodeRules);
String content = "一头小菜鸡的账号和密码";
System.out.println("加密的内容为:"+content);
String keyContent = AesUtil.AESEncode(encodeRules,content);
System.out.println("加密后的内容为:" + keyContent);
System.out.println("解密后的明文为:"+ AesUtil.AESDncode(encodeRules,keyContent));
}
}
常用MD5加密工具类
package com.justrun.healthwristband.utils;
import java.security.MessageDigest;
/**
* @author kuang ge
* @date 2021年03月31日 11:33
*/
public class Md5EncryptUtil {
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f","h","i","j","k"};
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String encode(String origin) {
return encode(origin, "UTF-8");
}
public static String encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
}
Reference Links
非常感谢下面两篇博文,更感谢其博主大佬
[1] https://www.iteye.com/blog/songjianyong-1571029
[2]
[3]