您的位置:首页 > 其它

Base64编码、MD5消息摘要、对称加密、非对称加密

2017-02-27 13:50 477 查看

一、 Base64编码

(一)、要点

可对传输中的数据进行简单处理,使之不易被辨认

Base64并不是一种加密算法,它只是一种编码方式

(二)、核心代码

1.编码

//Base64编码,第一个参数表示要编码的字符串
//第二个参数表示编码后字符串的格式等属性全部采用默认值
String s = Base64.encodeToString(srcStr.getBytes("UTF-8"), Base64.DEFAULT);


2.解码

//Base64解码
//第一个参数表示要解码的字符串
byte[] decode = Base64.decode(s, Base64.DEFAULT);


二、 MD5消息摘要

介绍:在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.

数字签名也是MD5的另一个使用场景.

(一)、要点

1.使用MD5加密可以保证数据的完整性

2.不管多长的数据都可以生产固定长度的消息摘要

3.不可逆,不可以由消息摘要推出数据正文

4.MD5一般可以用在密码加密处理上

(二)、核心代码

生成消息摘要:

//创建消息摘要实例,MD5表示生成消息摘要的算法名称
MessageDigest messageDigest = MessageDigest.getInstance("md5");
byte[] digest = messageDigest.digest(srcEt.getText().toString().getBytes("UTF-8"));
//用16进制显示生成的消息摘要
StringBuffer result = new StringBuffer();
for (byte b : digest) {
result.append(String.format("%02x", b));
}
showMd5.setText(result.toString());


三、 对称加密

(一)、要点

1.加密和解密的密钥相同,解密只是加密的逆运算

2.常用的对称加密算法有DES,3DES,AES三种,这三种加密算法对应的密钥长度分别是8位、24位以及32位

(二)、核心代码

1.DES加/解密

/**
* @param src  要加密/解密的字符串
* @param key  密钥
* @param mode 加密/解密
* @return
*/
public static String des(String src, String key, int mode) {
String charsetName = "UTF-8";
try {
byte[] bytes = key.getBytes(charsetName);
byte[] temp = new byte[8];
System.arraycopy(bytes, 0, temp, 0, Math.min(bytes.length, temp.length));
//1.密钥
//2.加密算法的名称
SecretKey secretKey = new SecretKeySpec(temp, "des");
//创建一个密码生成器,参数表示生成密码的算法名称
Cipher cipher = Cipher.getInstance("des");
//加密
if (mode == ENCRYPT) {
//初始化密码生成器
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
//生成密文
byte[] bytes1 = cipher.doFinal(src.getBytes(charsetName));
return Base64.encodeToString(bytes1, Base64.DEFAULT);
//解密
} else {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] bytes1 = cipher.doFinal(Base64.decode(src, Base64.DEFAULT));
return new String(bytes1, 0, bytes1.length);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} 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();
}
return null;
}


2.3DES加/解密

public static String des3(String src, String key, int mode) {
String charsetName = "UTF-8";
try {
byte[] keyBytes = key.getBytes(charsetName);
byte[] temp = new byte[24];
System.arraycopy(keyBytes, 0, temp, 0, Math.min(keyBytes.length, temp.length));
SecretKey secretKey = new SecretKeySpec(temp, "desede");
Cipher cipher = Cipher.getInstance("desede");
if (mode == ENCRYPT) {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(src.getBytes(charsetName));
return Base64.encodeToString(bytes, Base64.DEFAULT);
} else {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(Base64.decode(src, Base64.DEFAULT));
return new String(bytes, 0, bytes.length);
}
} catch (UnsupportedEncodingException e) {
4000
e.printStackTrace();
} 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();
}
return null;
}


3.AES加/解密

public static String aes(String src, String key, int mode) {
String charsetName = "UTF-8";
try {
byte[] keyBytes = key.getBytes(charsetName);
byte[] temp = new byte[32];
System.arraycopy(keyBytes, 0, temp, 0, Math.min(keyBytes.length, temp.length));
SecretKey secretKey = new SecretKeySpec(temp, "aes");
Cipher cipher = Cipher.getInstance("aes");
if (mode == ENCRYPT) {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(src.getBytes(charsetName));
return Base64.encodeToString(bytes, Base64.DEFAULT);
} else {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(Base64.decode(src, Base64.DEFAULT));
return new String(bytes, 0, bytes.length);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} 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();
}
return null;
}


四、非对称加密

(一)、要点

1.加密key和解密key不同

2.一般流程:发信方使用收信方的公钥(public_key)加密并发送消息,收信方使用自己的私钥(private_key)解密得到明文

3.常用算法有:RSA

4.RSA算法原理:两个大素数的乘积极难被因式分解,因此这两个大素数可以分别被用来作为公钥和私钥

(二)、核心代码

加密代码:

//获得生成一个公钥/私钥的工厂方法
KeyFactory keyFactory = KeyFactory.getInstance("rsa");
RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(new BigInteger(MODULUS), new BigInteger(PUB_KEY));
//获得一个密码生成器,参数表示生成密码的算法
Cipher cipher = Cipher.getInstance("rsa");
//生成公钥
PublicKey key = keyFactory.generatePublic(rsaPublicKeySpec);
//用公钥加密文本
cipher.init(Cipher.ENCRYPT_MODE, key);
//获得密文
byte[] bytes = cipher.doFinal(s.getBytes("UTF-8"));
String s1 = Base64.encodeToString(bytes, Base64.DEFAULT);
decodeEt.setText(s1);


解密代码:

KeyFactory keyFactory = KeyFactory.getInstance("rsa");
RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(new BigInteger(MODULUS), new BigInteger(PRI_KEY));
Cipher cipher = Cipher.getInstance("rsa");
//获取私钥
PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(Base64.decode(s, Base64.DEFAULT));
showSrc.setText(new String(bytes, 0, bytes.length));


注意:实际开发工作中,公钥和密钥都是由服务端的研发人员使用工具自动生成,然后将公钥发送给Android前端工程师进行加密
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  加密算法
相关文章推荐