關於部落格
Android‧教學分享‧娛樂共享
  • 67902

    累積人氣

  • 0

    今日人氣

    0

    追蹤人氣

加密/解密

import java.util.Random; public class GetPassWord { public static void main(String[] args) { // TODO code application logic here int a; int count=10; while (count>0) { a = (int) (Math.random() * 4 + 1); String inputStr = getNewPassword(); System.out.println("產生的文字:" + inputStr); String key1 = "abcdef" + "123456789a"; String key2 = "abcdeg" + "123456789b"; String key3 = "abcdeh" + "123456789c"; String key4 = "abcdei" + "123456789d"; if (a == 1) { System.out.println("now use key1"); String str = HloveyRC4(inputStr, key1); System.out.println("加密:"+str); System.out.println("解密:"+HloveyRC4(str, key1)); } else if (a == 2) { System.out.println("now use key2"); String str = HloveyRC4(inputStr, key2); System.out.println("加密:"+str); System.out.println("解密:"+HloveyRC4(str, key2)); } else if (a == 3) { System.out.println("now use key3"); String str = HloveyRC4(inputStr, key3); System.out.println("加密:"+str); System.out.println("解密:"+HloveyRC4(str, key3)); } else if (a == 4) { System.out.println("now use key4"); String str = HloveyRC4(inputStr, key4); System.out.println("加密:"+str); System.out.println("解密:"+HloveyRC4(str, key4)); } count--; System.out.println("==================================="); } } public static String HloveyRC4(String aInput, String aKey) { int[] iS = new int[256]; byte[] iK = new byte[256]; for (int i = 0; i < 256; i++) { iS[i] = i; } int j = 1; for (short i = 0; i < 256; i++) { iK[i] = (byte) aKey.charAt((i % aKey.length())); } j = 0; for (int i = 0; i < 255; i++) { j = (j + iS[i] + iK[i]) % 256; int temp = iS[i]; iS[i] = iS[j]; iS[j] = temp; } int i = 0; j = 0; char[] iInputChar = aInput.toCharArray(); char[] iOutputChar = new char[iInputChar.length]; for (short x = 0; x < iInputChar.length; x++) { i = (i + 1) % 256; j = (j + iS[i]) % 256; int temp = iS[i]; iS[i] = iS[j]; iS[j] = temp; int t = (iS[i] + (iS[j] % 256)) % 256; int iY = iS[t]; char iCY = (char) iY; iOutputChar[x] = (char) (iInputChar[x] ^ iCY); } return new String(iOutputChar); } public static String getNewPassword() { String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; int size = str.length(); StringBuffer sb = new StringBuffer(); Random rad = new Random(); for (int i = 1; i < 9; i++) { int start = rad.nextInt(size); String tmp = str.substring(start, start + 1); sb.append(tmp); } String password = sb.toString(); return password; } }
相簿設定
標籤設定
相簿狀態