在java中实现A5 / 1算法没有给出任何输出

时间:2016-09-22 08:20:03

标签: java algorithm encryption stream-cipher

以下代码未提供任何解密输出

样品:

输入要加密的字符串:
你好

输入64位密钥
0000000000111111111100000000001111111111000000000011111111110101

加密消息是:
00101110100000001001011111001101101

解密的消息是:

有人能说出代码有什么问题吗?

    import java.io.*;

    class A51 {

        public static void main(String args[])throws Exception {

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Enter the string to be encrypted:");
            String msg = br.readLine();

            byte[] st = null;
            st = msg.getBytes("UTF-8");

            msg = "";

            for(byte b:st) {
                msg = msg + Integer.toBinaryString(b);
            }

            int len = msg.length();
            int s[] = new int[len];

            System.out.println("\nEnter the 64 bit key");

            int x[] = new int[19];
            int y[] = new int[22];
            int z[] = new int[23];
            int i;

            String str[] = br.readLine().split("");

            for(i=0; i<19; i++)
                x[i] = Integer.parseInt(str[i]);

            for(i=0; i<22; i++)
                y[i] = Integer.parseInt(str[i+19]);

            for(i=0; i<23; i++)
                z[i] = Integer.parseInt(str[i+19+22]);

            int m = 0, t;

            for(int j=0; j<len; j++) {

                if(x[8]==y[10] || x[8]==z[10])
                    m = x[8];
                else if(y[10]==z[10])
                    m = y[10];

                if(x[8]==m) {
                    t = x[13]^x[16]^x[17]^x[18];
                    for(i=18; i>0; i--)
                        x[i] = x[i-1];
                    x[0] = t;
                }

                if(y[10]==m) {
                    t = y[20]^y[21];
                    for(i=21; i>0; i--)
                        y[i] = y[i-1];
                    y[0] = t;
                }

                if(z[10]==m) {
                    t = z[7]^z[20]^z[21]^z[22];
                    for(i=22; i>0; i--)
                        z[i] = z[i-1];
                    z[0] = t;
                }

                s[j] = x[18]^y[21]^z[22];  
            }

            int enc[] = new int[len];
            int dec[] = new int[len];

            System.out.println("\nThe encrypted message is:");
            for(i=0; i<len; i++) {
                enc[i] = Integer.parseInt(Character.toString(msg.charAt(i)))^s[i];
                System.out.print(enc[i]);
            }

            msg = "";

            System.out.println("\n\nThe decrypted message is:");
            for(i=0; i<len; i++) {
                msg = msg + dec[i];
                if(i%7==6) {
                    int decp = Integer.parseInt(msg, 2);
                    System.out.print((char)decp);
                    msg = "";
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

数组dec[]为空

int dec[] = new int[len];

//Add values to dec

for(i=0; i<len; i++) {
                msg = msg + dec[i];
                if(i%7==6) {
                    int decp = Integer.parseInt(msg, 2);
                    System.out.print((char)decp);
                    msg = "";
                }
            }

您需要在循环

之前向dec[]添加值