Java加密解密相关


关于解释加密解密中的填充方案:

http://laokaddk.blog.51cto.com/368606/461279/


关于对称加密中的反馈模式:

http://blog.csdn.net/aaaaatiger/article/details/2525561


:写法为:

DES/CBC/PKCS5Padding


各种加密算法的加密解密写法:已有DES,MD5,SHA(未结束)

package com.qin.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class Main {
	public static void main(String[] args) throws InvalidKeyException,
			NoSuchAlgorithmException, InvalidKeySpecException,
			IllegalBlockSizeException, BadPaddingException,
			NoSuchPaddingException, IOException {
		Main m = new Main();
		// m.desJie(m.desJia());
		// m.createMD5Code();
		m.createSHACode();
	}

	/*********************************************************************************
	 * 对称DES加密解密 DES(Data Encryption Standard)是发明最早的最广泛使用的分组对称加密算法。
	 * DES算法的入口参数有三个
	 * :Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;
	 * Mode为DES的工作方式,有两种:加密或解密。
	 */

	private String desKey = "12345678";// des加密key必须为8位长度

	public byte[] desJia() throws InvalidKeyException,
			NoSuchAlgorithmException, InvalidKeySpecException,
			NoSuchPaddingException, IllegalBlockSizeException,
			BadPaddingException {
		// 因为普通的Random是线性可预测的,安全性不高,而这个安全性比较高
		SecureRandom s = new SecureRandom();
		// 创建Deskey对象
		DESKeySpec DESKey = new DESKeySpec(desKey.getBytes());
		// 创建DES密钥工厂
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		// 使用密钥工厂来生成符合规范的密钥对象
		SecretKey secretKey = keyFactory.generateSecret(DESKey);
		// 进行实际的加密
		Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding");
		// 初始化
		c.init(Cipher.ENCRYPT_MODE, secretKey, s);
		// 开始加密
		byte[] b = c.doFinal("我是加密的内容".getBytes());
		for (byte bb : b) {
			System.out.println("" + bb);
		}
		return b;
	}

	public void desJie(byte[] neirong) throws InvalidKeyException,
			NoSuchAlgorithmException, InvalidKeySpecException,
			IllegalBlockSizeException, BadPaddingException,
			NoSuchPaddingException {
		// 因为普通的Random是线性可预测的,安全性不高,而这个安全性比较高
		SecureRandom s = new SecureRandom();
		// 创建Deskey对象
		DESKeySpec DESKey = new DESKeySpec(desKey.getBytes());
		// 创建DES密钥工厂
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		// 使用密钥工厂来生成符合规范的密钥对象
		SecretKey secretKey = keyFactory.generateSecret(DESKey);
		// 进行实际的解密
		Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding");
		// 初始化
		c.init(Cipher.DECRYPT_MODE, secretKey, s);
		// 开始解密
		byte[] hehe = c.doFinal(neirong);
		System.out.println(new String(hehe));
	}

	/*********************************************************************************************
	 * MD5消息摘要算法 MD5的典型应用是对一段Message(字节串)产生fingerprint(指纹),以防止被“篡改”。
	 * 相同的内容会产生相同的md5值
	 * 
	 */
	public void createMD5Code() throws NoSuchAlgorithmException, IOException {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		File f = new File("D:\\bb.txt");// 验教文件的md5值
		FileInputStream fileInputStream = new FileInputStream(f);
		byte[] bytes = new byte[256];
		int len = -1;
		while ((len = fileInputStream.read(bytes)) > -1) {
			md5.update(bytes, 0, len);
		}
		fileInputStream.close();
		byte[] md5Code = md5.digest();
		System.out.println("我的" + f.getName() + " md5 :"
				+ new BigInteger(1, md5Code).toString(16));
	}

	/**
	 * SHA类似MD5,不过产生的长度比md5长
	 */
	public void createSHACode() throws NoSuchAlgorithmException, IOException {
		MessageDigest md5 = MessageDigest.getInstance("SHA");
		File f = new File("D:\\bb.txt");// 验教文件的SHA值
		FileInputStream fileInputStream = new FileInputStream(f);
		byte[] bytes = new byte[256];
		int len = -1;
		while ((len = fileInputStream.read(bytes)) > -1) {
			md5.update(bytes, 0, len);
		}
		fileInputStream.close();
		byte[] md5Code = md5.digest();
		System.out.println("我的" + f.getName() + " SHA :"
				+ new BigInteger(1, md5Code).toString(16));
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-02-25 16:59  隐没  阅读(205)  评论(0编辑  收藏  举报