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
|
""" SM4 GM """ import copy
SboxTable = [ 0xd6,0x90,0xe9,0xfe,0xcc,0xe1,0x3d,0xb7,0x16,0xb6,0x14,0xc2,0x28,0xfb,0x2c,0x05, 0x2b,0x67,0x9a,0x76,0x2a,0xbe,0x04,0xc3,0xaa,0x44,0x13,0x26,0x49,0x86,0x06,0x99, 0x9c,0x42,0x50,0xf4,0x91,0xef,0x98,0x7a,0x33,0x54,0x0b,0x43,0xed,0xcf,0xac,0x62, 0xe4,0xb3,0x1c,0xa9,0xc9,0x08,0xe8,0x95,0x80,0xdf,0x94,0xfa,0x75,0x8f,0x3f,0xa6, 0x47,0x07,0xa7,0xfc,0xf3,0x73,0x17,0xba,0x83,0x59,0x3c,0x19,0xe6,0x85,0x4f,0xa8, 0x68,0x6b,0x81,0xb2,0x71,0x64,0xda,0x8b,0xf8,0xeb,0x0f,0x4b,0x70,0x56,0x9d,0x35, 0x1e,0x24,0x0e,0x5e,0x63,0x58,0xd1,0xa2,0x25,0x22,0x7c,0x3b,0x01,0x21,0x78,0x87, 0xd4,0x00,0x46,0x57,0x9f,0xd3,0x27,0x52,0x4c,0x36,0x02,0xe7,0xa0,0xc4,0xc8,0x9e, 0xea,0xbf,0x8a,0xd2,0x40,0xc7,0x38,0xb5,0xa3,0xf7,0xf2,0xce,0xf9,0x61,0x15,0xa1, 0xe0,0xae,0x5d,0xa4,0x9b,0x34,0x1a,0x55,0xad,0x93,0x32,0x30,0xf5,0x8c,0xb1,0xe3, 0x1d,0xf6,0xe2,0x2e,0x82,0x66,0xca,0x60,0xc0,0x29,0x23,0xab,0x0d,0x53,0x4e,0x6f, 0xd5,0xdb,0x37,0x45,0xde,0xfd,0x8e,0x2f,0x03,0xff,0x6a,0x72,0x6d,0x6c,0x5b,0x51, 0x8d,0x1b,0xaf,0x92,0xbb,0xdd,0xbc,0x7f,0x11,0xd9,0x5c,0x41,0x1f,0x10,0x5a,0xd8, 0x0a,0xc1,0x31,0x88,0xa5,0xcd,0x7b,0xbd,0x2d,0x74,0xd0,0x12,0xb8,0xe5,0xb4,0xb0, 0x89,0x69,0x97,0x4a,0x0c,0x96,0x77,0x7e,0x65,0xb9,0xf1,0x09,0xc5,0x6e,0xc6,0x84, 0x18,0xf0,0x7d,0xec,0x3a,0xdc,0x4d,0x20,0x79,0xee,0x5f,0x3e,0xd7,0xcb,0x39,0x48, ]
FK = [0xa3b1bac6,0x56aa3350,0x677d9197,0xb27022dc]
CK = [ 0x00070e15,0x1c232a31,0x383f464d,0x545b6269, 0x70777e85,0x8c939aa1,0xa8afb6bd,0xc4cbd2d9, 0xe0e7eef5,0xfc030a11,0x181f262d,0x343b4249, 0x50575e65,0x6c737a81,0x888f969d,0xa4abb2b9, 0xc0c7ced5,0xdce3eaf1,0xf8ff060d,0x141b2229, 0x30373e45,0x4c535a61,0x686f767d,0x848b9299, 0xa0a7aeb5,0xbcc3cad1,0xd8dfe6ed,0xf4fb0209, 0x10171e25,0x2c333a41,0x484f565d,0x646b7279 ]
ENCRYPT = 0 DECRYPT = 1
def GET_UINT32_BE(key_data): tmp_data = int((key_data[0] << 24) | (key_data[1] << 16) | (key_data[2] << 8) | (key_data[3])) return tmp_data
def PUT_UINT32_BE(n): return [int((n>>24)&0xff), int((n>>16)&0xff), int((n>>8)&0xff), int((n)&0xff)]
def SHL(x, n): xx = int(int(x << n) & 0xffffffff) return xx
def ROTL(x, n): xx = SHL(x, n) yy = xx | int((x >> (32 - n)) & 0xffffffff) return yy
def XOR(a, b): return list(map(lambda x, y: x ^ y, a, b))
def sm4Sbox(idx): return SboxTable[idx]
def sm4CalciRK(ka): b = [0, 0, 0, 0] a = PUT_UINT32_BE(ka) b[0] = sm4Sbox(a[0]) b[1] = sm4Sbox(a[1]) b[2] = sm4Sbox(a[2]) b[3] = sm4Sbox(a[3]) bb = GET_UINT32_BE(b[0:4]) rk = bb ^ (ROTL(bb, 13)) ^ (ROTL(bb, 23)) return rk
def sm4Lt(ka): b = [0, 0, 0, 0] a = PUT_UINT32_BE(ka) b[0] = sm4Sbox(a[0]) b[1] = sm4Sbox(a[1]) b[2] = sm4Sbox(a[2]) b[3] = sm4Sbox(a[3]) bb = GET_UINT32_BE(b[0:4]) c = bb ^ (ROTL(bb, 2)) ^ (ROTL(bb, 10)) ^ (ROTL(bb, 18)) ^ (ROTL(bb, 24)) return c
def sm4F(x0, x1, x2, x3, rk): return (x0 ^ sm4Lt(x1 ^ x2 ^ x3 ^ rk))
class Sm4(object): def __init__(self): self.sk = [0]*32 self.mode = ENCRYPT
def sm4_set_key(self, key_data, mode): self.sm4_setkey(key_data, mode)
def sm4_setkey(self, key, mode): MK = [0, 0, 0, 0] k = [0]*36 MK[0] = GET_UINT32_BE(key[0:4]) MK[1] = GET_UINT32_BE(key[4:8]) MK[2] = GET_UINT32_BE(key[8:12]) MK[3] = GET_UINT32_BE(key[12:16]) k[0:4] = XOR(MK[0:4], FK[0:4]) for i in range(32): k[i + 4] = k[i] ^ (sm4CalciRK(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i])) self.sk[i] = k[i + 4] self.mode = mode if mode == DECRYPT: for idx in range(16): t = self.sk[idx] self.sk[idx] = self.sk[31 - idx] self.sk[31 - idx] = t
def sm4_one_round(self, sk, in_put): out_put = [] ulbuf = [0]*36 ulbuf[0] = GET_UINT32_BE(in_put[0:4]) ulbuf[1] = GET_UINT32_BE(in_put[4:8]) ulbuf[2] = GET_UINT32_BE(in_put[8:12]) ulbuf[3] = GET_UINT32_BE(in_put[12:16]) for idx in range(32): ulbuf[idx + 4] = sm4F(ulbuf[idx], ulbuf[idx + 1], ulbuf[idx + 2], ulbuf[idx + 3], sk[idx])
out_put += PUT_UINT32_BE(ulbuf[35]) out_put += PUT_UINT32_BE(ulbuf[34]) out_put += PUT_UINT32_BE(ulbuf[33]) out_put += PUT_UINT32_BE(ulbuf[32]) return out_put
def sm4_crypt_ecb(self, input_data): length = len(input_data) i = 0 output_data = [] while length > 0: output_data += self.sm4_one_round(self.sk, input_data[i:i+16]) i += 16 length -= 16 return output_data
def sm4_crypt_cbc(self, iv, input_data): length = len(input_data) i = 0 output_data = [] tmp_input = [0]*16 if self.mode == ENCRYPT: while length > 0: tmp_input[0:16] = XOR(input_data[i:i+16], iv[0:16]) output_data += self.sm4_one_round(self.sk, tmp_input[0:16]) iv = copy.deepcopy(output_data[i:i+16]) i += 16 length -= 16 else: while length > 0: output_data += self.sm4_one_round(self.sk, input_data[i:i+16]) output_data[i:i+16] = XOR(output_data[i:i+16], iv[0:16]) iv = copy.deepcopy(input_data[i:i + 16]) i += 16 length -= 16 return output_data
def sm4_crypt_ecb(mode, key, data): sm4_d = Sm4() sm4_d.sm4_set_key(key, mode) en_data = sm4_d.sm4_crypt_ecb(data) return en_data
def sm4_crypt_cbc(mode, key, iv, data): sm4_d = Sm4() sm4_d.sm4_set_key(key, mode) en_data = sm4_d.sm4_crypt_cbc(iv, data) return en_data
def encrypt(key, message): sm4_d = Sm4() sm4_d.sm4_set_key(key, ENCRYPT) cipher = sm4_d.sm4_crypt_ecb(message) return cipher
def decrypt(key, cipher): sm4_d = Sm4() sm4_d.sm4_set_key(key, DECRYPT) de_data = sm4_d.sm4_crypt_ecb(cipher) message=(''.join(map(chr,de_data))) return message a1=[195, 240, 101, 163, 196, 230, 26, 199, 90, 214, 164, 63, 21, 75, 63, 253, 38, 215, 63, 67, 130, 140, 20, 203, 141, 3, 234, 95, 86, 10, 114, 97, 186, 84, 115, 198, 73, 252, 192, 245, 206, 156, 153, 186, 210, 155, 190, 134, 211, 213, 143, 96, 239, 67, 56, 36, 188, 124, 237, 196, 212, 117, 90, 231, 214, 67, 157, 133, 145, 214, 251, 127, 43, 62, 173, 13, 107, 231, 41, 128, 156, 240, 150, 8, 180, 146, 88, 64, 71, 242, 75, 222, 90, 2, 38, 50, 65, 143, 71, 110, 63, 4, 205, 29, 71, 20, 10, 39, 206, 142, 85, 76, 75, 95, 3, 124, 34, 62, 42, 39, 32, 80, 80, 91, 137, 141, 196, 209, 194, 149, 129, 180, 214, 254, 128, 72, 174, 149, 242, 217, 223, 129, 207, 96, 23, 116, 64, 34, 49, 229, 65, 36, 205, 60, 89, 161, 137, 188, 97, 112, 116, 213, 174, 163, 214, 154, 44, 142, 239, 22, 134, 135, 200, 237, 10, 244, 164, 8, 34, 27, 220, 204, 10, 229, 53, 252, 212, 119, 70, 242, 102, 199, 223, 214, 34, 90, 135, 173, 125, 247, 6, 229, 242, 160, 1, 65, 219, 57, 2, 40, 10, 197, 63, 248, 178, 112, 192, 138, 181, 187, 105, 157, 23, 252, 162, 254, 214, 178, 83, 207, 47, 66, 114, 41, 233, 206, 220, 94, 2, 112, 97, 75, 200, 28, 58, 188, 245, 54, 163, 27, 113, 115, 21, 193, 34, 118, 92, 247, 188, 252, 173, 211, 219, 127, 93, 142, 177, 147, 116, 138, 77, 89, 238, 207, 115, 173, 182, 201, 29, 139, 109, 240, 155, 64, 87, 21, 2, 67, 21, 113, 67, 66, 13, 172, 45, 20, 160, 235, 115, 249, 16, 211, 238, 180, 102, 211, 81, 105, 166, 59, 248, 150, 60, 164, 51, 14, 120, 38, 136, 25, 12, 184, 167, 19, 240, 104, 4, 191, 179, 162, 202, 103, 163, 40, 97, 253, 143, 156, 200, 243, 114, 116, 6, 78, 238, 108, 28, 82, 112, 85, 241, 107, 54, 88, 37, 156, 23, 46, 10, 21, 46, 58, 229, 18, 226, 164, 138, 169, 113, 1, 8, 157, 209, 62, 180, 210, 189, 20, 217, 175, 149, 156, 156, 129, 213, 235, 201, 108, 208, 109, 210, 96, 129, 76, 126, 202, 203, 46, 184, 210, 226, 252, 29, 246, 245, 57, 160, 129, 167, 226, 171, 136, 179, 17, 102, 4, 210, 1, 193, 5, 98, 197, 212, 190, 246, 103, 147, 22, 8, 83, 88, 228, 113, 83, 102, 163, 207, 157, 11, 42, 82, 21, 97, 31, 241, 103, 143, 217, 182, 168, 65, 253, 159, 182, 221, 41, 98, 207, 216, 252, 8, 64, 172, 53, 94, 169, 14, 81, 2, 104, 141, 210, 3, 27, 192, 31, 180, 253, 89, 88, 219, 119, 120, 40, 144, 184, 3, 210, 204, 39, 144, 66, 115, 77, 192, 247, 88, 5, 108, 134, 0, 4, 196, 108, 39, 44, 148, 64, 139, 48, 161, 142, 92, 226, 77, 15, 94, 136, 240, 43, 54, 42, 7, 241, 200, 57, 92, 213, 40, 32, 38, 89, 147, 251, 37, 115, 53, 25, 241, 144, 181, 44, 227, 216, 90, 15, 109, 249, 49, 129, 199, 190, 226, 159, 210, 137, 15, 184, 14, 123, 185, 201, 190, 14, 243, 214, 244, 179, 141, 10, 3, 43, 51, 103, 8, 239, 154, 158, 59, 224, 85, 64, 120, 46, 207, 33, 103, 215, 186, 80, 113, 232, 45, 17, 87, 163, 92, 108, 219, 45, 239, 109, 4, 93, 131, 104, 105, 11, 247, 101, 205, 168, 202, 255, 46, 237, 68, 3, 220, 35, 210, 210, 187, 64, 18, 239, 8, 84, 162, 47, 73, 240, 12, 35, 165, 146, 93, 108, 203, 242, 68, 214, 254, 97, 43, 190, 204, 100, 65, 103, 174, 249, 191, 121, 39, 233, 196, 173, 78, 119, 116, 125, 143, 117, 126, 55, 212, 173, 117, 116, 38, 126, 209, 41, 251, 39, 10, 211, 195, 124, 80, 17, 196, 79, 74, 203, 3, 246, 182, 136, 246, 192, 210, 175, 37, 108, 109, 224, 170, 46, 85, 174, 160, 48, 87, 79, 188, 17, 55, 5, 88, 17, 34, 185, 65, 215, 169, 82, 222, 91, 12, 169, 146, 207, 1, 172, 48, 27, 122, 121, 98, 182, 30, 141, 25, 196, 107, 107, 90, 112, 91, 99, 41, 217, 73, 44, 205, 189, 159, 142, 135, 153, 159, 96, 64, 60, 165, 110, 31, 244, 226, 47, 19, 115, 98, 195, 251, 194, 86, 129, 112, 25, 174, 162, 160, 38, 190, 118, 213, 168, 68, 43, 229, 213, 31, 210, 44, 105, 153, 165, 119, 141, 157, 237, 31, 35, 72, 107, 232, 2, 89, 245, 98, 119, 155, 238, 215, 49, 118, 80, 132, 152, 13, 103, 24, 200, 54, 59, 4, 185, 213, 43, 205, 114, 6, 164, 36, 44, 47, 159, 151, 104, 209, 149, 43, 65, 27, 70, 216, 181, 251, 5, 208, 142, 207, 168, 232, 215, 189, 85, 10, 11, 23, 223, 210, 184, 110, 191, 84, 67, 202, 161, 174, 227, 92, 156, 22, 222, 218, 16, 171, 107, 242, 202, 122, 45, 50, 250, 73, 2, 186, 205, 242, 113, 78, 52, 252, 120, 92, 215, 183, 140, 64, 104, 181, 37, 37, 254, 31, 178, 61, 45, 11, 225, 97, 65, 24, 139, 34, 135, 92, 127, 210, 108, 205, 215, 79, 1, 215, 158, 126, 71, 108, 218, 206, 85, 78, 181, 0, 52, 44, 25, 57, 120, 191, 34, 17, 140, 203, 193, 77, 13, 150, 228, 60, 247, 219, 38, 253, 174, 44, 26, 20, 114, 248, 36, 79, 147, 48, 55, 39, 182, 95, 202, 72, 254, 148, 209, 45, 113, 103, 238, 9, 153, 227, 205, 85, 25, 85, 91, 85, 20, 1, 64, 146, 223, 246, 2, 177, 105, 199, 112, 248, 73, 11, 215, 140, 249, 179, 44, 209, 86, 154, 10, 210, 183, 39, 171, 39, 2, 63, 41, 120, 116, 65, 241, 133, 72, 175, 228, 1, 96, 229, 5, 79, 2, 188, 125, 24, 76, 251, 39, 200, 214, 134, 191, 11, 131, 233, 141, 79, 87, 234, 152, 174, 30, 115, 97, 109, 95, 248, 143, 214, 239, 213, 166, 92, 253, 99, 118, 98, 70, 247, 69, 183, 250, 158, 37, 228, 10, 68, 211, 64, 37, 237, 127, 231, 5, 239, 63, 209, 72, 116, 119, 77, 69, 235, 249, 72, 127, 31, 123, 118, 157, 18, 9, 41, 58, 29, 104, 5, 10, 143, 144, 11, 37, 88, 251, 36, 77, 49, 10, 116, 11, 35, 161, 104, 49, 252, 36, 242, 215, 104, 167, 150, 227, 42, 235, 122, 191, 122, 16, 166, 162, 197, 13, 109, 15, 172, 33, 51, 230, 33, 104, 250, 4, 253, 185, 218, 251, 35, 233, 4, 246, 161, 28, 112, 203, 75, 123, 217, 32, 85, 164, 242, 14, 59, 246, 191, 158, 242, 138, 82, 17, 114, 128, 35, 52, 4, 239, 162, 125, 12, 125, 129, 26, 144, 112, 200, 156, 10, 71, 97, 93, 161, 84, 26, 32, 6, 152, 17, 174, 244, 143, 80, 115, 105, 61, 80, 212, 76, 247, 191, 52, 133, 219, 6, 90, 135, 80, 160, 39, 251, 114, 10, 96, 36, 100, 240, 201, 8, 181, 39, 66, 150, 19, 26, 86, 245, 247, 93, 142, 77, 226, 141, 217, 222, 250, 101, 87, 159, 201, 145, 106, 218, 79, 11, 133, 26, 191, 233, 158, 132, 79, 157, 138, 156, 232, 144, 115, 158, 7, 17, 167, 5, 102, 0, 3, 49, 128, 229, 128, 69, 24, 62, 1, 59, 146, 12, 152, 206, 16, 242, 209, 114, 79, 238, 175, 226, 73, 116, 164, 13, 210, 134, 116, 71, 225, 58, 242, 25, 227, 120, 93, 125, 10, 64, 2, 72, 56, 59, 38, 52, 211, 20, 142, 19, 197, 5, 151, 142, 232, 137, 186, 200, 126, 6, 216, 24, 255, 99, 73, 18, 199, 35, 88, 13, 240, 100, 84, 234, 12, 50, 104, 196, 122, 218, 132, 1, 67, 188, 116, 189, 153, 129, 255, 65, 143, 111, 186, 127, 29, 60, 203, 89, 238, 176, 91, 40, 108, 230, 241, 52, 66, 23, 233, 239, 246, 93, 86, 132, 192, 41, 2, 9, 233, 239, 35, 22, 32, 104, 248, 160, 153, 113, 186, 213, 195, 224, 211, 119, 170, 130, 203, 0, 24, 255, 30, 176, 164, 123, 55, 33, 206, 249, 122, 2, 155, 220, 237, 152, 11, 9, 152, 172, 171, 77, 139, 70, 206, 197, 153, 127, 58, 87, 188, 160, 138, 43, 123, 169, 2, 214, 96, 229, 166, 153, 61, 83, 121, 238, 130, 243, 63, 237, 45, 208, 53, 96, 133, 6, 135, 207, 95, 79, 131, 38, 59, 233, 91, 87, 181, 90, 32, 2, 216, 208, 68, 188, 15, 160, 126, 182, 193, 123, 178, 252, 57, 62, 129, 81, 42, 204, 175, 211, 17, 197, 18, 240, 41, 82, 244, 201, 190, 87, 189, 234, 56, 26, 239, 55, 64, 82, 21, 158, 121, 210, 80, 58, 177, 122, 134, 151, 29, 225, 173, 37, 112, 85, 67, 62, 102, 73, 61, 69, 22, 209, 254, 137, 65, 86, 120, 9, 222, 91, 48, 24, 138, 135, 152, 96, 246, 126, 175, 39, 102, 197, 41, 236, 130, 159, 56, 198, 251, 69, 166, 172, 156, 240, 149, 204, 162, 211, 150, 76, 79, 158, 38, 153, 55, 20, 94, 62, 121, 218, 55, 183, 180, 45, 249] if __name__ == "__main__": message = [115, 109, 52, 32, 116, 101, 115, 116, 48, 49, 50, 51, 52, 53, 54, 55] cipher=[167, 67, 35, 40, 2, 174, 156, 180, 14, 3, 97, 185, 54, 222, 236, 63]
assert len(message)%16==0 for i in range(0,len(a1),16):
s=[] for j in range(16): s.append(a1[i+j]) print decrypt(s,cipher)
|