testsafer.java

来自「jpeg2000编解码」· Java 代码 · 共 790 行 · 第 1/3 页

JAVA
790
字号
  136, 213,  53,   6,  47, 224, 109, 160,   219,  86,  60, 145, 172, 116,  48, 107,  176,  32,   6,  89,  39, 152, 171, 143,    17, 161, 187,  95, 105, 135, 164,   1,  211,  29, 246,  81,  22,  51, 105,  79,    58,  56,  54, 193, 174,  28, 140, 184,  244,  40, 192,  18, 185,  19,  13, 176,   160, 217,  42,  37, 241, 100,  49,  80,   92, 189, 119, 176,  54,  38, 254,  59,    50, 146,  73, 211, 147, 199,  33, 182,   36, 245, 251, 101, 134, 120, 241, 150,   114, 250, 222,  38,  78, 201, 243, 140,  199, 196, 185,  46, 222,  24,  66, 229,   232, 251,  77, 219, 221,  32, 101,  22,   19, 191, 242, 109,  41, 207, 125, 223,   103, 135,  55, 167, 240, 202, 224,  23,   39, 208,   7,  63, 206, 207, 109,  91,    99,  43,  38,  87,   0, 115,  25,   0,   93,  30,  36, 147, 239, 236,  50, 109,   120,  18, 104, 132, 241,  85,  45, 194,    };        out.println("\nSAFER in ECB mode: " +            "safer -e -ecb -kx 00000000000000000102030405060708 -s -r 10\n");        byte[] output = new byte[data.length];        for (int i = 0; i < data.length; i++)            output[i] = (byte)data[i];        Cipher alg = Cipher.getInstance("SAFER", "Cryptix");//        ((Parameterized) alg).setParameter("variant", "SK-128"); // this is the default variant//        ((Parameterized) alg).setParameter("rounds", new Integer(10)); // this is also the default        byte[] b = new byte[16];        for (int i = 0; i < 8; i++) { b[i + 8] = (byte)(i + 1);  b[i] = (byte)0; }        RawSecretKey key = new RawSecretKey("SAFER", b);                    alg.initEncrypt(key);        compareIt(alg.crypt(input), output);    }    // The next two tests appear to be "options" only, they should probably be included here.    //    // This is using a text key - how to interpret extra chars?    //    safer -e -ecb -k AaBcDeFgHiJkLmNoPqRsTuVwXyZz0123456789    //    private void test8D() throws Exception {        int[] data = {  205,  12,  46, 193, 207, 240, 215,  91,    13, 210,  52,   6,  71, 172,   3, 254,  205,  12,  46, 193, 207, 240, 215,  91,    13, 210,  52,   6,  71, 172,   3, 254,   96,  29,  39,  40,  55, 146, 237, 123,   163, 222, 184, 162, 239,  94,  48, 227,  228, 222, 185, 200,   2, 157, 249, 164,    68,  39, 224, 119,  89,  33,  96,   7,  242, 117, 142,  64,  57, 232, 204, 149,   190, 247, 201, 153,  52,  49,   3,  63,  155, 237,  13,  46, 120, 238, 140, 219,   122,  30, 255,  85,  78,  61,  10,  48,  168, 115,  66, 152, 102,  60, 154, 227,   107,  14, 152,  53, 133, 152,  68, 139,   83, 200,  77,  30, 227,  67,  72, 211,   164, 177,  85, 204, 155, 172, 146,  35,    8, 193, 190,  95, 239,  98, 140,  95,   233,  65,  18, 184, 202, 230, 205,  91,  255, 104, 241,   3, 226, 173, 125,   6,   244, 176,  44,  67, 248, 195,  43, 191,  124,  41, 157,  69, 145,  16, 177, 172,   169, 234, 191, 227,  52, 188, 217, 124,  141, 132,  66, 147, 148, 208, 139,  53,    74,  34, 190, 164, 218, 124, 123, 229,  208, 105, 140,  21,  81,  68,  50,  58,   125, 159, 156,  47, 200, 186, 151,  23,  193, 147, 212, 182,  33, 100, 119, 187,    84, 210, 239, 222, 193, 230, 145,  53,  240, 212, 114, 130, 109, 253, 150, 142,   230, 219, 160, 207,  73, 147, 129,  21,   54, 163, 136, 104,  15, 147,  61, 237,    73,  70, 215,  57,  52, 209, 173, 244,   14,   9,  56,  52,  95,  76,  40,  70,   148,  21, 166,  15,  66, 204,  46, 196,   68, 243, 154,  48,  91, 149, 142,  20,   118,  65,   1, 124,  83,  86, 111,  82,    };        out.println("\nSAFER in ECB mode: " +            "safer -e -ecb -k AaBcDeFgHiJkLmNoPqRsTuVwXyZz0123456789\n");        byte[] output = new byte[data.length];        for (int i = 0; i < data.length; i++)            output[i] = (byte)data[i];        Cipher alg = Cipher.getInstance("SAFER", "Cryptix");        ((Parameterized) alg).setParameter("variant", "K-128");            // default rounds is 10 for K-128        byte[] b = new byte[16];//how to interpret key?//for (int i = 0; i < 8; i++) { b[i + 8] = (byte)(i + 1);  b[i] = (byte)0; }        RawSecretKey key = new RawSecretKey("SAFER", b);                    alg.initEncrypt(key);        compareIt(alg.crypt(input), output);    }    // This is just combining the options.    //    safer -ecbkx 42431BA40D291F81D66083C605D3A4D6    //    private void test8E() throws Exception {        int[] data = {  249, 118, 152, 143, 221,  25, 156, 132,    38,  89, 129, 249, 212, 217,  39,  66,  249, 118, 152, 143, 221,  25, 156, 132,    38,  89, 129, 249, 212, 217,  39,  66,  212, 160,  72, 109, 162, 244,  13,   6,   237, 228, 143, 223, 216, 238, 188, 151,  149, 236, 141, 238, 223, 191,  16, 133,   208, 169, 126,  87, 136,  68,  18,  19,  235, 112, 249,  81,  19,  74, 153,  74,   133, 255, 187, 201,  93,  49,   1, 135,  116, 231, 223, 137, 117, 184, 130, 120,   141, 191, 176, 154,  87,  91,  77,  77,  191,  70, 198, 162,  67, 234,  95,  57,    12,  74, 134,   3, 179,  67,  40,   1,  138,  85,  70, 107, 230, 140, 199, 224,    16,   6,  76, 150, 237, 122,  76, 178,  137, 186,  54,  55,  35,  38, 189, 191,   104, 123,  16,  46, 237,  93, 192, 191,  127, 251, 160,   7,  80, 103,  86, 226,    82, 145,  61,  28,  69,  49,  59, 239,  153,  40,  25, 164, 175, 163, 103,  54,   157, 181,  57, 211, 173,  10, 254, 209,  131,  21, 240, 174, 155, 170, 193, 252,    27,  46, 156, 207, 204,  38,  69,  60,   84, 241, 118, 129, 107, 181,  76,  70,   166, 233, 136,  85, 193, 118, 154, 207,   19, 129, 201, 152,  80, 235, 240, 180,   220, 151, 183, 196, 193,  98, 104, 193,  111, 215,  58, 139, 250,  52, 147,  22,   121, 231,  59, 251, 121,   1,  29, 148,  246, 143, 211, 211, 199, 202, 195,  13,    53, 225, 142,   6,  75,  43, 227,  74,  151,  96, 118, 187, 166,  56,   3,  15,   164, 176,  21, 134,  77, 114, 247,  69,  159, 143, 156, 109, 116, 149,  75, 109,   138,  83,  97,  21,  95,  62,  45,  41,    };        out.println("\nSAFER in ECB mode: " +            "safer -ecbkx 42431BA40D291F81D66083C605D3A4D6\n");        byte[] output = new byte[data.length];        for (int i = 0; i < data.length; i++)            output[i] = (byte)data[i];        Cipher alg = Cipher.getInstance("SAFER", "Cryptix");        ((Parameterized) alg).setParameter("variant", "K-128");            // default rounds is 10 for K-128//      String kk = "42431BA40D291F81D66083C605D3A4D6";//      RawSecretKey key = new RawSecretKey("SAFER", Hex.fromString(kk));        alg.initEncrypt(aKey);        compareIt(alg.crypt(input), output);    }//............................... MODE TESTS ................................//// Mode tests are not strictly necessary.// The next test in <code>check.ref</code> is test #11, further down.// It is a (CBC) mode test://    safer -cbckx 42431BA40D291F81D66083C605D3A4D6:74536EBDC211484A//...........................................................................    private void test9() throws Exception {        int[] data = {  251, 221, 148,   3, 222,  99, 246, 105,     9, 253, 180,  32,  22,  75,  11, 213,   56, 232, 236, 153, 153,  92, 253,  97,    39,  54,  14,  69,  23,  95, 212,  37,  161,   9,  26, 224, 245, 215,  57, 210,    53, 131,  63, 111,  94, 178,  65, 235,  183,  88, 244,  42,  86,   6, 154, 213,   111,  59, 179,  11,   4,   4,  39, 199,    8, 190, 114, 105, 117, 168, 126, 106,   226, 247,  84, 100, 219, 249,  30,  18,  250,  82, 221,  11, 124, 218, 156,  27,   108,  72,  43, 222, 132, 148,  37, 177,  185, 197, 166,  74,  41,  88, 247, 169,    14, 163,  26, 178,  79,  15,  49, 112,  129, 171,  54, 147, 224,  78, 181,  50,   221, 191, 166, 224, 235,  73, 160, 113,  170, 172, 188,  19,  25, 154, 114, 144,   158,  64, 185,  30, 243, 120, 184, 193,  128, 159, 136, 128,  49, 166,  81,  75,   166, 120,  95, 116, 225,  37, 228,  62,  245,  30,  89, 115,  23, 205, 247, 137,   197, 194, 211,  91, 115, 241, 191,  93,  113, 126, 232, 106, 118,  95,  62, 206,    69, 102,  42, 176, 155, 102, 166,  27,   82,   6, 174,  34, 144, 203,  93, 255,    57, 197,   9,  64, 138, 225, 204, 151,  214,   0, 123, 225,  85, 191, 233,  18,   176, 217, 192, 128, 162, 213,  16, 212,  101,  97, 100, 135, 194, 186,  79, 105,   216,  70, 152,  40, 134, 217,  76, 115,   65,  89, 202,  13, 123, 181,  94,  35,    38, 253, 153, 104, 137, 244, 208, 102,   94, 142,  58, 219, 141, 132, 166, 136,   174, 247,  99,  31,  50,  74,  68,  79,  106,  89,  51,  71,  70,   6,  84,  69,   139, 229,  78, 232, 188, 118,  26,  17    };        out.println("\nSAFER in CFB mode: " +            "safer -cfbkx 42431BA40D291F81D66083C605D3A4D6:74536EBDC211484A\n");        byte[] output = new byte[data.length];        for (int i = 0; i < data.length; i++)            output[i] = (byte)data[i];        Cipher alg = Cipher.getInstance("SAFER/CFB", "Cryptix");        ((Parameterized) alg).setParameter("variant", "K-128");        ((FeedbackCipher) alg).setInitializationVector(anIV);        alg.initEncrypt(aKey);        compareIt(alg.crypt(input), output);        out.println("\nDecrypting input and checking it against output:\n");        alg.initDecrypt(aKey);        compareIt(alg.crypt(output), input);    }        private void test10() throws Exception {        int[] data = {  251, 221, 148,   3, 222,  99, 246, 105,     9, 253, 180,  32,  22,  75,  11, 213,  162,  53, 126,  83, 219,  33,  48, 193,    63, 143,  14, 194,  20,  93, 226,  67,   66,  17, 114,  26, 204, 190, 221, 173,   181,  32, 143, 213,  75,  56, 106, 155,   19,  68, 122, 230, 144,  69,  36, 218,   207,  25,  28,  90, 228, 113,   4, 170,  201,  65, 206,  75, 122,  23,  55, 252,    77, 162,   4, 254, 172,  25, 129, 238,  230,  17,  91, 175,  92, 252, 132, 126,   252, 174, 150, 132, 148, 118,  43, 206,   36,  16, 155,  58, 130,  46, 173,  74,    88, 230, 128, 171, 154, 183,  67, 235,   75,  70, 180, 110, 170, 220, 206, 193,   237, 221, 214,  64, 157, 118, 200,   3,   31, 200,   1, 251,   5,  54, 135,  22,    51, 245, 132, 202, 244, 202,  40,  39,   98, 110, 244, 198,  96,  90,  76, 174,    53, 128, 217, 224,  53,  69, 118,  51,  230, 117,  36, 211, 166,  41, 143, 183,   195, 155,  46,  49, 203,  54,  88,  70,  216, 185, 212, 220, 122,  45,  93,  43,   167,  67, 131,  82,   7, 104,  18,  90,  238,  97, 204, 240,  42, 153, 157, 105,   194,  77,  87, 247,  54, 210, 142,  56,   31, 191,  59, 182,  26, 200,  38,  91,    76,  22, 100, 209,   2, 135,  77, 156,  231, 128,  81, 241,  44, 218,   8, 227,    50, 236, 152, 128,  25, 206,  82,  62,  105,  57,  67, 153,  42,  57, 105, 198,   135,  68,  17,  54,  73, 216, 136,   9,   91,  85, 103, 146, 107,  37, 224,   1,   238, 149,  31, 133, 147, 138, 233,  61,  187, 120, 162,   6, 230,  35,  39,  75,   169, 142, 185, 209, 195, 141,  66, 198    };        out.println("\nSAFER in OFB mode: " +            "safer -ofbkx 42431BA40D291F81D66083C605D3A4D6:74536EBDC211484A\n");        byte[] output = new byte[data.length];        for (int i = 0; i < data.length; i++)            output[i] = (byte)data[i];        Cipher alg = Cipher.getInstance("SAFER/OFB", "Cryptix");        ((Parameterized) alg).setParameter("variant", "K-128");        ((FeedbackCipher) alg).setInitializationVector(anIV);        alg.initEncrypt(aKey);        compareIt(alg.crypt(input), output);                out.println("\nDecrypting input and checking it against output:\n");        alg.initDecrypt(aKey);        compareIt(alg.crypt(output), input);    }        private void test11() throws Exception {        int[] data = {  251, 221, 148,   3, 222,  99, 246, 105,   226,  42,  71,  84,  68,  15,  77, 253,  159, 242, 171, 246, 112,  85,   1,  17,   201, 140,  36,  25, 241, 217, 207,  44,   80, 203, 156, 123,   6, 117, 108,  33,   253, 118, 156,  50, 215,  83, 192, 211,  192,  96, 132, 205, 118,  92, 206, 254,   202, 230, 171, 236,  51, 135, 233, 194,   53,  57,  72,  89,   0, 227, 222,  39,   185,  94, 100, 159,  46, 224, 186, 134,  232,  36,  54, 169,  81, 223, 150, 161,   105,  97, 100, 184,  37, 205, 111, 144,   43, 156, 135, 129,  56,  57,   2, 115,    59, 120, 171,  26, 204,  97, 254,  53,  164,  17, 135,  22,  23, 121,  49, 144,   173,  38, 181,  61, 227, 132, 145,  40,   75,  69,  65, 164,  65,  64,  25,   9,   199,  38, 132, 165, 135,  97, 129, 144,    4, 179, 112,  60, 182, 250, 222, 252,   248,  23,  59, 228, 191,   0,  56, 237,  163,  20,  30, 104, 216,  45, 209, 254,   107, 188, 169,  97, 173,  41, 205, 249,  164, 128, 120, 201, 215, 188,  35, 103,   108, 202, 174, 140, 138,  95, 241,  71,   83, 199,  70, 162,  73, 226,  87,  84,    43,  17, 178, 203,  21, 250, 128, 159,  137, 190, 174,  11, 215,  22, 155,  34,    34, 169,  13,  95, 208, 118, 231,  89,   88, 179,   4,  29, 176,  17,  64, 235,    58,  88, 114,  65,  57,  96, 211, 163,  188,  18, 100,   4,  91,  45, 233,  80,   217,  90,  45,  93, 144,  82,  60, 203,  224,  81, 113, 231, 159, 185, 171, 128,   109, 224, 154, 141,  33,  66, 255,  44,  112,  41, 125, 205, 154, 174, 226, 225,   173, 243, 107, 172, 214, 234,  22, 186    };        out.println("\nSAFER in CBC mode: " +            "safer -cbckx 42431BA40D291F81D66083C605D3A4D6:74536EBDC211484A\n");        byte[] output = new byte[data.length];        for (int i = 0; i < data.length; i++)            output[i] = (byte)data[i];        Cipher alg = Cipher.getInstance("SAFER/CBC", "Cryptix");        ((Parameterized) alg).setParameter("variant", "K-128");        ((FeedbackCipher) alg).setInitializationVector(anIV);        alg.initEncrypt(aKey);        byte[] x = alg.crypt(input);        compareIt(x, output);                out.println("\nNow decrypting this and checking it against input:\n");        alg.initDecrypt(aKey);        compareIt(alg.crypt(x), input);    }    // These (skipped) tests from <code>check.ref</code> are Hash Functions    // tandem Davies-Meyer scheme:    //        safer -h -tan -kx 42431BA40D291F81D66083C605D3A4D6    // abreast Davies-Meyer scheme:    //        safer -h -abr -kx 42431BA40D291F81D66083C605D3A4D6    private void compareIt (byte[] o1, byte[] o2) {        out.print("  computed output:" + Hex.dumpString(o1));        boolean ok = ArrayUtil.areEqual(o1, o2);        if (!ok)            out.print("\n certified output:" + Hex.dumpString(o2));        passIf(ok, " *** SAFER OUTPUT");    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?