java加密(对称)

Posted 暴疯禹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java加密(对称)相关的知识,希望对你有一定的参考价值。

java加密(对称)

   常用对称加密:AES,DES,DESede,IDEA。

   "AES/ECB/PKCS5Padding"
 密钥(keyGenerator)密文(cipher)
AES“AES”
DES“DES”"DES/ECB/PKCS5Padding"
DESede"DESede""DESede/ECB/PKCS5Padding"
IDEA"IDEA""IDEA/ECB/PKCS5Padding"

如下面代码,使用不同加密算法,只要替换对应字符串。
 
 以AES为例: 
public class AESUtil 
	public static void main(String[]args)
		byte[] code=encryptAESCode("zmycontent", "zmykey");
		String scode=byteCodeToString(code);
		System.out.println(Arrays.toString(code));
		System.out.println(scode);
		System.out.println(Arrays.toString(stringCodeToBytes(scode)));
		System.out.println(decryptAESCode(code, "zmykey"));
	
	//AES加密
	public static byte[] encryptAESCode(String source,String password)
		try 
			
			//密钥生成器
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			//使用指定种子字节设定随机数
			byte[] pswBytes = password.getBytes("utf-8");
			SecureRandom sr = new SecureRandom(pswBytes);
			//初始化密钥生成器
			//指定密钥大小时只能是128, 192 or 256
			//由于美国对软件出口的限制,读到的是受限的policy文件,所以AES这个地方init长度只能是128,想要修改为其他长度,上网去下policy文件。
			kgen.init(128,sr);
			//生成密钥
			SecretKey secretKey = kgen.generateKey();
			//获取密钥字节
			byte[] enCodeFormat = secretKey.getEncoded();
			//根据给定密钥字节数组构造密钥
			SecretKeySpec key = new SecretKeySpec(enCodeFormat,"AES");
			
			//创建密码器
			Cipher cipher= Cipher.getInstance("AES/ECB/PKCS5Padding");
			byte[] contents=source.getBytes("utf-8");
			//初始化
			cipher.init(Cipher.ENCRYPT_MODE, key);
			
			byte[] result = cipher.doFinal(contents);
			
			return result;
					
		 catch (NoSuchAlgorithmException e) 
			e.printStackTrace();
		 catch (UnsupportedEncodingException e) 
			e.printStackTrace();
		 catch (NoSuchPaddingException e) 
			e.printStackTrace();
		 catch (InvalidKeyException e) 
			e.printStackTrace();
		 catch (IllegalBlockSizeException e) 
			e.printStackTrace();
		 catch (BadPaddingException e) 
			e.printStackTrace();
		
		
		
		return null;
	
	//AES解密
	public static String decryptAESCode(byte[] encryptCode,String password)
		
		try 
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128,new SecureRandom(password.getBytes("utf-8")));
			SecretKey skey = kgen.generateKey();
			SecretKeySpec sks = new SecretKeySpec(skey.getEncoded(),"AES");
			
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, sks);
			byte[] result = cipher.doFinal(encryptCode);
			
			return new String(result,"utf-8");
			
		 catch (NoSuchAlgorithmException e) 
			e.printStackTrace();
		 catch (UnsupportedEncodingException e) 
			e.printStackTrace();
		 catch (NoSuchPaddingException e) 
			e.printStackTrace();
		 catch (InvalidKeyException e) 
			e.printStackTrace();
		 catch (IllegalBlockSizeException e) 
			e.printStackTrace();
		 catch (BadPaddingException e) 
			e.printStackTrace();
		
		
		return null;
	
	//将加密byte[]转换成String
	public static String byteCodeToString(byte[] code)
		StringBuilder sb = new StringBuilder();
		
		//将code范围==>char范围==>转换成String
		for(int i=0;i<code.length;i++)
			int temp =(code[i]+256)%256;
			sb.append((char)temp);
		
		return sb.toString();
	
	//将加密String转换成byte[]
	public static byte[] stringCodeToBytes(String code)
		byte[] result = new byte[code.length()];
		for(int i=0;i<code.length();i++)
			char c =code.charAt(i);
			if(c>127)
				result[i]=(byte)(c-256);
			else
				result[i]=(byte)c;
			
		
		
		return result;
	
将AES密钥文件提出,作为key文件存储:
public class AESUtil 
	public static void main(String[]args) throws Exception
		generateAESkey("zmykey","c:\\\\myencrypt\\\\SecretKey.zmy");
		byte[] encryptCode =encryptByObjKey("Objtestcontent", "c:\\\\myencrypt\\\\SecretKey.zmy");
		System.out.println(Arrays.toString(encryptCode));
		System.out.println(byteCodeToString(encryptCode));
		System.out.println(decryptByObjKey(encryptCode, "c:\\\\myencrypt\\\\SecretKey.zmy"));
	
	//AES加密,生成key文件
	public static void generateAESkey(String password,String keyfileurl) throws Exception
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128,new SecureRandom(password.getBytes("utf-8")));
		SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(),"AES");
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keyfileurl));
		oos.writeObject(sks);
		oos.close();
	
	//AES加密,采用的是文件key
	public static byte[] encryptByObjKey(String source,String keyfileurl)throws  Exception		
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(keyfileurl));
		SecretKeySpec key =(SecretKeySpec) ois.readObject();
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] result = cipher.doFinal(source.getBytes("utf-8"));
		return result;
	
	//AES解密
	public static String decryptByObjKey(byte[] encryptCode,String keyfileurl)throws  Exception		
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(keyfileurl));
		SecretKeySpec key =(SecretKeySpec) ois.readObject();
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte[] result = cipher.doFinal(encryptCode);
		return new String(result,"utf-8");
	
       //将加密byte[]转换成String
	public static String byteCodeToString(byte[] code)
		StringBuilder sb = new StringBuilder();
		//将code范围==>char范围==>转换成String
		for(int i=0;i<code.length;i++)
			int temp =(code[i]+256)%256;
			sb.append((char)temp);
		
		return sb.toString();
	
	//将加密String转换成byte[]
	public static byte[] stringCodeToBytes(String code)
		byte[] result = new byte[code.length()];
		for(int i=0;i<code.length();i++)
			char c =code.charAt(i);
			if(c>127)
				result[i]=(byte)(c-256);
			else
				result[i]=(byte)c;
			
		
		return result;
	

 
  



以上是关于java加密(对称)的主要内容,如果未能解决你的问题,请参考以下文章

Java加密与解密笔记 非对称加密

非对称加密算法都有哪些,安全性能对比

JAVA加密解密DES对称加密算法

java 加密技术

java加密算法入门-算法概念及单向加密

非对称加密及RSA加密算法