📄 rc4test.java
字号:
//// Created by Thomas Dixon on 03/04/06.//// used test vectors found on the web, almost anywhere i could get em// http://www.4guysfromrolla.com/webtech/code/rc4.inc.html// http://cvs.sourceforge.net/viewcvs.py/madwifi/madwifi/net80211/test_wep.c?rev=1.4// http://www.koders.com/c/fid762A1A15781E9982A94303EA7CA4EDC32745C133.aspxpublic class RC4Test { private static final char[] HEX_DIGITS = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; private static int ERRORS = 0, TEST_COUNT = 7; // Will be faster than creating 7 instances of the same class private static RC4 rc4 = new RC4(); public static void main(String[] args) { // run the tests and time it double timeA = System.currentTimeMillis(); for (int i = 0; i < TEST_COUNT; i++) { System.out.print("Test Vector "+i+": ... "); testCases(i); } double timeB = System.currentTimeMillis(); // print a seperator of dashes String sep = ""; for (int i = 0; i < 20; i++) sep += "-"; System.out.println(sep); // print the overall result, with failure count if any System.out.print("Result: "); if (ERRORS == 0) { System.out.println("OK"); } else { System.out.println("FAIL with "+ERRORS+" error(s)"); } // tell them how long the tests took System.out.println("Ran "+TEST_COUNT+" tests in "+(timeB-timeA)+" ms"); } private static void testCases(int n) { switch (n) { case 0: byte[] testKey1 = { (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; byte[] testIn1 = { (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; String testOut1 = "75b7878099e0c596"; compare(testKey1, testIn1, testOut1); break; case 1: byte[] testKey2 = { (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; byte[] testIn2 = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; String testOut2 = "7494c2e7104b0879"; compare(testKey2, testIn2, testOut2); break; case 2: byte[] testKey3 = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; byte[] testIn3 = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; String testOut3 = "de188941a3375d3a"; compare(testKey3, testIn3, testOut3); break; case 3: byte[] testKey4 = { (byte) 0xef, (byte) 0x01, (byte) 0x23, (byte) 0x45 }; byte[] testIn4 = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; String testOut4 = "d6a141a7ec3c38dfbd61"; compare(testKey4, testIn4, testOut4); break; case 4: byte[] testKey5 = { (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; byte[] testIn5 = new byte[512]; for (int i = 0; i < 512; i++) testIn5[i] = (byte)0x01; String testOut5 = "7595c3e6114a09780c4ad452338e1ffd9a1be9498f813d76533449b6778dcad8c78a8d2ba9ac6608" +"5d0e53d59c26c2d1c490c1ebbe0ce66d1b6b1b13b6b919b847c25a91447a95e75e4ef16779cde8bf" +"0a95850e32af9689444fd377108f98fdcbd4e726567500990bcc7e0ca3c4aaa304a387d20f3b8fbb" +"cd42a1bd311d7a4303dda5ab078896ae80c18b0af66dff319616eb784e495ad2ce90d7f772a81747" +"b65f62093b1e0db9e5ba532fafec47508323e671327df9444432cb7367cec82f5d44c0d00b67d650" +"a075cd4b70dedd77eb9b10231b6b5b741347396d62897421d43df9b42e446e358e9c11a9b2184ecb" +"ef0cd8e7a877ef968f1390ec9b3d35a5585cb009290e2fcde7b5ec66d9084be44055a619d9dd7fc3" +"166f9487f7cb272912426445998514c15d53a18c864ce3a2b7555793988126520eacf2e3066e230c" +"91bee4dd5304f5fd0405b35bd99c73135d3d9bc335ee049ef69b3867bf2d7bd1eaa595d8bfc0066f" +"f8d31509eb0c6caa006c807a623ef84c3d33c195d23ee320c40de0558157c822d4b8c569d849aed5" +"9d4e0fd7f379586b4b7ff684ed6a189f7486d49b9c4bad9ba24b96abf924372c8a8fffb10d553549" +"00a77a3db5f205e1b99fcd8660863a159ad4abe40fa48934163ddde542a6585540fd683cbfd8c00f" +"12129a284deacc4cdefe58be7137541c047126c8d49e2755ab181ab7e940b0c0"; compare(testKey5, testIn5, testOut5); break; case 5: byte[] testKey6 = { (byte) 0xfb, (byte) 0x02, (byte) 0x9e, (byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x33, (byte) 0x34 }; byte[] testIn6 = { (byte) 0xaa, (byte) 0xaa, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x08, (byte) 0x00, (byte) 0x45, (byte) 0x00, (byte) 0x00, (byte) 0x4e, (byte) 0x66, (byte) 0x1a, (byte) 0x00, (byte) 0x00, (byte) 0x80, (byte) 0x11, (byte) 0xbe, (byte) 0x64, (byte) 0x0a, (byte) 0x00, (byte) 0x01, (byte) 0x22, (byte) 0x0a, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x89, (byte) 0x00, (byte) 0x89, (byte) 0x00, (byte) 0x3a, (byte) 0x00, (byte) 0x00, (byte) 0x80, (byte) 0xa6, (byte) 0x01, (byte) 0x10, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x45, (byte) 0x43, (byte) 0x45, (byte) 0x4a, (byte) 0x45, (byte) 0x48, (byte) 0x45, (byte) 0x43, (byte) 0x46, (byte) 0x43, (byte) 0x45, (byte) 0x50, (byte) 0x46, (byte) 0x45, (byte) 0x45, (byte) 0x49, (byte) 0x45, (byte) 0x46, (byte) 0x46, (byte) 0x43, (byte) 0x43, (byte) 0x41, (byte) 0x43, (byte) 0x41, (byte) 0x43, (byte) 0x41, (byte) 0x43, (byte) 0x41, (byte) 0x43, (byte) 0x41, (byte) 0x41, (byte) 0x41, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x01, (byte) 0x1b, (byte) 0xd0, (byte) 0xb6, (byte) 0x04 }; String testOut6 = "f69c5806bd6ce84626bcbefb9474650aad1f7909b0f64d5f58a503a258b7ed22eb0ea64930d3a056" +"a55742fcce141d485f8aa836dea18df42c5380805ad0c61a5d6f58f41040b24b7d1a693856ed0d43" +"98e7aee3bf0e2a2ca8f7"; compare(testKey6, testIn6, testOut6); break; case 6: byte[] testKey7 = { (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; byte[] testIn7 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }; String testOut7 = "66a0949f8af7d6891f7f832ba833c00c892ebe30143ce28740011ecf"; compare(testKey7, testIn7, testOut7); break; default: } // switch } private static void compare(byte[] key, byte[] in, String hex) { // init the cipher with the key try { rc4.engineInit(key); } catch (Exception e) { } // set string to hex representation of the ciphertext String result = toString(rc4.engineCrypt(in,0)); if (result.equals(hex)) { System.out.println("ok"); } else { System.out.println("FAIL"); ERRORS++; } } private static String toString(byte[] ba) { return toString(ba, 0, ba.length); } private static String toString(byte[] ba, int o00set, int length) { char[] buf = new char[length * 2]; for (int i = 0, j = 0, k; i < length; i++) { k = ba[o00set++]; buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEX_DIGITS[ k & 0x0F]; } return new String(buf); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -