⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycipher.java

📁 一个基于java平台的DES加密解密算法
💻 JAVA
字号:
package tywy;
import java.io.Serializable;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/*
 * Created on 2005-5-14
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MyCipher implements Serializable {
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	private SecretKey desKey = null;

	MyCipher() {
		try {
			KeyGenerator keygen = KeyGenerator.getInstance("DES");
			desKey = keygen.generateKey();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public SecretKey getSecretKey() {
		return desKey;
	}

	private String bytes2hex(byte[] bytes) {
		StringBuffer buf = new StringBuffer(bytes.length * 2);
		for (int pos = 0; pos < bytes.length; pos++) {
			int n = bytes[pos] & 0xFF;
			buf.append(halfByte2hex((n >> 4) & 0xF));
			buf.append(halfByte2hex(n & 0xF));
		}
		String rv = buf.toString();
		return rv;
	}

	private char halfByte2hex(int n) {
		//ensure: 0 <= n <= 15
		return (char) (n > 9 ? 'A' + n - 10 : '0' + n);
	}

	private int hex2halfByte(char c) {
		switch (c) {
		case '0':
			return 0;
		case '1':
			return 1;
		case '2':
			return 2;
		case '3':
			return 3;
		case '4':
			return 4;
		case '5':
			return 5;
		case '6':
			return 6;
		case '7':
			return 7;
		case '8':
			return 8;
		case '9':
			return 9;
		case 'A':
			return 10;
		case 'B':
			return 11;
		case 'C':
			return 12;
		case 'D':
			return 13;
		case 'E':
			return 14;
		case 'F':
			return 15;
		default:
			System.out.println("char out of range when decrypt");
		}
		return -1;
	}

	private byte[] hex2bytes(String str) {
		byte[] bt = new byte[str.length() / 2];
		StringBuffer buf = new StringBuffer(str.length());
		for (int i = 0; i < str.length(); i++) {
			buf.append(str.charAt(i));
		}
		for (int i = 0; i < str.length() / 2; i++) {
			int gao = -1, di = -1;
			gao = hex2halfByte(buf.charAt(2 * i));
			di = hex2halfByte(buf.charAt(2 * i + 1));
			bt[i] = (byte) ((gao << 4 | di) & 0xff);
		}
		return bt;
	}

	public String enCryptString(String clearMessage) {
		String cipherMessage = null;
		Cipher desCipher = null;
		try {
			desCipher = Cipher.getInstance("DES");
		} catch (NoSuchAlgorithmException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (NoSuchPaddingException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		try {
			//初始化cipher用来加密
			desCipher.init(Cipher.ENCRYPT_MODE, desKey);
		} catch (InvalidKeyException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			//加密
			byte[] clearText = clearMessage.getBytes();
			byte[] cipherText = desCipher.doFinal(clearText);
			cipherMessage = bytes2hex(cipherText);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cipherMessage;
	}

	public String decryptString(String cipherMessage) {
		String clearMessage = null;
		Cipher desCipher = null;
		try {
			desCipher = Cipher.getInstance("DES");
		} catch (NoSuchAlgorithmException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (NoSuchPaddingException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		try {
			//初始化cipher用来解密
			desCipher.init(Cipher.DECRYPT_MODE, desKey);
		} catch (InvalidKeyException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			byte[] cipherText = hex2bytes(cipherMessage);
			byte[] clearText = desCipher.doFinal(cipherText);
			clearMessage = new String(clearText);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clearMessage;
	}

	public static void main(String[] args) {
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -