1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| import base64 def rc4_main(key = "init_key", message = "init_message"): print("RC4解密主函数调用成功") print('\n') s_box = rc4_init_sbox(key) crypt = rc4_excrypt(message, s_box) return crypt def rc4_init_sbox(key): s_box = list(range(256)) print("原来的 s 盒:%s" % s_box) print('\n') j = 0 for i in range(256): j = (j + s_box[i] + ord(key[i % len(key)])) % 256 s_box[i], s_box[j] = s_box[j], s_box[i] print("混乱后的 s 盒:%s"% s_box) print('\n') return s_box def rc4_excrypt(plain, box): print("调用解密程序成功。") print('\n') plain = base64.b64decode(plain.encode('utf-8')) plain = bytes.decode(plain) res = [] i = j = 0 for s in plain: i = (i + 1) % 256 j = (j + box[i]) % 256 box[i], box[j] = box[j], box[i] t = (box[i] + box[j]) % 256 k = box[t] res.append(chr(ord(s) ^ k)) print("res用于解密字符串,解密后是:%res" %res) print('\n') cipher = "".join(res) print("解密后的字符串是:%s" %cipher) print('\n') print("解密后的输出(没经过任何编码):") print('\n') return cipher a=[] key="flag{123321321123badbeef012}" s="\x1d\xc5\x80_\xe7\x0cX\x06\xb1\x9e\x1d=x?\x85v\xa6\x97\x89\x0f\xe2\x8c\x84U\xc6[\xc4V\x02\xbb\xf2\xbaq\xa3\x16\xc1x\xa6!\xa7\x04\x96)" for i in a: s+=chr(i) s=str(base64.b64encode(s.encode('utf-8')), 'utf-8') rc4_main(key, s)
D:\CollegeStudy\Python_code\venv\Scripts\python.exe D:/CTF_Tools/0_CTF_Scripts/BuuCTF_Test/erefinal.py RC4解密主函数调用成功
原来的 s 盒:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
混乱后的 s 盒:[77, 211, 134, 160, 149, 85, 141, 43, 217, 61, 105, 55, 34, 206, 113, 188, 245, 84, 115, 82, 200, 96, 223, 47, 26, 17, 14, 49, 13, 187, 23, 33, 97, 146, 46, 124, 108, 159, 65, 155, 129, 79, 51, 199, 0, 168, 132, 5, 169, 63, 192, 89, 22, 76, 87, 182, 88, 226, 184, 111, 50, 148, 183, 176, 41, 122, 152, 24, 91, 156, 73, 218, 39, 229, 241, 44, 252, 102, 196, 27, 28, 29, 135, 2, 104, 185, 78, 140, 38, 170, 11, 179, 224, 138, 30, 207, 66, 107, 60, 202, 167, 139, 56, 128, 83, 175, 144, 100, 253, 72, 219, 190, 80, 130, 126, 7, 90, 15, 164, 174, 151, 150, 198, 239, 157, 3, 92, 197, 32, 101, 142, 244, 225, 203, 116, 12, 231, 161, 98, 16, 162, 201, 209, 163, 127, 25, 165, 255, 71, 118, 119, 235, 112, 21, 215, 36, 173, 53, 230, 143, 247, 20, 236, 45, 242, 254, 213, 180, 52, 181, 221, 154, 4, 166, 58, 193, 62, 93, 1, 248, 238, 37, 8, 110, 208, 67, 222, 42, 178, 194, 133, 18, 186, 106, 249, 147, 177, 171, 48, 69, 70, 109, 216, 212, 81, 123, 136, 233, 10, 114, 210, 250, 99, 214, 220, 120, 227, 54, 64, 59, 103, 94, 75, 189, 117, 246, 86, 240, 172, 205, 153, 35, 74, 131, 251, 137, 243, 19, 9, 232, 191, 95, 6, 204, 121, 234, 158, 68, 145, 228, 237, 125, 57, 195, 40, 31]
调用解密程序成功。
res用于解密字符串,解密后是:['f', 'l', 'a', 'g', '{', 'R', 'C', '_', 'f', '0', 'u', 'r', '_', 'A', 'n', 'd', '_', 'B', 'a', 's', 'e', '_', 's', '1', 'x', 't', 'y', '_', 'f', '0', 'u', 'r', '_', 'E', 'n', 'c', 'r', 'y', 'p', 't', '_', '!', '}']es
解密后的字符串是:flag{RC_f0ur_And_Base_s1xty_f0ur_Encrypt_!}
解密后的输出(没经过任何编码):
进程已结束,退出代码0
|