两个Java加密解密例子

java 加密解密 示例: http://rian.iteye.com/blog/196031

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

public class Demo {
	public static void main(String args[]) {
		String password = "administrator";
		try {
			Demo.PrivEncrypt(password,"t.obj");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param oldStr
	 *            要加密的串
	 * @param keyFile
	 *            私有key所在的文件路径
	 * @throws Exception
	 */
	public static void PrivEncrypt(String oldStr,String keyFile) throws Exception {
		System.out.println("原始:"+oldStr);
		byte[] plainText = oldStr.getBytes("GBK");
		Key key = getPriveKey(keyFile);
		// 获得一个私鈅加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		
		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"));
	}

	// 把Key 写到文件中:生成一个私有Key对象,保存在文件中
	public static void setPriveKey(String file) {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			KeyGenerator keyGen = KeyGenerator.getInstance("DES");
			keyGen.init(128);
			Key key = keyGen.generateKey();// 生成私钥Key
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(key);
			oos.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	// 从文件中读Key:客户端可以根据管理员提供的私钥Key文件提取Key对象
	public static Key getPriveKey(String file) throws Exception {
		FileInputStream fis = new FileInputStream(file);
		ObjectInputStream ois = new ObjectInputStream(fis);
		Key key = (Key) ois.readObject();
		return key;
	}
}
/* 运行结果:
 * 原始:administrator 
 * 加密:!Y????I?? ??R;?
 * 解密:administrator
 */




DES加密、解密字符串算法: http://www.icnote.com/des-encrypt/
package com.common;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;

public class DESPlus {
	private static String strDefaultKey = "national";
	private Cipher encryptCipher = null;
	private Cipher decryptCipher = null;

	public static String byteArr2HexStr(byte[] arrB) throws Exception {
		int iLen = arrB.length;
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = arrB[i];
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString();
	}

	public static byte[] hexStr2ByteArr(String strIn) throws Exception {
		byte[] arrB = strIn.getBytes();
		int iLen = arrB.length;

		byte[] arrOut = new byte[iLen / 2];
		for (int i = 0; i < iLen; i = i + 2) {
			String strTmp = new String(arrB, i, 2);
			arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
		}
		return arrOut;
	}

	public DESPlus() throws Exception {
		this(strDefaultKey);
	}

	public DESPlus(String strKey) throws Exception {
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		Key key = getKey(strKey.getBytes());

		encryptCipher = Cipher.getInstance("DES");
		encryptCipher.init(Cipher.ENCRYPT_MODE, key);

		decryptCipher = Cipher.getInstance("DES");
		decryptCipher.init(Cipher.DECRYPT_MODE, key);
	}
	public byte[] encrypt(byte[] arrB) throws Exception {
		return encryptCipher.doFinal(arrB);
	}
	public String encrypt(String strIn) throws Exception {
		return byteArr2HexStr(encrypt(strIn.getBytes()));
	}
	public byte[] decrypt(byte[] arrB) throws Exception {
		return decryptCipher.doFinal(arrB);
	}
	public String decrypt(String strIn) throws Exception {
		return new String(decrypt(hexStr2ByteArr(strIn)));
	}
	private Key getKey(byte[] arrBTmp) throws Exception {
		byte[] arrB = new byte[8];

		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}
		Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
		return key;
	}

	public static void main(String[] args) {
		try {
			String test = "Hellow Word!";
			 DESPlus des = new DESPlus("6b189a916ce2477fd55dd80903836156");
			 System.out.println("str=:" + test);
			 System.out.println("encrypt str=:"+des.encrypt(test));
			 System.out.println("decrypt str=:"+des.decrypt(des.encrypt(test)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

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