java---加密-笔记

1.凯撒密码

  • 通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文
    • 明文:原始信息
    • 密文:加密后的信息
    • 秘钥:加密与解密算法的参数
    • 加密算法:以秘钥为参数,对明文进行多种置换和转换的规则和步骤,变换结果为密文
    • 解密算法:加密算法逆变换

2.对称加密

  • 加密解密用同一个秘钥
  • 常用数学运算:
    • 移位和循环移位
    • 置换
    • 扩展
    • 压缩
    • 异或
    • 迭代:多次重复相同运算
  • DES算法(Data Encryption Stantard)
//java
/**
* originalKey 8字节
* 问题:GBK,UTF-8等对字符编码后,加密,可能导致乱码导致字节丢失问题,解密后不能正常解码
*/
public static String encrypt(String clearText, String originalKey)
{
	//1.获取加密算法工具类对象
	Cipher cipher = Cipher.getInstance("DES");
	//2.对该对象初始化
	//mode:加密/解密模式
	//key:对originalKey处理之后的秘钥
	SecretKeySpec key = getKey(originalKey);
	cipher.init(Cipher.ENCRYPT_MODE, key);
	
	//3.加密
	byte[] dofinal = cipher.doFinal(clearText.getBytes());
	return new String(dofinal);
}
public static SecretKeySpec getKey(String originalKey)
{
	SecretKeySpec key = new SecretKeySpec(originalKey.getBytes(),"DES");
	return key;
}

3. 写一个java 加密类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
 * 加密
 * 加密解密可能造成编解码过程的字节丢失,引入Base64编码
 * 加密解密的字节块大小必须相等
 * 流程:
 * 加密: 获取字节数组->字节数组加密->得到加密的字节数组->对该字节数组进行Base64编码->写入(存储)
 * 解密: 获取字节数组->Base64解码->得到解码后的字节数组->对该字节数组解密->写入(存储)
 *  note:
 *  	FileOutputStream 可以输入不存在的文件路径,但其文件夹路径必须存在
 * @author Administrator
 *
 */
public abstract class MyCipher {
	private static final String ALGORITHM = "DES"; // 加密算法
	private static final String KEY = "12345678"; // 秘钥
	/**
	 * N: 读入字节数组最大长度,对此长度字节数组加密
	 * 	加密后得到的字节数组长度扩大为8的整数倍
	 * 	Base64编码以3个字节为一组,一组按6位编码为4个字节,不足3字节的一组将填充至3字节
	 * 	因此一次加密操作将得到 M = (N/8)*8 + (N%8!=0)?8:0 字节;
	 * 	再进行一次Base64编码将得到 Z = (M/3)*4 + (M%3!=0)?4:0 字节;
	 * 问题:未解决->已解决
	 * 	经过测试,少于8字节的数字加密后得到8字节的数组,但等于8字节的数组加密后将得到16字节的数组
	 * 	因此暂时将N设为7,程序能正常运行
	 * 	问题解决:
	 * 		经过测试,1~7字节加密生成8字节,8~15字节加密生成16字节
	 * 		所以 N为8的整数倍时,规则有所不同
	 * 		所以将避免将N的取值取到8的倍数
	 */
	private static final int N = 31;
	private static final Base64.Encoder encoder = Base64.getEncoder(); // Base64编码器
	private static final Base64.Decoder decoder = Base64.getDecoder(); // Base64解码器

	/**
	 * 加密
	 * @param clearByte 明文字节数组
	 * @return 经过Base64编码的密文字节数组
	 */
	public static byte[] encrypt(byte[] clearByte)
	{
		SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
		Cipher cipher;
		try {
			cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE,skey);
			//1.加密
			byte[] dofinal = cipher.doFinal(clearByte);
			//2.编码
			byte[] encode = encoder.encode(dofinal);
			return encode;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 解密
	 * @param cipherByte 加密的字节数组
	 * @return 解码后经过解密的字节数组
	 */
	public static byte[] decrypt(byte[] cipherByte)
	{
		SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
		Cipher cipher;
		try {
			cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE,skey);
			//1.对密文线进行Base64解码,获取字节
			byte[] decode = decoder.decode(cipherByte);
			//2.解密
			byte[] dofinal = cipher.doFinal(decode);
			return dofinal;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 加密文件
	 * @param source
	 * @param target
	 * @throws IOException
	 */
	public static void encryptFile(File source, File target) throws IOException
	{
		FileInputStream fin = new FileInputStream(source);
		FileOutputStream fout = new FileOutputStream(target);
		byte[] buffer = new byte[N];
		int num = 0;
		while((num=fin.read(buffer))!=-1)
		{
			byte[] temp = Arrays.copyOf(buffer, num);
			// 加密,并返回Base64编码的字节数组
			byte[] encrypted = encrypt(temp);
			fout.write(encrypted);
		}
		fin.close();
		fout.close();
	}
	/**
	 * 解密文件
	 * @param source
	 * @param target
	 * @throws IOException
	 */
	public static void decryptFile(File source, File target) throws IOException
	{
		FileInputStream fin = new FileInputStream(source);
		FileOutputStream fout = new FileOutputStream(target);
		int M = (N/8)*8 + (N%8!=0?8:0);
		int Z = (M/3)*4 + (M%3!=0?4:0);
		byte[] buffer = new byte[Z];
		int num = 0;
		while((num=fin.read(buffer))!=-1)
		{
			byte[] temp = Arrays.copyOf(buffer, num);
			byte[] decrypted = decrypt(temp);
			fout.write(decrypted);
		}
		fin.close();
		fout.close();
	}
	private static void printByte(byte[] b, int n)
	{
		int m = b.length<n?b.length:n;
		for(int i=0;i<m;i++)
		{
			System.out.print(b[i]+" ");
		}
		System.out.println();
	}
	/**
	 * 加密字符串
	 * @param clearText 明文
	 * @return 密文
	 */
	public static String encryptString(String clearText)
	{
		SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
		Cipher cipher;
		try {
			cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE,skey);
			//1.加密
			byte[] dofinal = cipher.doFinal(clearText.getBytes());
			//2.对加密后的字节进行Base64编码,避免出现乱码
			String encode = encoder.encodeToString(dofinal);
			return encode;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 解密字符串
	 * @param cipherText 密文
	 * @return 明文
	 */
	public static String decryptString(String cipherText)
	{
		SecretKey skey = new SecretKeySpec(KEY.getBytes(),ALGORITHM);
		Cipher cipher;
		try {
			cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE,skey);
			//1.对密文线进行Base64解码,获取字节
			byte[] decode = decoder.decode(cipherText);
			//2.解密
			byte[] dofinal = cipher.doFinal(decode);
			return new String(dofinal);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值