欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-13 14:49:05
...
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==
}

 

相关标签: 加密