MOD:init 5092
Showing
3 changed files
with
95 additions
and
7 deletions
... | @@ -47,7 +47,8 @@ from apps.doc.ocr.cms import cms | ... | @@ -47,7 +47,8 @@ from apps.doc.ocr.cms import cms |
47 | from apps.doc.exceptions import GCAPException | 47 | from apps.doc.exceptions import GCAPException |
48 | from apps.doc.named_enum import RequestTeam, RequestTrigger, ProcessName, ErrorType, SystemName | 48 | from apps.doc.named_enum import RequestTeam, RequestTrigger, ProcessName, ErrorType, SystemName |
49 | from common.tools.comparison import cp | 49 | from common.tools.comparison import cp |
50 | from common.tools.des import decode_des | 50 | # from common.tools.des import decode_des |
51 | from common.tools.aes_util import aes_decrypt_cbc | ||
51 | 52 | ||
52 | compare_log = logging.getLogger('compare') | 53 | compare_log = logging.getLogger('compare') |
53 | log_base = '[Compare]' | 54 | log_base = '[Compare]' |
... | @@ -55,6 +56,7 @@ log_base = '[Compare]' | ... | @@ -55,6 +56,7 @@ log_base = '[Compare]' |
55 | empty_str = '' | 56 | empty_str = '' |
56 | empty_error_type = 1000 | 57 | empty_error_type = 1000 |
57 | des_key = conf.CMS_DES_KEY | 58 | des_key = conf.CMS_DES_KEY |
59 | des_iv = conf.CMS_DES_IV | ||
58 | 60 | ||
59 | 61 | ||
60 | def rotate_bound(image, angle): | 62 | def rotate_bound(image, angle): |
... | @@ -971,7 +973,7 @@ def get_se_cms_compare_info_auto(application_id, last_obj, application_entity, d | ... | @@ -971,7 +973,7 @@ def get_se_cms_compare_info_auto(application_id, last_obj, application_entity, d |
971 | if id_info.get('idType') in consts.SE_CMS_FIRST_ID_FIELD_MAPPING: | 973 | if id_info.get('idType') in consts.SE_CMS_FIRST_ID_FIELD_MAPPING: |
972 | license_en, is_prc = consts.SE_CMS_FIRST_ID_FIELD_MAPPING[id_info['idType']] | 974 | license_en, is_prc = consts.SE_CMS_FIRST_ID_FIELD_MAPPING[id_info['idType']] |
973 | # ['customerName', 'idNum', 'dateOfBirth', 'idExpiryDate', 'hukouProvince'] | 975 | # ['customerName', 'idNum', 'dateOfBirth', 'idExpiryDate', 'hukouProvince'] |
974 | id_num = decode_des(id_info.get('idNum', ''), des_key) | 976 | id_num = aes_decrypt_cbc(id_info.get('idNum', ''), des_key, des_iv) |
975 | field_input = [('customerName', customer_name), ('idNum', id_num), | 977 | field_input = [('customerName', customer_name), ('idNum', id_num), |
976 | ('idExpiryDate', id_info.get('idExpiryDate', ''))] | 978 | ('idExpiryDate', id_info.get('idExpiryDate', ''))] |
977 | # if is_prc: | 979 | # if is_prc: |
... | @@ -983,7 +985,7 @@ def get_se_cms_compare_info_auto(application_id, last_obj, application_entity, d | ... | @@ -983,7 +985,7 @@ def get_se_cms_compare_info_auto(application_id, last_obj, application_entity, d |
983 | elif id_info.get('idType') in ['Unified Social Credit Code', 'Tax Number', 'Business License Number']: | 985 | elif id_info.get('idType') in ['Unified Social Credit Code', 'Tax Number', 'Business License Number']: |
984 | # ['companyName', 'legalRepName', 'businessLicenseNo', 'organizationCreditCode', | 986 | # ['companyName', 'legalRepName', 'businessLicenseNo', 'organizationCreditCode', |
985 | # 'taxRegistrationCertificateNo', 'establishmentDate', 'businessLicenseDueDate'] | 987 | # 'taxRegistrationCertificateNo', 'establishmentDate', 'businessLicenseDueDate'] |
986 | id_num = decode_des(id_info.get('idNum', ''), des_key) | 988 | id_num = aes_decrypt_cbc(id_info.get('idNum', ''), des_key, des_iv) |
987 | bl_field_input = [ | 989 | bl_field_input = [ |
988 | ('companyName', customer_name), | 990 | ('companyName', customer_name), |
989 | ('legalRepName', legal_name), | 991 | ('legalRepName', legal_name), |
... | @@ -1172,7 +1174,7 @@ def get_se_cms_compare_info_auto(application_id, last_obj, application_entity, d | ... | @@ -1172,7 +1174,7 @@ def get_se_cms_compare_info_auto(application_id, last_obj, application_entity, d |
1172 | # 银行卡------------------------------------------------------------------------------------------------------- | 1174 | # 银行卡------------------------------------------------------------------------------------------------------- |
1173 | bank_info = {} | 1175 | bank_info = {} |
1174 | bank_name = cms_info.get('bankAccountDetails', {}).get('bankName', '') | 1176 | bank_name = cms_info.get('bankAccountDetails', {}).get('bankName', '') |
1175 | account_no = decode_des(cms_info.get('bankAccountDetails', {}).get('accountNo', ''), des_key) | 1177 | account_no = aes_decrypt_cbc(cms_info.get('bankAccountDetails', {}).get('accountNo', ''), des_key, des_iv) |
1176 | account_holder_name = cms_info.get('bankAccountDetails', {}).get('accountHolderName', '') | 1178 | account_holder_name = cms_info.get('bankAccountDetails', {}).get('accountHolderName', '') |
1177 | is_gsyh = True if '工商' in bank_name else False | 1179 | is_gsyh = True if '工商' in bank_name else False |
1178 | 1180 | ||
... | @@ -1631,7 +1633,7 @@ def get_se_cms_compare_info(application_id, last_obj, application_entity, detect | ... | @@ -1631,7 +1633,7 @@ def get_se_cms_compare_info(application_id, last_obj, application_entity, detect |
1631 | if id_info.get('idType') in consts.SE_CMS_FIRST_ID_FIELD_MAPPING: | 1633 | if id_info.get('idType') in consts.SE_CMS_FIRST_ID_FIELD_MAPPING: |
1632 | license_en, is_prc = consts.SE_CMS_FIRST_ID_FIELD_MAPPING[id_info['idType']] | 1634 | license_en, is_prc = consts.SE_CMS_FIRST_ID_FIELD_MAPPING[id_info['idType']] |
1633 | # ['customerName', 'idNum', 'dateOfBirth', 'idExpiryDate', 'hukouProvince'] | 1635 | # ['customerName', 'idNum', 'dateOfBirth', 'idExpiryDate', 'hukouProvince'] |
1634 | id_num = decode_des(id_info.get('idNum', ''), des_key) | 1636 | id_num = aes_decrypt_cbc(id_info.get('idNum', ''), des_key, des_iv) |
1635 | field_input = [('customerName', customer_name), ('idNum', id_num), | 1637 | field_input = [('customerName', customer_name), ('idNum', id_num), |
1636 | ('idExpiryDate', id_info.get('idExpiryDate', ''))] | 1638 | ('idExpiryDate', id_info.get('idExpiryDate', ''))] |
1637 | # if is_prc: | 1639 | # if is_prc: |
... | @@ -1643,7 +1645,7 @@ def get_se_cms_compare_info(application_id, last_obj, application_entity, detect | ... | @@ -1643,7 +1645,7 @@ def get_se_cms_compare_info(application_id, last_obj, application_entity, detect |
1643 | elif id_info.get('idType') in ['Unified Social Credit Code', 'Tax Number', 'Business License Number']: | 1645 | elif id_info.get('idType') in ['Unified Social Credit Code', 'Tax Number', 'Business License Number']: |
1644 | # ['companyName', 'legalRepName', 'businessLicenseNo', 'organizationCreditCode', | 1646 | # ['companyName', 'legalRepName', 'businessLicenseNo', 'organizationCreditCode', |
1645 | # 'taxRegistrationCertificateNo', 'establishmentDate', 'businessLicenseDueDate'] | 1647 | # 'taxRegistrationCertificateNo', 'establishmentDate', 'businessLicenseDueDate'] |
1646 | id_num = decode_des(id_info.get('idNum', ''), des_key) | 1648 | id_num = aes_decrypt_cbc(id_info.get('idNum', ''), des_key, des_iv) |
1647 | bl_field_input = [ | 1649 | bl_field_input = [ |
1648 | ('companyName', customer_name), | 1650 | ('companyName', customer_name), |
1649 | ('legalRepName', legal_name), | 1651 | ('legalRepName', legal_name), |
... | @@ -1832,7 +1834,7 @@ def get_se_cms_compare_info(application_id, last_obj, application_entity, detect | ... | @@ -1832,7 +1834,7 @@ def get_se_cms_compare_info(application_id, last_obj, application_entity, detect |
1832 | # 银行卡------------------------------------------------------------------------------------------------------- | 1834 | # 银行卡------------------------------------------------------------------------------------------------------- |
1833 | bank_info = {} | 1835 | bank_info = {} |
1834 | bank_name = cms_info.get('bankAccountDetails', {}).get('bankName', '') | 1836 | bank_name = cms_info.get('bankAccountDetails', {}).get('bankName', '') |
1835 | account_no = decode_des(cms_info.get('bankAccountDetails', {}).get('accountNo', ''), des_key) | 1837 | account_no = aes_decrypt_cbc(cms_info.get('bankAccountDetails', {}).get('accountNo', ''), des_key, des_iv) |
1836 | account_holder_name = cms_info.get('bankAccountDetails', {}).get('accountHolderName', '') | 1838 | account_holder_name = cms_info.get('bankAccountDetails', {}).get('accountHolderName', '') |
1837 | is_gsyh = True if '工商' in bank_name else False | 1839 | is_gsyh = True if '工商' in bank_name else False |
1838 | 1840 | ... | ... |
src/common/tools/aes.py
0 → 100644
1 | #这个有问题 | ||
2 | from Crypto.Cipher import AES | ||
3 | from base64 import b64encode, b64decode | ||
4 | |||
5 | |||
6 | def encrypt_ecb(data, key): | ||
7 | data = data.encode() | ||
8 | key = key.encode() | ||
9 | aes = AES.new(key, AES.MODE_CBC, bytes(16)) | ||
10 | res = aes.encrypt(pad(data, 32)) | ||
11 | return b64encode(res).decode() | ||
12 | |||
13 | |||
14 | def decrypt(data, key, iv): | ||
15 | key = key.encode() | ||
16 | iv = iv.encode() | ||
17 | # aes = AES.new(key, AES.MODE_CBC, bytes(16)) | ||
18 | aes = AES.new(key, AES.MODE_CBC, iv) | ||
19 | res = aes.decrypt(b64decode(data)) | ||
20 | return unpad(res, 32).decode() | ||
21 | |||
22 | |||
23 | def unpad(padded_data, block_size, style='pkcs7'): | ||
24 | pdata_len = len(padded_data) | ||
25 | if pdata_len == 0: | ||
26 | raise ValueError("Zero-length input cannot be unpadded") | ||
27 | if pdata_len % block_size: | ||
28 | raise ValueError("Input data is not padded") | ||
29 | if style in ('pkcs7', 'x923'): | ||
30 | padding_len = bord(padded_data[-1]) | ||
31 | if padding_len<1 or padding_len>min(block_size, pdata_len): | ||
32 | raise ValueError("Padding is incorrect.") | ||
33 | if style == 'pkcs7': | ||
34 | if padded_data[-padding_len:]!=bchr(padding_len)*padding_len: | ||
35 | raise ValueError("PKCS#7 padding is incorrect.") | ||
36 | else: | ||
37 | if padded_data[-padding_len:-1]!=bchr(0)*(padding_len-1): | ||
38 | raise ValueError("ANSI X.923 padding is incorrect.") | ||
39 | elif style == 'iso7816': | ||
40 | padding_len = pdata_len - padded_data.rfind(bchr(128)) | ||
41 | if padding_len<1 or padding_len>min(block_size, pdata_len): | ||
42 | raise ValueError("Padding is incorrect.") | ||
43 | if padding_len>1 and padded_data[1-padding_len:]!=bchr(0)*(padding_len-1): | ||
44 | raise ValueError("ISO 7816-4 padding is incorrect.") | ||
45 | else: | ||
46 | raise ValueError("Unknown padding style") | ||
47 | return padded_data[:-padding_len] | ||
48 | |||
49 | def pad(data_to_pad, block_size, style='pkcs7'): | ||
50 | padding_len = block_size-len(data_to_pad)%block_size | ||
51 | if style == 'pkcs7': | ||
52 | padding = bchr(padding_len)*padding_len | ||
53 | elif style == 'x923': | ||
54 | padding = bchr(0)*(padding_len-1) + bchr(padding_len) | ||
55 | elif style == 'iso7816': | ||
56 | padding = bchr(128) + bchr(0)*(padding_len-1) | ||
57 | else: | ||
58 | raise ValueError("Unknown padding style") | ||
59 | return data_to_pad + padding | ||
60 | |||
61 | def bord(s): | ||
62 | return s | ||
63 | def bchr(s): | ||
64 | return bytes([s]) | ||
65 | |||
66 | if __name__ == '__main__': | ||
67 | |||
68 | decrypt_data = decrypt('QkjNiuixpmtcxxqxaIZ30A==', 'm0XsOHC52YZ5KtakhpuMSZtF7DhwudmG', 'OCRocr2024UATocr') | ||
69 | print('解密:', decrypt_data) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
src/common/tools/aes_util.py
0 → 100644
1 | from Crypto.Cipher import AES | ||
2 | from base64 import b64encode, b64decode | ||
3 | |||
4 | def aes_encrypt_cbc(data, key, iv): | ||
5 | cipher = AES.new(key, AES.MODE_CBC, iv) | ||
6 | return cipher.encrypt(data) | ||
7 | |||
8 | def aes_decrypt_cbc(data, key, iv): | ||
9 | cipher = AES.new(key.encode(), AES.MODE_CBC, iv.encode()) | ||
10 | res = cipher.decrypt(b64decode(data)) | ||
11 | return res.decode('utf-8') | ||
12 | |||
13 | # 示例使用 | ||
14 | key = 'm0XsOHC52YZ5KtakhpuMSZtF7DhwudmG' # 密钥长度必须是16、24或32字节 | ||
15 | iv = 'OCRocr2024UATocr' | ||
16 | decrypted_data = aes_decrypt_cbc('QkjNiuixpmtcxxqxaIZ30A==', key, iv) | ||
17 | print("解密:", decrypted_data) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment