1. Base64 编码解码
编码:结果是string
解码:结果是byte
String Tag="denganzhi1";
@RequiresApi(api = Build.VERSION_CODES.O)
public void base64Show(View view){
String encode = null;
try {
// 编码, 编码的结果是字符串
encode = java.util.Base64.getEncoder().encodeToString("123456".getBytes("UTF-8"));
Log.e(Tag, "encode:"+ encode);
// 解码, 解码的结果是 二进制
byte[] decode = java.util.Base64.getDecoder().decode(encode);
Log.e(Tag,"解码:"+ new String(decode, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
2. RSA : 非对称加密:
原理: 大质数相乘,对结果进行因式分解困难
使用Openssl生成公钥与私钥
Openssl 安装:https://blog.csdn.net/sunhuansheng/article/details/82218678
OpenSSL: 内部集成了完整算法 rsa/aes
openssl : 进入 终端
genrsa -out rsa_private_key.pem 1024 : 长度1024,生成私钥,私钥长度长于公钥
rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem : 基于私钥生成公钥
*重新根据pkcs8的秘钥创建私钥,openssl默认是pkcs1,java使用的是pkcs8转化一下,否则无法解秘
* pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform pem -nocrypt -out rsa_private_key_pkcs8.pem
*
* 根据pkcs8 根据私钥 重新 生成公钥
* rsa -in rsa_private_key_pkcs8.pem -pubout -out rsa_public_key_pkcs8.pem
-----------------------------------------------------------------------------
Java Api:
Java 通过KeyFactory 获取各种 加密算法 实例 RSA|AES
网络传输公钥、私钥使用:
公钥: 发出去
私钥:自己留着
只有自己才可以解密
TCP三次握手秘钥传输:
A -> B 发送公钥
B -> A 返回私钥
A -> B Ack收到
RSA加密核心类:
//手动 生成 RSA 的 秘钥
// 1. 初始哈参数变量
//2. 获取RSA 的 Cipher
//3. 执行加密
package mk.denganzhi.com.keymodule;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/**
* Created by denganzhi on 2020/4/3.
*/
public class RSA {
/****
* 加密函数,传入明文,公钥返回 密文
*/
public static byte[] encryptToByte(int data, String publicKey) {
String message= String.valueOf(data);
// base64 编码 返回 byte
byte[] decoded= base64Decode(message);
byte[] result= null;
try {
// 获取RSA 算法 X509EncodedKeySpec 是协议 类型
RSAPublicKey pubKey= (RSAPublicKey) KeyFactory.getInstance("RSA")
.generatePublic(new X509EncodedKeySpec(decoded));
// 这里指定具体RSA算法,这里的是 PKSC1Padding
Cipher cipper=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipper.init(Cipher.ENCRYPT_MODE,pubKey);
result= cipper.doFinal(message.getBytes("utf-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}catch (InvalidKeySpecException e){
e.printStackTrace();
}catch (InvalidKeyException e){
e.printStackTrace();
}
return result;
}
/****
* 加密函数,传入明文,公钥返回 密文, 返回base64字符串
*/
public static String encryptToBase64(int data, String publicKey){
String message= String.valueOf(data);
// base64 编码 返回 byte
byte[] decoded= base64Decode(publicKey);
byte[] result = new byte[]{0};
try {
// 获取RSA 算法 X509EncodedKeySpec 是协议 类型
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
.generatePublic(new X509EncodedKeySpec(decoded));
// 这里指定具体RSA算法,这里的是 PKSC1Padding
Cipher cipper=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipper.init(Cipher.ENCRYPT_MODE,pubKey);
result= cipper.doFinal(message.getBytes("utf-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}catch (InvalidKeyException e) {
e.printStackTrace();
}finally {
return base64Encord(result);
}
}
/**
* 解密
* 密文+ 私钥
*/
public static String decypt(String encryted,String key){
byte [] decode = base64Decode(key);
byte [] content = base64Decode(encryted);
byte[] result= new byte[]{0};
RSAPrivateKey priKey= null;
try {
priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").
generatePrivate(new PKCS8EncodedKeySpec(decode));
// 这里指定具体RSA算法,这里的是 PKSC1Padding
Cipher cipper = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipper.init(Cipher.DECRYPT_MODE,priKey);
result= cipper.doFinal(content);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} 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();
}
return new String(result);
}
// base64 编码
public static String base64Encord(byte[] data){
String encord = null;
try{
encord= android.util.Base64.encodeToString(data,android.util.Base64.NO_WRAP);
}catch (Exception e){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
encord = Base64.getEncoder().encodeToString(data);
}
}
return encord;
}
// base64的解码
public static byte[] base64Decode(String data){
byte[] decode = null;
try{
// Java 提供了Base64 (不太好用)
// Android 中也提供了 base64( 简单好用)
// android.util.Base64.NO_WRAP 太长了 是否 换行 , 不换行
decode= android.util.Base64.decode(data, android.util.Base64.NO_WRAP);
}catch (Exception e){
// Adroid 中没有 ,用 Java 中的
// 在 服务端运行 需要
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
decode = Base64.getMimeDecoder().decode(data);
}
}
return decode;
}
}
加密、解密功能调用:
/***
*重新根据pkcs8的秘钥创建私钥
* pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform pem -nocrypt -out rsa_private_key_pkcs8.pem
*
* 根据pkcs8 根据私钥 重新 生成公钥
* rsa -in rsa_private_key_pkcs8.pem -pubout -out rsa_public_key_pkcs8.pem
* @param view
*/
public void rsaShow(View view){
// openssl 生成的是 base64编码的 字符串
String pubKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZFFN1SzejUOylCihG4tzbOOJJ\n" +
"vzyQvj8d+eWAm5D1LdQZcKGIa7eb30Ue/ULvXIiVVTZdFXolejWmZVUN88oONsF3\n" +
"cgWa9h1hRioQMfKypfSo8KwjhxAUJjtLBHv+q+bSX8lG2QEMTChTzA6S7BnNaRsv\n" +
"rfpD7ow29pl3oGR3awIDAQAB";
String priKey="MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANkUU3VLN6NQ7KUK\n" +
"KEbi3Ns44km/PJC+Px355YCbkPUt1BlwoYhrt5vfRR79Qu9ciJVVNl0VeiV6NaZl\n" +
"VQ3zyg42wXdyBZr2HWFGKhAx8rKl9KjwrCOHEBQmO0sEe/6r5tJfyUbZAQxMKFPM\n" +
"DpLsGc1pGy+t+kPujDb2mXegZHdrAgMBAAECgYEAkWTqzTn25v0RsyzNc0UJtg8w\n" +
"iUJ8F1pFPhedXvlTKtRqJivpFU1Iai8txabNyTJKDPNI+SmkaSukeXGPeUva/VOP\n" +
"GvIsCgp6I9dJWsRtVTRV2t4Ydc85f8hJojQtYdOtpTOQwN5C5MV3wy4x3ZvCLzaf\n" +
"P53n7zqlF17LZ+JdxHkCQQD9pOAUEhe/rJy/gsBVydzxZQxjbILMZt8MrEFkDe0N\n" +
"GLypp67oUe8P4QHT0d/hMR0E+DhofmIHNbP03LCqfgR3AkEA2xiBgDcDFBOjjH8G\n" +
"A+YbJmo1hytqEpJ9yVpw3J0iQ8lZj5TQ1opFy6zV9MdFbjUc7vkMmTfNtaWGYl1s\n" +
"muvlrQJBAI2rkpEDTgp8Ig0BveXs2yBlfVbnG+OJLdpJODjnYlknUPE9vH78jJeE\n" +
"6N1j4zVHllrZPcB3ns9CGqKQxYhk3J8CQBUI4UYCAhWnXQhcGaO+L9UcVU9I58Io\n" +
"tlLij/teSq/fYO3cB8DRPao0ScZaa1wNVV3MI12CcgmCfshq18Z353ECQQCuHhE9\n" +
"HnVU9nj+v6Hyj/4gz9EZtdnCOF5N5qMiayDmTC/sWup9ffEQTyBRMbg06kuvbIUX\n" +
"dnMSxAn7z32a2Mem";
int context= 123;
// 加密
String enctryed = RSA.encryptToBase64(context,pubKey);
// 解密
String result= RSA.decypt(enctryed,priKey);
Log.e(Tag,"RSA解密:result: "+result);
}
3. AES对称加密:
需要使用代码自己手动生成秘钥
//手动 生成 AES的 秘钥
// 1. 初始哈参数变量
//2. 获取AES的 Cipher
//3. 执行加密
AES 功能:
package mk.denganzhi.com.keymodule;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class AES {
private SecretKey mKey;
public AES(){
try {
// 获取AES 算法
KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
//
SecureRandom secureRandom=new SecureRandom();
secureRandom.setSeed(System.currentTimeMillis());
keyGenerator.init(128,secureRandom);
mKey= keyGenerator.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
// 加密
public byte[] entrypt(String context){
byte result[]=new byte[]{-1};
if(mKey==null){
return result;
}
try {
Cipher cipher=Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,mKey);
result= cipher.doFinal(context.getBytes());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}finally {
return result;
}
}
public byte[] decrypt(byte[] conent){
byte result[]=new byte[]{-1};
if(mKey==null){
return result;
}
Cipher cipher= null;
try {
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE,mKey);
result = cipher.doFinal(conent);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}finally {
return result;
}
}
}
函数调用:
public void aesClick(View view) {
AES aes=new AES();
String context="thi is age";
byte[] entrypted= aes.entrypt(context);
// Log.e(Tag,"加密:"+new String(entrypted));
byte[] dectrypted= aes.decrypt(entrypted);
Log.e(Tag,"解密:"+ new String(dectrypted));
}