私钥加密

public static void PrivEncrypt(String oldStr) throws Exception {
		byte[] plainText = oldStr.getBytes("UTF8");
		// 获得一个私鈅加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		SecretKeySpec key = new SecretKeySpec("1234567812345678".getBytes(), "AES"); 
		cipher.init(Cipher.ENCRYPT_MODE, key);// 使用私鈅加密
		byte[] cipherText = cipher.doFinal(plainText);
		System.out.println("加密:"+new String(cipherText, "UTF8"));
		
		cipher.init(Cipher.DECRYPT_MODE, key);// 使用私钥解密
		byte[] newPlainText = cipher.doFinal(cipherText);
		System.out.println("解密:"+new String(newPlainText, "UTF8"));
	}

你可能感兴趣的:(加密)