package text; import java.util.Scanner; public class text { public static String myrc4(String aInput,String aKey){ int[] iS = new int[256]; byte[] iK = new byte[256]; for(int i = 0;i<256;i++){ iS[i] = i; } for(int i = 0;i<256;i++){ iK[i] = (byte)aKey.charAt(i%aKey.length()); } int j = 0; for(int i = 0;i < 256;i++){ j = (j + iS[i] + iK[i]) % 256; int temp = iS[i]; iS[i] = iS[j]; iS[j] = temp; } j =0; int i = 0; char[] iInputChar = aInput.toCharArray(); char[] iOutputChar = new char[iInputChar.length]; for(int x = 0;x < aInput.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 void main(String[] args){ String str1 = myrc4("20200809030229徐仕华","jkhh"); System.out.println(str1); String str2 = myrc4(str1,"jkhh"); System.out.println(str2); } }注释
时间: 2024-04-25 09:27:06 浏览: 76
该程序实现了一个自定义的RC4加解密算法。其中,myrc4方法接受两个参数,分别是需要加密或解密的字符串和密钥,返回加密或解密后的结果。main方法中调用了myrc4方法,对字符串"20200809030229徐仕华"进行加密,并将加密后的结果输出。接着,将加密后的结果作为参数,再次调用myrc4方法,进行解密,并输出解密后的结果。这样就可以验证加解密算法是否正确。
阅读全文