代码在MyEclipse6.0测试成功,但在8.0上无法找到import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;这两个包。
package OperateFile; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.Security; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class CipherTest { public static Key getKey(String keyBase) throws NoSuchAlgorithmException { Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1); KeyGenerator generator = KeyGenerator.getInstance("DES"); generator.init(new SecureRandom(keyBase.getBytes())); Key theKey = generator.generateKey(); return theKey; } /** * 根据提供的密鈅進行加密 * @param key 密鈅 * @param data 需要加密的數据 * @return byte[] 加密后的數据 * @throws util.EncryptException */ public static byte[] doEncrypt(String keyBase, byte[] data) { Key key = null; try { key = getKey(keyBase); } catch(NoSuchAlgorithmException e1) { e1.printStackTrace(); } try { // Get a cipher object Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // Encrypt cipher.init(Cipher.ENCRYPT_MODE, key); byte[] raw = cipher.doFinal(data); return raw; } catch(Exception e) { e.printStackTrace(); } return null; } public static String getEncString(String keyBase, String strMing) { byte[] byteMi = null; byte[] byteMing = null; String strMi = ""; BASE64Encoder base64en = new BASE64Encoder(); try { //把明文字符串放在byte數組中 byteMing = strMing.getBytes("utf8"); byteMi = doEncrypt(keyBase, byteMing);//加密 //strMi 加密后的字符串 strMi = base64en.encode(byteMi); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } finally { base64en = null; byteMing = null; byteMi = null; } return strMi; } public static String getDesString(String keyBase, String strMi) { BASE64Decoder base64de = new BASE64Decoder(); byte[] byteMing = null; byte[] byteMi = null; String strMing = ""; try { //把密文字符串放在byte數組中 byteMi = base64de.decodeBuffer(strMi); byteMing = doDecrypt(keyBase, byteMi); //strMing 解密后的字符串 strMing = new String(byteMing, "utf8"); } catch (IOException e) { e.printStackTrace(); } finally { base64de = null; byteMing = null; byteMi = null; } return strMing; } /** * 指定的密鈅解密byte[] * * @param String * keyBase 密鈅基碼 * @param byte[] * raw 待解密的數据 * @return byte[] 解密后的數据 * @throws util.EncryptException */ public static byte[] doDecrypt(String keyBase, byte[] raw) { Key key = null; try { key = getKey(keyBase); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } try { // Get a cipher object Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] data = cipher.doFinal(raw); return data; } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String data_E = getEncString("00", "lee20121220");//加密 System.out.println(data_E); String data_D = getDesString("00", data_E);//解密 System.out.println(data_D); } }
----------------------------
package operateFile; public class DocodeAction { public static String decode(String str) { int len = str.length(); String tmp1 = ""; String tmp2 = ""; if ((len % 2) == 0) { tmp1 = str.substring(0, len / 2); tmp2 = str.substring(len / 2, len); } else { tmp1 = str.substring(0, (len / 2) + 1); tmp2 = str.substring((len / 2) + 1, len); } str = ""; for (int i = 0; i < tmp2.length(); i++) { str += tmp1.substring(i, i + 1) + tmp2.substring(i, i + 1); } if ((len % 2) != 0) str += tmp1.substring(tmp1.length() - 1, tmp1.length()); str = repairStr(str); return str; } public static String encode(String str) { str = changeStr(str); int len = str.length(); String tmp1 = ""; String tmp2 = ""; for (int i = 0; i < len; i++) { if ((i % 2) == 0) tmp1 += str.charAt(i); else tmp2 += str.charAt(i); } str = ""; str = tmp1 + tmp2; return str; } public static String changeStr(String str) { str = str.replace("a", "ㄅ"); str = str.replace("b", "ㄆ"); str = str.replace("c", "ㄇ"); str = str.replace("d", "ㄈ"); str = str.replace("e", "ㄉ"); str = str.replace("f", "ㄊ"); str = str.replace("g", "ㄋ"); str = str.replace("h", "ㄌ"); str = str.replace("i", "ㄍ"); str = str.replace("j", "ㄎ"); str = str.replace("k", "ㄏ"); str = str.replace("l", "ㄐ"); str = str.replace("m", "ㄑ"); str = str.replace("n", "ㄒ"); str = str.replace("o", "ㄓ"); str = str.replace("p", "ㄔ"); str = str.replace("q", "ㄕ"); str = str.replace("r", "ㄖ"); str = str.replace("s", "ㄗ"); str = str.replace("t", "ㄘ"); str = str.replace("u", "ㄙ"); str = str.replace("v", "ㄚ"); str = str.replace("w", "ㄛ"); str = str.replace("x", "ㄜ"); str = str.replace("y", "ㄝ"); str = str.replace("z", "ㄞ"); str = str.replace("//", "ㄟ"); str = str.replace("1", "ㄠ"); str = str.replace("2", "ㄣ"); str = str.replace("3", "ㄡ"); str = str.replace("4", "ㄤ"); str = str.replace("5", "ㄥ"); str = str.replace("6", "ㄧ"); str = str.replace("7", "ㄨ"); str = str.replace("8", "ㄩ"); str = str.replace("9", "ㄢ"); str = str.replace("0", "ㄦ"); str = str.replace(":", "ˇ"); return str; } public static String repairStr(String str) { str = str.replace("ㄅ", "a"); str = str.replace("ㄆ", "b"); str = str.replace("ㄇ", "c"); str = str.replace("ㄈ", "d"); str = str.replace("ㄉ", "e"); str = str.replace("ㄊ", "f"); str = str.replace("ㄋ", "g"); str = str.replace("ㄌ", "h"); str = str.replace("ㄍ", "i"); str = str.replace("ㄎ", "j"); str = str.replace("ㄏ", "k"); str = str.replace("ㄐ", "l"); str = str.replace("ㄑ", "m"); str = str.replace("ㄒ", "n"); str = str.replace("ㄓ", "o"); str = str.replace("ㄔ", "p"); str = str.replace("ㄕ", "q"); str = str.replace("ㄖ", "r"); str = str.replace("ㄗ", "s"); str = str.replace("ㄘ", "t"); str = str.replace("ㄙ", "u"); str = str.replace("ㄚ", "v"); str = str.replace("ㄛ", "w"); str = str.replace("ㄜ", "x"); str = str.replace("ㄝ", "y"); str = str.replace("ㄞ", "z"); str = str.replace("ㄟ", "//"); str = str.replace("ㄠ", "1"); str = str.replace("ㄣ", "2"); str = str.replace("ㄡ", "3"); str = str.replace("ㄤ", "4"); str = str.replace("ㄥ", "5"); str = str.replace("ㄧ", "6"); str = str.replace("ㄨ", "7"); str = str.replace("ㄩ", "8"); str = str.replace("ㄢ", "9"); str = str.replace("ㄦ", "0"); str = str.replace("ˇ", ":"); return str; } public static void main(String [] args) { String username = "scylla"; DocodeAction da = new DocodeAction(); String result = da.encode(username); System.out.println(result); } }