Pages

2011年9月7日 星期三

使用Triple DES 加密字串

最近因為有需求,需要使用加密的方法來讓連線的帳號密碼用成亂碼來連線。
因此找到了使用 Tripple DES的方式加密字串。

1. 首先,先寄建立Decrypter與Encrypter
package security.crypto;

import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;

public classDecrypter {
	Cipher decipher;

	public Decrypter(SecretKey key) {
		try {
			decipher = Cipher.getInstance("DESede");
			decipher.init(Cipher.DECRYPT_MODE, key);

		} catch (javax.crypto.NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (java.security.InvalidKeyException e) {
			e.printStackTrace();
		}
	}

	public String decrypt(String str) {
		try {
			byte[] decipher1 = new sun.misc.BASE64Decoder().decodeBuffer(str);

			byte[] decryptedBytes = decipher.doFinal(decipher1);

			return new String(decryptedBytes, "UTF8");
		} catch (javax.crypto.BadPaddingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (java.io.IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

package security.crypto;
import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;

import sun.misc.BASE64Encoder;

public class Encrypter {
	private Cipher encipher;

	public Encrypter(SecretKey key) {
		try {
			encipher = Cipher.getInstance("DESede");
			encipher.init(Cipher.ENCRYPT_MODE, key);

		} catch (javax.crypto.NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (java.security.InvalidKeyException e) {
			e.printStackTrace();
		}
	}

	public String encrypt(String str) {
		try {
			byte[] encipher1 = str.getBytes("UTF8");

			byte[] encrpyedBytes = encipher.doFinal(encipher1);

			return new BASE64Encoder().encode(encrpyedBytes);
		} catch (javax.crypto.BadPaddingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
}

2. 接著建立使用該方法的連接介面
package security;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import wn.security.crypto.TripleDESDecrypter;
import wn.security.crypto.TripleDESEncrypter;

/**
 * 
 * @author asqqsa
 * 
 *         Here is a basic implementation of encrpytion and decryption using TripleDES.
 *         
 */

public class DES{

	public String getDecryptoString(String string) {
		String decrypted = null;
		try {
			Decrypter decrypter = new Decrypter(getKey());
			decrypted = decrypter.decrypt(string);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return decrypted;
	}
	
	public String getEncryptoString(String string) {
		String encrypted = null;
		try {
			Encrypter encrypter = new Encrypter(getKey());
			encrypted = encrypter.encrypt(string);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encrypted;
	}

	private SecretKey getKey() {
 		// there are 32 chars
		String keyString ="1234567890abcdefghijklmnopqrstuv";
		byte[] keyB = new byte[24]; // a Triple DES key is a byte[24] array
		for (int i = 0; i < keyString.length() && i < keyB.length; i++) {
			keyB[i] = (byte) keyString.charAt(i);
		}
		SecretKey key = new SecretKeySpec(keyB, "DESede");
		return key;
	}
    /*****Random Generate Key Vlaue***
		public SecretKey getKey() {
		    try {
 		        return KeyGenerator.getInstance("DESede").generateKey();
 		    } catch (NoSuchAlgorithmException e) {
		        e.printStackTrace();
		    }
        	    return null;
		}
    **/
}
3. 建立JSP介面測試
<%@ page import="security.DES" %>
<%@ page contentType="text/html;charset=big5" %> 
<html>
<head></head>
<body>
<%
	DESCrypto des = new DESCrypto();
	String plain = null;
	String encrp = null;
	encrp = request.getParameter("plaintext");
	plain = request.getParameter("secrettext");
%>
<form method='post' name='des' action='Crypto.jsp'>
<table border = 1>
<tr><td colspan='2'>字串Triple DES 使用單一Key 加解密</td> </tr>
<tr>
<td>plaintext encrypto</td>
<td>secrettext decrypto</td>
</tr>
<tr>
<td><input name='plaintext' type='text'></inpute></td>
<td><input name='secrettext' type='text'></inpute></td>
</tr>
<tr>
<td><%= (encrp == null || encrp.equals(""))? " " : des.getEncryptoString(encrp)%></td>
<td><%= (plain == null || plain.equals(""))? " ": des.getDecryptoString(plain)%></td>
</tr>
<tr>
<td colspan='2'><input  type='submit' value='submit'></inpute></td>
</tr>
</table>
</form>
</body>
</html>

0 意見: