一、Java常用加密方式
- Base64加密算法(编码方式)
- MD5加密(消息摘要算法,验证信息完整性)
- 对称加密算法
- 非对称加密算法
- 数字签名算法
- 数字证书
二、分类
按加密算法是否需要key被分为两类:
- 不基于key的有: Base64算法、MD5
- 基于key的有: 对称加密算法、非对称加密算法、数字签名算法、数字证书、HMAC、RC4(对称加密)
按加密算法是否可逆被分为两类:
- 单向加密算法(不可解密):MD5、SHA、HMAC
- 非单项加密算法(可解密):BASE64、对称加密算法、非对称加密算法、数字签名算法、数字证书
三、算法介绍
1.对称加密
对称加密是最快速、最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥(secret key)。对称加密有很多种算法,由于它效率很高,所以被广泛使用在很多加密协议的核心当中。
对称加密通常使用的是相对较小的密钥,一般小于256 bit。因为密钥越大,加密越强,但加密与解密的过程越慢。如果你只用1 bit来做这个密钥,那黑客们可以先试着用0来解密,不行的话就再用1解;但如果你的密钥有1 MB大,黑客们可能永远也无法破解,但加密和解密的过程要花费很长的时间。密钥的大小既要照顾到安全性,也要照顾到效率,是一个trade-off。
DES(Data Encryption Standard)和TripleDES是对称加密的两种实现。
DES和TripleDES基本算法一致,只是TripleDES算法提供的key位数更多,加密可靠性更高。
DES使用的密钥key为8字节,初始向量IV也是8字节。
TripleDES使用24字节的key,初始向量IV也是8字节。
两种算法都是以8字节为一个块进行加密,一个数据块一个数据块的加密,一个8字节的明文加密后的密文也是8字节。如果明文长度不为8字节的整数倍,添加值为0的字节凑满8字节整数倍。所以加密后的密文长度一定为8字节的整数倍
下面举个例子:
import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import org.apache.commons.codec.binary.Base64; public class DESDemo { // 算法名称 public static final String KEY_ALGORITHM = "DES"; // 算法名称/加密模式/填充方式 // DES共有四种工作模式-->>ECB:电子密码本模式、CBC:加密分组链接模式、CFB:加密反馈模式、OFB:输出反馈模式 public static final String CIPHER_ALGORITHM = "DES/ECB/NoPadding"; /** * * 生成密钥key对象 * * @param KeyStr * 密钥字符串 * @return 密钥对象 * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws Exception */ private static SecretKey keyGenerator(String keyStr) throws Exception { byte input[] = HexString2Bytes(keyStr); DESKeySpec desKey = new DESKeySpec(input); // 创建一个密匙工厂,然后用它把DESKeySpec转换成 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); return securekey; } private static int parse(char c) { if (c >= 'a') return (c - 'a' + 10) & 0x0f; if (c >= 'A') return (c - 'A' + 10) & 0x0f; return (c - '0') & 0x0f; } // 从十六进制字符串到字节数组转换 public static byte[] HexString2Bytes(String hexstr) { byte[] b = new byte[hexstr.length() / 2]; int j = 0; for (int i = 0; i < b.length; i++) { char c0 = hexstr.charAt(j++); char c1 = hexstr.charAt(j++); b[i] = (byte) ((parse(c0) << 4) | parse(c1)); } return b; } /** * 加密数据 * * @param data * 待加密数据 * @param key * 密钥 * @return 加密后的数据 */ public static String encrypt(String data, String key) throws Exception { Key deskey = keyGenerator(key); // 实例化Cipher对象,它用于完成实际的加密操作 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); SecureRandom random = new SecureRandom(); // 初始化Cipher对象,设置为加密模式 cipher.init(Cipher.ENCRYPT_MODE, deskey, random); byte[] results = cipher.doFinal(data.getBytes()); // 该部分是为了与加解密在线测试网站(http://tripledes.online-domain-tools.com/)的十六进制结果进行核对 for (int i = 0; i < results.length; i++) { System.out.print(results[i] + " "); } System.out.println(); // 执行加密操作。加密后的结果通常都会用Base64编码进行传输 return Base64.encodeBase64String(results); } /** * 解密数据 * * @param data * 待解密数据 * @param key * 密钥 * @return 解密后的数据 */ public static String decrypt(String data, String key) throws Exception { Key deskey = keyGenerator(key); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); // 初始化Cipher对象,设置为解密模式 cipher.init(Cipher.DECRYPT_MODE, deskey); // 执行解密操作 return new String(cipher.doFinal(Base64.decodeBase64(data))); } public static void main(String[] args) throws Exception { String source = "helloittx"; System.out.println("原文: " + source); String key = "A1B2C3D4E5F60708"; String encryptData = encrypt(source, key); System.out.println("加密后: " + encryptData); String decryptData = decrypt(encryptData, key); System.out.println("解密后: " + decryptData); } }
2.非对称加密
非对称加密为数据的加密与解密提供了一个非常安全的方法,它使用了一对密钥,公钥(public key)和私钥(private key)。私钥只能由一方安全保管,不能外泄,而公钥则可以发给任何请求它的人。非对称加密使用这对密钥中的一个进行加密,而解密则需要另一个密钥。比如,你向银行请求公钥,银行将公钥发给你,你使用公钥对消息加密,那么只有私钥的持有人–银行才能对你的消息解密。与对称加密不同的是,银行不需要将私钥通过网络发送出去,因此安全性大大提高。
目前最常用的非对称加密算法是RSA算法,是Rivest, Shamir, 和Adleman于1978年发明,他们那时都是在MIT。请看下面的例子:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.spec.RSAPrivateCrtKeySpec; import java.security.spec.RSAPublicKeySpec; import javax.crypto.Cipher; import com.lxh.rsatest.HexUtil; import Decoder.BASE64Decoder; import Decoder.BASE64Encoder; public class RSAEncrypt { /** 指定加密算法为DESede */ private static String ALGORITHM = "RSA"; /** 指定key的大小 */ private static int KEYSIZE = 1024; /** 指定公钥存放文件 */ private static String PUBLIC_KEY_FILE = "public.keystore"; /** 指定私钥存放文件 */ private static String PRIVATE_KEY_FILE = "private.keystore"; /** * 生成密钥对 */ private static void generateKeyPair() throws Exception { /** RSA算法要求有一个可信任的随机数源 */ SecureRandom sr = new SecureRandom(); /** 为RSA算法创建一个KeyPairGenerator对象 */ KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM); /** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */ kpg.initialize(KEYSIZE, sr); /** 生成密匙对 */ KeyPair kp = kpg.generateKeyPair(); /** 得到公钥 */ Key publicKey = kp.getPublic(); /** 得到私钥 */ Key privateKey = kp.getPrivate(); /** 用对象流将生成的密钥写入文件 */ ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE)); ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE)); oos1.writeObject(publicKey); oos2.writeObject(privateKey); /** 清空缓存,关闭文件输出流 */ oos1.close(); oos2.close(); } /** * 生成密钥对字符串 */ private static void generateKeyPairString() throws Exception { /** RSA算法要求有一个可信任的随机数源 */ SecureRandom sr = new SecureRandom(); /** 为RSA算法创建一个KeyPairGenerator对象 */ KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM); /** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */ kpg.initialize(KEYSIZE, sr); /** 生成密匙对 */ KeyPair kp = kpg.generateKeyPair(); /** 得到公钥 */ Key publicKey = kp.getPublic(); /** 得到私钥 */ Key privateKey = kp.getPrivate(); /** 用字符串将生成的密钥写入文件 */ String algorithm = publicKey.getAlgorithm(); // 获取算法 KeyFactory keyFact = KeyFactory.getInstance(algorithm); BigInteger prime = null; BigInteger exponent = null; RSAPublicKeySpec keySpec = (RSAPublicKeySpec) keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class); prime = keySpec.getModulus(); exponent = keySpec.getPublicExponent(); System.out.println("公钥模量:" + HexUtil.bytes2Hex(prime.toByteArray())); System.out.println("公钥指数:" + HexUtil.bytes2Hex(exponent.toByteArray())); System.out.println(privateKey.getAlgorithm()); RSAPrivateCrtKeySpec privateKeySpec = (RSAPrivateCrtKeySpec) keyFact.getKeySpec(privateKey, RSAPrivateCrtKeySpec.class); BigInteger privateModulus = privateKeySpec.getModulus(); BigInteger privateExponent = privateKeySpec.getPrivateExponent(); System.out.println("私钥模量:" + HexUtil.bytes2Hex(privateModulus.toByteArray())); System.out.println("私钥指数:" + HexUtil.bytes2Hex(privateExponent.toByteArray())); } /** * 加密方法 source: 源数据 */ public static String encrypt(String source) throws Exception { generateKeyPair(); /** 将文件中的公钥对象读出 */ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE)); Key key = (Key) ois.readObject(); ois.close(); String algorithm = key.getAlgorithm(); // 获取算法 KeyFactory keyFact = KeyFactory.getInstance(algorithm); BigInteger prime = null; BigInteger exponent = null; if ("RSA".equals(algorithm)) { // 如果是RSA加密 RSAPublicKeySpec keySpec = (RSAPublicKeySpec) keyFact.getKeySpec(key, RSAPublicKeySpec.class); prime = keySpec.getModulus(); exponent = keySpec.getPublicExponent(); // System.out.println("公钥模量:" + HexUtil.bytes2Hex(prime.toByteArray())); // System.out.println("公钥指数:" + HexUtil.bytes2Hex(exponent.toByteArray())); } /** 得到Cipher对象来实现对源数据的RSA加密 */ Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] b = source.getBytes(); /** 执行加密操作 */ byte[] b1 = cipher.doFinal(b); BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(b1); } /** * 解密算法 cryptograph:密文 */ public static String decrypt(String cryptograph) throws Exception { /** 将文件中的私钥对象读出 */ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE)); Key key = (Key) ois.readObject(); String algorithm = key.getAlgorithm(); // 获取算法 KeyFactory keyFact = KeyFactory.getInstance(algorithm); RSAPrivateCrtKeySpec privateKeySpec = (RSAPrivateCrtKeySpec) keyFact.getKeySpec(key, RSAPrivateCrtKeySpec.class); BigInteger privateModulus = privateKeySpec.getModulus(); BigInteger privateExponent = privateKeySpec.getPrivateExponent(); // System.out.println("私钥模量:" + HexUtil.bytes2Hex(privateModulus.toByteArray())); // System.out.println("私钥指数:" + HexUtil.bytes2Hex(privateExponent.toByteArray())); /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */ Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); BASE64Decoder decoder = new BASE64Decoder(); byte[] b1 = decoder.decodeBuffer(cryptograph); /** 执行解密操作 */ byte[] b = cipher.doFinal(b1); return new String(b); } public static void main(String[] args) throws Exception { generateKeyPair(); //生成文件形式公钥和私钥 //generateKeyPairString();//生成字符串形式公钥和私钥 String source = "非对称加密RSA";// 要加密的字符串 String cryptograph = encrypt(source);// 生成的密文 String hexCrypt = HexUtil.bytes2Hex(cryptograph.getBytes(), false); System.out.println("生成的密文--->" + hexCrypt); String target = decrypt(HexUtil.hex2String(hexCrypt));// 解密密文 System.out.println("解密密文--->" + target); } }
虽然非对称加密很安全,但是和对称加密比起来,它非常的慢,所以我们还是要用对称加密来传送消息,但对称加密所使用的密钥我们可以通过非对称加密的方式发送出去。
(1) 对称加密加密与解密使用的是同样的密钥,所以速度快,但由于需要将密钥在网络传输,所以安全性不高。
(2) 非对称加密使用了一对密钥,公钥与私钥,所以安全性高,但加密与解密速度慢。
(3) 解决的办法是将对称加密的密钥使用非对称加密的公钥进行加密,然后发送出去,接收方使用私钥进行解密得到对称加密的密钥,然后双方可以使用对称加密来进行沟通。
3.Base64编码
Base 64 Encoding有什么用?举个简单的例子,你使用SMTP协议 (Simple Mail Transfer Protocol 简单邮件传输协议)来发送邮件。因为这个协议是基于文本的协议,所以如果邮件中包含一幅图片,我们知道图片的存储格式是二进制数据(binary data),而非文本格式,我们必须将二进制的数据编码成文本格式,这时候Base 64 Encoding就派上用场了。
public void testJDKBase64(){ String encoderStr = java.util.Base64.getEncoder().encodeToString(s.getBytes()); System.out.println("encode :"+encoderStr); String decodeStr = new String(java.util.Base64.getDecoder().decode(encoderStr)); System.out.println("decodeStr :"+decodeStr); } public void testCodecBase64(){ String encoderStr = org.apache.commons.codec.binary.Base64.encodeBase64String(s.getBytes()); System.out.println("encode :"+encoderStr); String decodeStr = new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoderStr)); System.out.println("decodeStr :"+decodeStr); }
附工具类
package com.hl.bluetooth.util; import org.springframework.boot.system.ApplicationHome; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; import java.util.Objects; /** * @DateL 2021/11/16 15:09 * @ClassName: FileUtils **/ @Component public class FileUtils { public static File multipartFileToFile(MultipartFile multipartFile) { File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename())); try { InputStream ins = null; ins = multipartFile.getInputStream(); OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } return file; } /** * 图片转化成base64字符串 * */ public static String getImageStr(String imgPath) { InputStream in = null; byte[] data = null; String encode = null; // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); try { // 读取图片字节数组 in = new FileInputStream(imgPath); data = new byte[in.available()]; in.read(data); encode = encoder.encode(data); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return encode; } /** * base64字符串转化成图片 * * @param imgData 图片编码 */ public static String generateImage(String imgData, String fileName) { if (imgData == null) { // 图像数据为空 return "null"; } ApplicationHome applicationHome = new ApplicationHome(FileUtils.class); File source = applicationHome.getSource(); String dirPath = source.getParentFile().toString() + "/upload"; BASE64Decoder decoder = new BASE64Decoder(); File dir = new File(dirPath); if (!dir.exists()){ dir.mkdirs(); } File file = new File(dirPath+"/"+fileName); if (file.exists()){ file.delete(); } try { // Base64解码 byte[] b = decoder.decodeBuffer(imgData); for (int i = 0; i < b.length; ++i) { // 调整异常数据 if (b[i] < 0) { b[i] += 256; } } OutputStream out = new FileOutputStream(dirPath+"\\"+fileName); out.write(b); out.flush(); out.close(); return dirPath+"\\"+fileName; } catch (Exception e) { e.printStackTrace(); return "null"; } } }