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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
| import base64 import binascii from base64 import b64encode, b64decode from binascii import hexlify, unhexlify
import grt from wb import DefineModule, wbinputs from workbench.log import log_info
key = binascii.unhexlify('你的key,十六进制字符串')
PY2 = False PY3 = True
_range = range string_types = (str,) text_type = str binary_type = bytes
E_FMT = 'UTF8'
S_BOX = { 0X00: 0XD6, 0X01: 0X90, 0X02: 0XE9, 0X03: 0XFE, 0X04: 0XCC, 0X05: 0XE1, 0X06: 0X3D, 0X07: 0XB7, 0X08: 0X16, 0X09: 0XB6, 0X0A: 0X14, 0X0B: 0XC2, 0X0C: 0X28, 0X0D: 0XFB, 0X0E: 0X2C, 0X0F: 0X05, 0X10: 0X2B, 0X11: 0X67, 0X12: 0X9A, 0X13: 0X76, 0X14: 0X2A, 0X15: 0XBE, 0X16: 0X04, 0X17: 0XC3, 0X18: 0XAA, 0X19: 0X44, 0X1A: 0X13, 0X1B: 0X26, 0X1C: 0X49, 0X1D: 0X86, 0X1E: 0X06, 0X1F: 0X99, 0X20: 0X9C, 0X21: 0X42, 0X22: 0X50, 0X23: 0XF4, 0X24: 0X91, 0X25: 0XEF, 0X26: 0X98, 0X27: 0X7A, 0X28: 0X33, 0X29: 0X54, 0X2A: 0X0B, 0X2B: 0X43, 0X2C: 0XED, 0X2D: 0XCF, 0X2E: 0XAC, 0X2F: 0X62, 0X30: 0XE4, 0X31: 0XB3, 0X32: 0X1C, 0X33: 0XA9, 0X34: 0XC9, 0X35: 0X08, 0X36: 0XE8, 0X37: 0X95, 0X38: 0X80, 0X39: 0XDF, 0X3A: 0X94, 0X3B: 0XFA, 0X3C: 0X75, 0X3D: 0X8F, 0X3E: 0X3F, 0X3F: 0XA6, 0X40: 0X47, 0X41: 0X07, 0X42: 0XA7, 0X43: 0XFC, 0X44: 0XF3, 0X45: 0X73, 0X46: 0X17, 0X47: 0XBA, 0X48: 0X83, 0X49: 0X59, 0X4A: 0X3C, 0X4B: 0X19, 0X4C: 0XE6, 0X4D: 0X85, 0X4E: 0X4F, 0X4F: 0XA8, 0X50: 0X68, 0X51: 0X6B, 0X52: 0X81, 0X53: 0XB2, 0X54: 0X71, 0X55: 0X64, 0X56: 0XDA, 0X57: 0X8B, 0X58: 0XF8, 0X59: 0XEB, 0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 0X50575E65, 0X6C737A81, 0X888F969D, 0XA4ABB2B9, 0XC0C7CED5, 0XDCE3EAF1, 0XF8FF060D, 0X141B2229, 0X30373E45, 0X4C535A61, 0X686F767D, 0X848B9299, 0XA0A7AEB5, 0XBCC3CAD1, 0XD8DFE6ED, 0XF4FB0209, 0X10171E25, 0X2C333A41, 0X484F565D, 0X646B7279)
_rk_cache = {}
SM4_ENCRYPT = 1
SM4_DECRYPT = 0
BLOCK_BYTE = 16 BLOCK_HEX = BLOCK_BYTE * 2
def num2hex(num, width=1): """ 整数转为指定长度的十六进制字符串,不足补0 >>> num2hex(1000, width=4) '03e8' :param num: 整数 :param width: 16进制字符串长度, 默认为1 :return str """ return '{:0>{width}}'.format(hex(num)[2:].replace('L', ''), width=width)
def _byte_unpack(num, byte_n=4): _len = 4 step = (byte_n // _len) * 2 hex_str = num2hex(num=num, width=byte_n * 2) split_v = list(_range(len(hex_str)))[::step] + [len(hex_str)] return tuple([int(hex_str[s:e], base=16) for s, e in zip(split_v[:-1], split_v[1:])])
def _byte_pack(byte_array, byte_n=4): _len = 4 width = (byte_n // _len) * 2 if len(byte_array) != _len: raise ValueError('byte_array length must be 4.') return int(''.join([num2hex(num=v, width=width) for v in byte_array]), 16)
def _s_box(byte): return S_BOX.get(byte)
def _non_linear_map(byte_array): """ 非线性变换, 输入A=(a0, a1, a2, a3) (b0, b1, b2, b3) = (Sbox(a0), Sbox(a1), Sbox(a2), Sbox(a3)) """ return (_s_box(byte_array[0]), _s_box(byte_array[1]), _s_box(byte_array[2]), _s_box(byte_array[3]))
def _linear_map(byte4): """ 线性变换L L(B) = B ⊕ (B <<< 2) ⊕ (B <<< 10) ⊕ (B <<< 18) ⊕ (B <<< 24) """ _left = loop_left_shift return byte4 ^ _left(byte4, 2) ^ _left(byte4, 10) ^ _left(byte4, 18) ^ _left(byte4, 24)
def _linear_map_s(byte4): """ 线性变换L' L'(B) = B ⊕ (B <<< 13) ⊕ (B <<< 23) """ _left = loop_left_shift return byte4 ^ _left(byte4, 13) ^ _left(byte4, 23)
def loop_left_shift(num, offset, base=32): """ 循环向左移位 >>> loop_left_shift(0b11010000, 3, base=8) >>> 0b10000110 """ bin_str = '{:0>{width}}'.format(bin(num)[2:], width=base) rem = offset % base return int(bin_str[rem:] + bin_str[:rem], 2)
def _rep_t(byte4): """合成置换T, 由非线性变换和线性变换L复合而成""" b_array = _non_linear_map(_byte_unpack(byte4)) return _linear_map(_byte_pack(b_array))
def _rep_t_s(byte4): """ 合成置换T', 由非线性变换和线性变换L'复合而成 """ b_array = _non_linear_map(_byte_unpack(byte4)) return _linear_map_s(_byte_pack(b_array))
def _round_keys(mk): """ 轮密钥由加密密钥通过密钥扩展算法生成 加密密钥MK = (MK0, MK1, MK2, MK3) 轮密钥生成算法: (K0, K1, K2, K3) = (MK0 ⊕ FK0, MK1 ⊕ FK1, MK2 ⊕ FK2, MK3 ⊕ FK3) rki = Ki+4 = Ki⊕T'(Ki+1 ⊕ Ki+2 ⊕ Ki+3 ⊕ CKi) i=0, 1,...,31 :param mk: 加密密钥, 16byte, 128bit :return list """ _rk_keys = _rk_cache.get(mk) if _rk_keys is None: mk0, mk1, mk2, mk3 = _byte_unpack(mk, byte_n=16) keys = [mk0 ^ FK[0], mk1 ^ FK[1], mk2 ^ FK[2], mk3 ^ FK[3]] for i in _range(32): rk = keys[i] ^ _rep_t_s(keys[i + 1] ^ keys[i + 2] ^ keys[i + 3] ^ CK[i]) keys.append(rk) _rk_keys = keys[4:] _rk_cache[mk] = _rk_keys return _rk_keys
def _round_f(byte4_array, rk): """ 轮函数, F(X0, X1, X2, X3, rk) = X0 ⊕ T(X1 ⊕ X2 ⊕ X3 ⊕ rk) :param byte4_array: (X0, X1, X2, X3), 每一项4byte, 32bit :param rk: 轮密钥, 4byte, 32bit """ x0, x1, x2, x3 = byte4_array return x0 ^ _rep_t(x1 ^ x2 ^ x3 ^ rk)
def _crypt(num, mk, mode=SM4_ENCRYPT): """ SM4加密和解密 :param num: 密文或明文 16byte :param mk: 密钥 16byte :param mode: 轮密钥顺序 """ x_keys = list(_byte_unpack(num, byte_n=16)) round_keys = _round_keys(mk) if mode == SM4_DECRYPT: round_keys = round_keys[::-1] for i in _range(32): x_keys.append(_round_f(x_keys[i:i + 4], round_keys[i])) return _byte_pack(x_keys[-4:][::-1], byte_n=16)
def encrypt(clear_num, mk): """ SM4加密算法由32次迭代运算和1次反序变换R组成. 明文输入为(X0, X1, X2, X3), 每一项4byte, 密文输出为(Y0, Y1, Y2, Y3), 每一项4byte 轮密钥为rki, i=0,1,...,32, 4byte, 运算过程如下: 1). 32次迭代运算: Xi+4 = F(Xi, Xi+1, Xi+2, Xi+3, rki), i=0,1,...,32 2). 反序变换: (Y0, Y1, Y2, Y3) = (X35, X34, X33, X32) :param clear_num: 明文, 16byte :param mk: 密钥, 16byte """ return _crypt(num=clear_num, mk=mk)
def decrypt(cipher_num, mk): """ SM4解密算法, 解密变换与加密变换结构相同, 不同的仅是轮密钥的使用顺序. 解密时轮密钥使用顺序为(rk31,rk30,...,rk0) :param cipher_num: 密文, 16byte :param mk: 密钥, 16byte """ return _crypt(num=cipher_num, mk=mk, mode=SM4_DECRYPT)
def _padding(text, mode=SM4_ENCRYPT): """ 加密填充和解密去填充 """ _str_or_bytes = string_types if PY2 else (string_types + (binary_type,)) if text is None or not isinstance(text, _str_or_bytes): return
if isinstance(text, text_type): text = text.encode(encoding=E_FMT)
if mode == SM4_ENCRYPT: p_num = BLOCK_BYTE - (len(text) % BLOCK_BYTE) space = '' if PY2 else b'' pad_s = (chr(p_num) * p_num) if PY2 else (chr(p_num).encode(E_FMT) * p_num) res = space.join([text, pad_s]) else: p_num = ord(text[-1]) if PY2 else text[-1] res = text[:-p_num] return res
def _key_iv_check(key_iv): """ 密钥或初始化向量检测 """ if key_iv is None or not isinstance(key_iv, (string_types, binary_type)): raise TypeError('Parameter key or iv:{} not string_types or binary_type'.format(key_iv))
if isinstance(key_iv, text_type): key_iv = key_iv.encode(encoding=E_FMT)
if len(key_iv) > BLOCK_BYTE: raise ValueError('Parameter key or iv:{} byte greater than {}'.format(key_iv.decode(E_FMT), BLOCK_BYTE)) return key_iv
def _hex(str_or_bytes): if PY2: hex_str = hexlify(str_or_bytes) else: if isinstance(str_or_bytes, text_type): byte = str_or_bytes.encode(encoding=E_FMT) elif isinstance(str_or_bytes, binary_type): byte = str_or_bytes else: byte = b'' hex_str = hexlify(byte) return hex_str
def _unhex(hex_str): return unhexlify(hex_str)
def encrypt_ecb(plain_text, key): """ SM4(ECB)加密 :param plain_text: 明文 :param key: 密钥, 小于等于16字节 """ plain_text = _padding(plain_text, mode=SM4_ENCRYPT) if plain_text is None: return
key = _key_iv_check(key_iv=key)
plain_hex = _hex(plain_text) cipher_hex_list = [] for i in _range(len(plain_text) // BLOCK_BYTE): sub_hex = plain_hex[i * BLOCK_HEX:(i + 1) * BLOCK_HEX] cipher = encrypt(clear_num=int(sub_hex, 16), mk=int(_hex(key), 16)) cipher_hex_list.append(num2hex(num=cipher, width=BLOCK_HEX))
cipher_text = b64encode(_unhex(''.join(cipher_hex_list))) return cipher_text if PY2 else cipher_text.decode(E_FMT)
def decrypt_ecb(cipher_text, key): """ SM4(ECB)解密 :param cipher_text: 密文 :param key: 密钥, 小于等于16字节 """ cipher_text = b64decode(cipher_text) cipher_hex = _hex(cipher_text)
key = _key_iv_check(key_iv=key) plain_hex_list = [] for i in _range(len(cipher_text) // BLOCK_BYTE): sub_hex = cipher_hex[i * BLOCK_HEX:(i + 1) * BLOCK_HEX] plain = decrypt(cipher_num=int(sub_hex, 16), mk=int(_hex(key), 16)) plain_hex_list.append(num2hex(num=plain, width=BLOCK_HEX))
plain_text = _padding(_unhex(''.join(plain_hex_list)), mode=SM4_DECRYPT) return plain_text if PY2 else plain_text.decode(E_FMT)
def encrypt_cbc(plain_text, key, iv): """ SM4(CBC)加密 :param plain_text: 明文 :param key: 密钥, 小于等于16字节 :param iv: 初始化向量, 小于等于16字节 """ plain_text = _padding(plain_text, mode=SM4_ENCRYPT) if plain_text is None: return
key = _key_iv_check(key_iv=key) iv = _key_iv_check(key_iv=iv)
plain_hex = _hex(plain_text) ivs = [int(_hex(iv), 16)] for i in _range(len(plain_text) // BLOCK_BYTE): sub_hex = plain_hex[i * BLOCK_HEX:(i + 1) * BLOCK_HEX] cipher = encrypt(clear_num=(int(sub_hex, 16) ^ ivs[i]), mk=int(_hex(key), 16)) ivs.append(cipher)
cipher_text = b64encode(_unhex(''.join([num2hex(num=c, width=BLOCK_HEX) for c in ivs[1:]]))) return cipher_text if PY2 else cipher_text.decode(E_FMT)
def decrypt_cbc(cipher_text, key, iv): """ SM4(CBC)解密 :param cipher_text: 密文 :param key: 密钥 小于等于16字节 :param iv: 初始化向量 小于等于16字节 """ cipher_text = b64decode(cipher_text) cipher_hex = _hex(cipher_text)
key = _key_iv_check(key_iv=key) iv = _key_iv_check(key_iv=iv) ivs = [int(_hex(iv), 16)] plain_hex_list = [] for i in _range(len(cipher_text) // BLOCK_BYTE): sub_hex = cipher_hex[i * BLOCK_HEX:(i + 1) * BLOCK_HEX] cipher = int(sub_hex, 16) plain = (ivs[i] ^ decrypt(cipher_num=cipher, mk=int(_hex(key), 16))) ivs.append(cipher) plain_hex_list.append(num2hex(num=plain, width=BLOCK_HEX))
plain_text = _padding(_unhex(''.join(plain_hex_list)), mode=SM4_DECRYPT) return plain_text if PY2 else plain_text.decode(E_FMT)
ModuleInfo = DefineModule(name="DataUtils", author="b", version="1.0")
@ModuleInfo.plugin("wb.sqlide.decryptData", caption="decrypt data from resultSet", input=[wbinputs.currentResultset(), wbinputs.currentEditableResultset()], pluginMenu="SQL/Utilities", accessibilityName="decryptData from resultSet") @ModuleInfo.export(grt.INT, grt.classes.db_query_Resultset, grt.classes.db_query_EditableResultset) def decryptData(result_set, edit_result_set): """decryptData from resultSet """ try: row_count = result_set.rowCount column_count = len(result_set.columns) log_info(str(row_count) + '\n') log_info(str(column_count) + '\n') for a in range(row_count - 1): result_set.goToRow(a) edit_column(result_set, edit_result_set, column_count) result_set.nextRow() except Exception as e: log_info(str(e) + '\n') return 0
def edit_column(result_set, edit_result_set, column_count): for a in range(column_count - 1): value = result_set.stringFieldValue(a) log_info(value + '\n') if value.startswith('enc:'): b = binascii.unhexlify(value[4:]) data = base64.b64encode(b).decode() edit_result_set.setStringFieldValue(a, decrypt_ecb(data, key))
|