好友
阅读权限 10
听众
最后登录 1970-1-1
薛-蓝狐
发表于 2019-11-12 11:11
经常使用加密算法:DES、3DES、RC4、AES,RSA等;对称加密:des,3des,aes非对称加密:rsa不可逆加密:md5加密模式:ECB、CBC、CFB、OFB等;填充模式:NoPadding、PKCS1Padding、PKCS5Padding、PKCS7Padding一、几种方式的加密和解密(Base64,RSA,DES,AES)
实现类 MainActivity
测试 setBase64()
$map效果setRSA() private void setRSA() { // 获取到密钥对 KeyPair keyPair = RSAUtil.generateRSAKeyPair(1024);
// 获取公钥和私钥 PublicKey aPublic = keyPair.getPublic(); PrivateKey aPrivate = keyPair.getPrivate(); ; byte[] aPublicEncoded = aPublic.getEncoded(); byte[] aPrivateEncoded = aPrivate.getEncoded();
try {
// 公钥加密 byte[] bytes = RSAUtil.encryptByPublicKey(type, "123".getBytes(), aPublicEncoded); String s1 = Base64Util.encodeWord(bytes.toString()); // String encode = Base64Utils.encode(bytes); // Log.d(TAG, "公钥加密文件: " + encode); Log.d(TAG, s1);
// 私钥解密 byte[] bytes1 = RSAUtil.decryptByPrivateKey(type, bytes, aPrivateEncoded); String s = new String(bytes1); Log.d(TAG, "私钥解密文件: " + s);
} catch (Exception e) { e.printStackTrace(); }
System.out.println("");
try { // 私钥加密 byte[] bytes = RSAUtil.encryptByPrivateKey(type, "456".getBytes(), aPrivateEncoded); String encode = Base64Utils.encode(bytes); Log.d(TAG, "私钥加密文件: " + encode);
// 公钥解密 byte[] bytes1 = RSAUtil.decryptByPublicKey(type, bytes, aPublicEncoded); String s = new String(bytes1); Log.d(TAG, "公钥解密文件: " + s);
} catch (Exception e) { e.printStackTrace(); }
setDES()
二、以下为我根据网上所总结的工具类(包含Base64 ,RSA,DES,AES) 工具类 Base64 public class Base64Util { /** * 编码 * * @Param message 需编码的信息 * @return * @throws UnsupportedEncodingException */ public static String encodeWord(String message) throws UnsupportedEncodingException {
return Base64.encodeToString(message.getBytes("utf-8"), Base64.NO_WRAP);
}
/** * 解码 * * @param encodeWord 编码后的内容 * @return * @throws UnsupportedEncodingException */ public static String decodeWord(String encodeWord) throws UnsupportedEncodingException {
return new String(Base64.decode(encodeWord, Base64.NO_WRAP), "utf-8");
} } RSA public class RSAUtil {
// 非对称加密密钥算法
public static final String RSA = "RSA";
// 加密填充方式,android 的
public static final String ECB_NO_PADDING = "RSA/None/NoPadding";
// 加密填充方式,标准jdk 的
public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";
//秘钥默认长度
public static final int DEFAULT_KEY_SIZE = 2048;
// 当要加密的内容超过bufferSize,则采用partSplit进行分块加密
public static final byte[] DEFAULT_SPLIT = "#PART#".getBytes();
// 当前秘钥支持加密的最大字节数
public static final int DEFAULT_BUFFERSIZE = (DEFAULT_KEY_SIZE / 8) - 11;
/**
* 随机生成RSA密钥对
*
* @param keyLength 密钥长度,范围:512~2048
* 一般1024
* @return
*/
public static KeyPair generateRSAKeyPair(int keyLength) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
kpg.initialize(keyLength);
return kpg.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
// ------------------------------------------------ 公钥 ---------------------------------------
/**
* 用公钥对字符串进行加密
*
* @param data 原文
*/
public static byte[] encryptByPublicKey(String type, byte[] data, byte[] publicKey) throws Exception {
// 得到公钥
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PublicKey keyPublic = kf.generatePublic(keySpec);
// 加密数据
if (type.equalsIgnoreCase("java")) {
Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
cp.init(Cipher.ENCRYPT_MODE, keyPublic);
return cp.doFinal(data);
} else {
Cipher cp = Cipher.getInstance(ECB_NO_PADDING);
cp.init(Cipher.ENCRYPT_MODE, keyPublic);
return cp.doFinal(data);
}
}
/**
* 公钥解密
*
* @param data 待解密数据
* @param publicKey 密钥
* @Return byte[] 解密数据
*/
public static byte[] decryptByPublicKey(String type, byte[] data, byte[] publicKey) throws Exception {
// 得到公钥
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PublicKey keyPublic = kf.generatePublic(keySpec);
// 数据解密
if (type.equalsIgnoreCase("java")) {
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
cipher.init(Cipher.DECRYPT_MODE, keyPublic);
return cipher.doFinal(data);
} else {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, keyPublic);
return cipher.doFinal(data);
}
}
// ------------------------------------------------ 私钥 ---------------------------------------
/**
* 私钥加密
*
* @param data 待加密数据
* @param privateKey 密钥
* @return byte[] 加密数据
*/
public static byte[] encryptByPrivateKey(String type, byte[] data, byte[] privateKey) throws Exception {
// 得到私钥
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PrivateKey keyPrivate = kf.generatePrivate(keySpec);
// 数据加密
if (type.equalsIgnoreCase("java")) {
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
return cipher.doFinal(data);
} else {
Cipher cipher = Cipher.getInstance(ECB_NO_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
return cipher.doFinal(data);
}
}
/**
* 使用私钥进行解密
*/
public static byte[] decryptByPrivateKey(String type, byte[] encrypted, byte[] privateKey) throws Exception {
// 得到私钥
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PrivateKey keyPrivate = kf.generatePrivate(keySpec);
// 解密数据
if (type.equalsIgnoreCase("java")) {
Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
cp.init(Cipher.DECRYPT_MODE, keyPrivate);
return cp.doFinal(encrypted);
} else {
Cipher cp = Cipher.getInstance(RSA);
cp.init(Cipher.DECRYPT_MODE, keyPrivate);
return cp.doFinal(encrypted);
}
}
} DES public class DESUtil {
private final static String HEX = "0123456789ABCDEF";
//DES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
private final static String TRANSFORMATION = "DES/CBC/PKCS5Padding";
//初始化向量参数,AES 为16bytes. DES 为8bytes.
private final static String IVPARAMETERSPEC = "01020304";
//DES是加密方式
private final static String ALGORITHM = "DES";
// SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法
private static final String SHA1PRNG = "SHA1PRNG";
// ------------------------------------- 静态 --------------------------------------------------
/**
* DES 加密
*
* @param message 原文
* @param key 密钥,长度不能够小于8位
* @return
* @throws Exception
*/
public static String desEncrypt(String message, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptbyte = cipher.doFinal(message.getBytes());
return new String(Base64.encode(encryptbyte, Base64.DEFAULT)).trim();
}
/**
* DES解密
*
* @param message 密文
* @param key 密钥,长度不能够小于8位
* @return
* @throws Exception
*/
public static String desDecrypt(String message, String key) throws Exception {
byte[] bytesrc = Base64.decode(message.getBytes(), Base64.DEFAULT);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
// ------------------------------------- 动态 --------------------------------------------------
/*
* 生成随机数,可以当做动态的密钥 加密和解密的密钥必须一致,不然将不能解密
*/
public static String generateKey() {
try {
SecureRandom localSecureRandom = SecureRandom.getInstance(SHA1PRNG);
byte[] bytes_key = new byte[20];
localSecureRandom.nextBytes(bytes_key);
String str_key = toHex(bytes_key);
return str_key;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 对密钥进行处理(方式一)
@SuppressLint("ObsoleteSdkInt")
private static Key getRawKey(String key) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
//for android
SecureRandom sr = null;
// 在4.2以上版本中,SecureRandom获取方式发生了改变
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
} else {
sr = SecureRandom.getInstance(SHA1PRNG);
}
// for Java
// secureRandom = SecureRandom.getInstance(SHA1PRNG);
sr.setSeed(key.getBytes());
kgen.init(64, sr); //DES固定格式为64bits,即8bytes。
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return new SecretKeySpec(raw, ALGORITHM);
}
// 对密钥进行处理(方式二)
private static Key getRawKey2(String key) throws Exception {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
return keyFactory.generateSecret(dks);
}
//二进制转字符
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf);
}
return result.toString();
}
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
*/
public static String encode(String key, String data) {
return encode(key, data.getBytes());
}
/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
*/
public static String encode(String key, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
IvParameterSpec iv = new IvParameterSpec(IVPARAMETERSPEC.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, getRawKey(key), iv);
byte[] bytes = cipher.doFinal(data);
return Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception e) {
return null;
}
}
/**
* 获取编码后的值
*
* @param key
* @param data
* @return
*/
public static String decode(String key, String data) {
return decode(key, Base64.decode(data, Base64.DEFAULT));
}
/**
* DES算法,解密
*
* @param data 待解密字符串
* @param key 解密私钥,长度不能够小于8位
* @return 解密后的字节数组
*/
public static String decode(String key, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
IvParameterSpec iv = new IvParameterSpec(IVPARAMETERSPEC.getBytes());
cipher.init(Cipher.DECRYPT_MODE, getRawKey(key), iv);
byte[] original = cipher.doFinal(data);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
return null;
}
}
}AES public class AESUtil {
private final static String HEX = "0123456789ABCDEF";
//AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
//AES 加密
private static final String AES = "AES";
// SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法
private static final String SHA1PRNG = "SHA1PRNG";
/**
* @return 动态生成秘钥
*/
public static String generateKey() {
try {
SecureRandom localSecureRandom = SecureRandom.getInstance(SHA1PRNG);
byte[] bytes_key = new byte[20];
localSecureRandom.nextBytes(bytes_key);
String str_key = toHex(bytes_key);
return str_key;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 对秘钥进行处理
*
* @param seed 动态生成的秘钥
* @return
* @throws Exception
*/
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance(AES);
//for android
SecureRandom sr = null;
// 在4.2以上版本中,SecureRandom获取方式发生了改变
int sdk_version = android.os.Build.VERSION.SDK_INT;
// Android 6.0 以上
if (sdk_version > 23) {
sr = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider());
//4.2及以上
} else if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
} else {
sr = SecureRandom.getInstance(SHA1PRNG);
}
// for Java
// secureRandom = SecureRandom.getInstance(SHA1PRNG);
sr.setSeed(seed);
//256 bits or 128 bits,192bits
kgen.init(128, sr);
//AES中128位密钥版本有10个加密循环,192比特密钥版本有12个加密循环,256比特密钥版本则有14个加密循环。
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
/**
* 加密
*
* @param key
* @param cleartext
* @return
*/
public static String encrypt(String key, String cleartext) {
if (TextUtils.isEmpty(cleartext)) {
return cleartext;
}
try {
byte[] result = encrypt(key, cleartext.getBytes());
return new String(Base64.encode(result, Base64.DEFAULT));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] encrypt(String key, byte[] clear) throws Exception {
byte[] raw = getRawKey(key.getBytes());
SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
/**
* 解密
*
* @param key
* @param encrypted
* @return
*/
public static String decrypt(String key, String encrypted) {
if (TextUtils.isEmpty(encrypted)) {
return encrypted;
}
try {
byte[] enc = Base64.decode(encrypted, Base64.DEFAULT);
byte[] result = decrypt(key, enc);
return new String(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] decrypt(String key, byte[] encrypted) throws Exception {
byte[] raw = getRawKey(key.getBytes());
SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
// ----------------------------------------------- 辅助方法 ------------------------------------
//二进制转字符
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf);
}
return result.toString();
}
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
// 增加 CryptoProvider 类
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");
}
}
}
以上是常见加密代码总结 由于代码量多直接复制
免费评分
查看全部评分