簡易的加解密程式 -- 使用 Java
程式作品C 語言JavaC#JavaScript常用函數文字處理遊戲程式衛星定位系統程式資料結構網路程式自然語言人工智慧機率統計資訊安全等待完成訊息相關網站參考文獻最新修改簡體版English |
package ccc; import java.util.*; public class Cryptography { public static void main(String[] args) { try { String text = STR.file2text("Cryptography\\encoderInput.txt").toLowerCase(); String modEncodeText = Cryptography.ModEncode(text, 3); String modDecodeText = Cryptography.ModDecode(modEncodeText, 3); System.out.println("modEncodeText="+modEncodeText+"\n"); System.out.println("modDecodeText="+modDecodeText+"\n"); STR.text2file(modEncodeText, "Cryptography\\encoderOutputMod.txt"); String xorEncodeText = Cryptography.XorEncode(text, 'x'); String xorDecodeText = Cryptography.XorDecode(xorEncodeText, 'x'); System.out.println("xorEncodeText="+xorEncodeText+"\n"); System.out.println("xorDecodeText="+xorDecodeText+"\n"); STR.text2file(xorEncodeText, "Cryptography\\encoderOutputXor.txt"); TreeMap map = new TreeMap(), revMap = new TreeMap(); Cryptography.loadMap(map, revMap, "Cryptography\\encoderMap.txt"); System.out.println("map="+map); System.out.println("revMap="+revMap); String mapEncodeText = Cryptography.MapEncode(text, map); String mapDecodeText = Cryptography.MapDecode(mapEncodeText, revMap); System.out.println("mapEncodeText="+mapEncodeText+"\n"); System.out.println("mapDecodeText="+mapDecodeText+"\n"); STR.text2file(mapEncodeText, "Cryptography\\encoderOutputMap.txt"); } catch (Exception e) { e.printStackTrace(); System.out.println("error : "+e.getClass().getName()); } } public static String ModEncode(String inStr, int k) { StringBuffer outStr=new StringBuffer(); for (int i=0; i<inStr.length(); i++) { char ch = inStr.charAt(i); char rzCh; if (ch >='a' && ch <='z') { int num = (ch- (int) 'a' + k + 26) % 26 + (int) 'a'; rzCh = (char) num; } else rzCh = ch; outStr.append(rzCh); } return outStr.toString(); } public static String ModDecode(String inStr, int k) { return ModEncode(inStr, -1*k); } public static String XorEncode(String inStr, char key) { StringBuffer outStr=new StringBuffer(); for (int i=0; i<inStr.length(); i++) { char ch = inStr.charAt(i); char rzCh = (char) (ch ^ key); outStr.append(rzCh); } return outStr.toString(); } public static String XorDecode(String inStr, char key) { return XorEncode(inStr, key); } public static void loadMap(Map map, Map revMap, String fileName) throws Exception { String text = STR.file2text(fileName); System.out.println("text="+text); String[] lines = text.split("\n"); System.out.println("lines="+lines); for (int i=0; i<lines.length; i++) { String head = STR.head(lines[i], "="); String tail = STR.tail(lines[i], "="); map.put(head, tail); revMap.put(tail, head); } } public static String MapEncode(String inStr, Map map) { StringBuffer outStr=new StringBuffer(); for (int i=0; i<inStr.length(); i++) { char ch[] = new char[1]; ch[0] = inStr.charAt(i); String chStr = new String(ch); String rzChStr = (String) map.get(chStr); char rzCh = inStr.charAt(i); if (rzChStr != null) rzCh = rzChStr.charAt(0); outStr.append(rzCh); } return outStr.toString(); } public static String MapDecode(String inStr, Map revMap) { return MapEncode(inStr, revMap); } } |
page revision: 0, last edited: 04 Nov 2010 02:44
Post preview:
Close preview