日期:2014-05-20 浏览次数:20632 次
public class Again { private String plaintext; private String ciphertext; byte[] K = {0,7,0,9,0,3,0,1,2,2}; byte k; private int[] S; private int[] T; public Again(){ S = new int[256]; T = new int[256]; } public void initST(){ int size = this.K.length; for(int i = 0;i<256;i++){ S[i] = i; T[i] = K[i % size]; } } public void permS(){ int j = 0; for(int i = 0;i < 256;i++){ j = (j + S[i] + T[i]) % 256; int temp; temp = S[j]; S[j] = S[i]; S[i] = temp; } } public void encryp(){ int t,i = 0,j = 0; byte b[] = this.plaintext.getBytes(); byte z[] = new byte[b.length]; for(int m = 0;m < b.length; m++){ i = (i + 1) % 256; j = (j + 1) % 256; int temp; temp = S[j]; S[j] = S[i]; S[i] = temp; t = (S[i] + S[j]) % 256; System.out.println(); k = (byte) S[t]; z[m] = (byte) ((byte)b[m]^k); } this.ciphertext = new String(z); } public static void main(String[] args){ Again a = new Again(); a.plaintext = "你们好啊"; a.initST(); a.permS(); a.encryp(); System.out.println(a.plaintext); System.out.println(a.ciphertext); } }