加密入门-从凯撒密码说起

package PostPager;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.util.Base64Utils;

public class Demo01 {

	public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		String clearText = "helloWord";
		// 要求秘钥8个字节
		String originKey = "15425845";
		String cipherText =  desencript(clearText,originKey);
		System.out.println(cipherText);
	}

	private static String desencript(String clearText, String originKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		// 获取加密算法工具
		Cipher cipher = Cipher.getInstance("DES");
		// 对加密工具类对象进行初始化
		SecretKeySpec secretKeySpec = new SecretKeySpec(originKey.getBytes(), "DES");
		cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
		// 用加密工具类对象对明文进行加密
		byte[] doFinal = cipher.doFinal(clearText.getBytes());
		return Base64Utils.encodeToString(doFinal);
	}

	// 解密方法和加密相似,先用Base64解码,再用相同的密匙解密就可以了
}

 

package PostPager;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.util.Base64Utils;

public class Demo02 {

	public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		String clearText = "helloWord";
		// DES要求秘钥8个字节   AES一般要求密匙16个字节
		String originKey = "1542584545125895";
		String cipherText =  AESencript(clearText,originKey);
		System.out.println(cipherText);
	}

	private static String AESencript(String clearText, String originKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		// 获取加密算法工具
		Cipher cipher = Cipher.getInstance("AES");
		// 对加密工具类对象进行初始化
		SecretKeySpec secretKeySpec = new SecretKeySpec(originKey.getBytes(), "AES");
		cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
		// 用加密工具类对象对明文进行加密
		byte[] doFinal = cipher.doFinal(clearText.getBytes());
		return Base64Utils.encodeToString(doFinal);
	}

	// 解密方法和加密相似,先用Base64解码,再用相同的密匙解密就可以了
}
package PostPager;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.util.Base64Utils;

public class Demo01 {

	public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
	String clearText = "_t=2018-10-12 13:58:06&auth_code=&id=27&mid=30&mmember_area_id=27&mrecharge_amount=1000.0&payment_method=1";
		// DES要求秘钥8个字节   AES一般要求密匙16个字节
	    // System.out.println("0加密前"+Arrays.toString(clearText.getBytes()));
		// String originKey = "1542584545125895";
		Cipher cipher = Cipher.getInstance("AES");
		// String cipherText =  AESencript(cipher,clearText,originKey);
		String originKey = "jqmmbshhzsybdwmm";
		String cipherText = "E9ARmmHeu4/hnR8ne3tz1LOSZtAQsfnUOe46HCTClvywkumRfok2bFjqAg+ad9YOGP21olRRmSFcV0csjUflIQABO9HCMqyhtOkBSWmYb1HtMZD1Zx+v4+BbFiRcWck8j9PCo1sifbPtXtaEKntODg==";
		// System.out.println(cipherText);
		
		String mingWen = AESdecript( cipher ,cipherText,originKey);
		System.out.println(mingWen);
	}

    private static String AESdecript(Cipher cipher ,String cipherText, String originKey) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
       byte[] decodeFromString = Base64Utils.decodeFromString(cipherText);
       System.out.println("解码后"+Arrays.toString(decodeFromString));
        // 获取加密算法工具
        
        // 对加密工具类对象进行初始化
        SecretKeySpec secretKeySpec = new SecretKeySpec(originKey.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        // 用加密工具类对象对明文进行加密
        byte[] doFinal = cipher.doFinal(decodeFromString);
        return new String(doFinal);
    }

    private static String AESencript(Cipher cipher ,String clearText, String originKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		// 获取加密算法工具
		//Cipher cipher = Cipher.getInstance("AES");
		// 对加密工具类对象进行初始化
		SecretKeySpec secretKeySpec = new SecretKeySpec(originKey.getBytes(), "AES");
		cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
		// 用加密工具类对象对明文进行加密
		byte[] doFinal = cipher.doFinal(clearText.getBytes());
		System.out.println("一次加密后:"+Arrays.toString(doFinal));
		return Base64Utils.encodeToString(doFinal);
	}

	// 解密方法和加密相似,先用Base64解码,再用相同的密匙解密就可以了
	// PkIHK8XCtvdYtNrOGlE0vKLNPBUfzsbJ98Vx+k29lfw16O4B3Hiot9rr+jXHl5l8Zuh/2DDttAYnSaZDbVaZUV0Le3mIwKH2EjJKeCwrtVmmCI6AU17nU52QPL4np5SLYYcn0w+yB6NCnEAAJmUUZg==
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值