簡易的加解密程式 -- 使用 Java

程式作品

C 語言

Java

C#

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);
  }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License