Java字符串异或运算

Java字符串异或运算:

public class Test {
    public static void main(String[] args) {
        String string = "yihuoyunsuan";
        String key = "key";
        String result ="";
        char cString[] = string.toCharArray();
        char cKey[] = key.toCharArray();
        int ns = cString.length;
        int nk = cKey.length;

        if(ns>nk){
            int x = ns/nk;
            int y = ns%nk;

            for(int i = 0; i < x; i++){
                for(int j = 0; j < nk; j++){
                    result = result + (char)(cString[j + i*nk]^cKey[j]);
                }
            }
            for(int j = 0; j < y; j++){
                result = result + (char)(cString[j + x*nk]^cKey[j]);
            }
        }
        else{
            for(int j = 0; j < ns; j++){
                result = result + (char)(cString[j]^cKey[j]);
            }   
        }
        System.out.println(result);;
    }
}

即可;

你可能感兴趣的:(java,String,异或)