JS与Python AES解密代码转换问题及解决方案
本文分析一个JS AES解密代码转换为Python代码后报错的问题。JS代码运行正常,但Python代码抛出binascii.Error: Invalid base64-encoded string: number of data characters (1053) cannot be 1 more than a multiple of 4
错误。
问题根源:Base64编码长度差异
错误信息提示Base64编码字符串长度不符合规范。JS和Python在处理Base64编码时,可能存在细微差异,导致最终编码结果长度不同。这通常与填充(padding)有关。Base64编码的长度总是4的倍数,不足时会用=
进行填充。如果JS代码在编码过程中自动处理了填充,而Python代码没有,就会导致解码失败。
JS代码:
const crypto = require('crypto'); function m(e) { return crypto.createHash('md5').update(e).digest(); } let o = 'ydsecret://query/key/b*rgygvywfnbwpmbazg*wt7sioup2t0c9whmzn39j^dadazhanxvgccy6vyfwnhl'; let n = 'ydsecret://query/iv/c@lze2yzhtz2cygaxksvfsb7y4qwhjitppz0nqp87fbej!iv6v^6fvi2wn@bypj4'; function decode(t) { if (!t) return null; const a = Buffer.alloc(16, m(o)); const r = Buffer.alloc(16, m(n)); const i = crypto.createDecipheriv('aes-128-cbc', a, r); let s = i.update(t, 'base64', 'utf-8'); s += i.final('utf-8'); return s; } let t = 'z21kd9zk1ke6ugku2ccwu4n6elnvodt0yggi0y3g-v0b9syqg8l9d6uernozyohqnydl2efznym6trc_xs-zktftk4hb6jp8xwcznh0avc8qitquiiu_4wkkxjlipvmvfkvjaazzax6vetpkr2fdkfot_jgbm2grsvj3r40autidlimeng8hc0zh4ww7utwutt3oo_zpxg0bsq9wepsab75-chkigkf9htipecl2bl84sbd1xdffczpkkqhecyss0jloxoqp2ltavxrrg58hp1q5uigzz_oo2-jmd-t1r4es40drcaq5bjms62m2vjf8d6ojtoh9jtfnwgzd3cxyn-pd7-tghmynejekfxtayxzpjlfqtrcyde3szuylenkqsl8wrra1hm-1ntfib-blcwadrbynnpp5_54aq_-gbsq8bb_9yex5ovzdb4_ry_spvvuunb39iplmhcdcnjod3ngiidbl9suz-9npjbx05zyrdpmfpal424qdoaxevqnvoh8jqfpzvqahmzu4mjg0sicdwfh7gp1zqgrbxd3esjt_ibinl3gict2xvuhh_nubcelktec6xbqedrqkpunmpzxjhjcvslhtcmsw0s9f0445ho9kt2qzydmbc3fs0oahputfu77gzpqn7sgiqh8vlixicutfvvop-1c-vu5qjfubln2-s5por9fgyg6rt6ioe_pgmwwj-cc00zum7fybfarktr4d3rk57r72qpxn4ja86zscammdg-m5z31rqh_v7echj8kna3go3ywkck4vtswowrfhis5rtz6ekrgc3skfkbb5vp8wop_84mybtgnbmj4cczhtq2hcoxrjf4def6ydt2ubxyv4btvgx9yx3ub4gx0ik5kyvfma6b_lnkrwk331wjuxkqtbgyiuwkr8j5qtvbmirvaa7aa19z4xmieaqbcuq5p4i9fcelthbrjd9yocouhk4u27xxywjjxcjotvzg7zwtiv76fhdeqlgawvjj7ww4nfgjhqc6aka_2afxa4c_lavvzgfukl3xscl7pfkxp6ghjcgkesrr80pt1gffw2xi8x4ejjnm_prsuz'; console.log(decode(t));
Python代码 (修正版):
import hashlib from Crypto.Cipher import AES import base64 def m(e): return hashlib.md5(e.encode()).digest() def decode(t): if not t: return None o = 'ydsecret://query/key/b*rgygvywfnbwpmbazg*wt7sioup2t0c9whmzn39j^dadazhanxvgccy6vyfwnhl' n = 'ydsecret://query/iv/c@lze2yzhtz2cygaxksvfsb7y4qwhjitppz0nqp87fbej!iv6v^6fvi2wn@bypj4' a = m(o) r = m(n) i = AES.new(a, AES.MODE_CBC, r) try: s = i.decrypt(base64.b64decode(t + "==")).decode('utf-8') # 添加填充 return s except Exception as e: print(f"Decoding error: {e}") return None t = 'z21kd9zk1ke6ugku2ccwu4n6elnvodt0yggi0y3g-v0b9syqg8l9d6uernozyohqnydl2efznym6trc_xs-zktftk4hb6jp8xwcznh0avc8qitquiiu_4wkkxjlipvmvfkvjaazzax6vetpkr2fdkfot_jgbm2grsvj3r40autidlimeng8hc0zh4ww7utwutt3oo_zpxg0bsq9wepsab75-chkigkf9htipecl2bl84sbd1xdffczpkkqhecyss0jloxoqp2ltavxrrg58hp1q5uigzz_oo2-jmd-t1r4es40drcaq5bjms62m2vjf8d6ojtoh9jtfnwgzd3cxyn-pd7-tghmynejekfxtayxzpjlfqtrcyde3szuylenkqsl8wrra1hm-1ntfib-blcwadrbynnpp5_54aq_-gbsq8bb_9yex5ovzdb4_ry_spvvuunb39iplmhcdcnjod3ngiidbl9suz-9npjbx05zyrdpmfpal424qdoaxevqnvoh8jqfpzvqahmzu4mjg0sicdwfh7gp1zqgrbxd3esjt_ibinl3gict2xvuhh_nubcelktec6xbqedrqkpunmpzxjhjcvslhtcmsw0s9f0445ho9kt2qzydmbc3fs0oahputfu77gzpqn7sgiqh8vlixicutfvvop-1c-vu5qjfubln2-s5por9fgyg6rt6ioe_pgmwwj-cc00zum7fybfarktr4d3rk57r72qpxn4ja86zscammdg-m5z31rqh_v7echj8kna3go3ywkck4vtswowrfhis5rtz6ekrgc3skfkbb5vp8wop_84mybtgnbmj4cczhtq2hcoxrjf4def6ydt2ubxyv4btvgx9yx3ub4gx0ik5kyvfma6b_lnkrwk331wjuxkqtbgyiuwkr8j5qtvbmirvaa7aa19z4xmieaqbcuq5p4i9fcelthbrjd9yocouhk4u27xxywjjxcjotvzg7zwtiv76fhdeqlgawvjj7ww4nfgjhqc6aka_2afxa4c_lavvzgfukl3xscl7pfkxp6ghjcgkesrr80pt1gffw2xi8x4ejjnm_prsuz' print(decode(t))
解决方案:手动添加Base64填充
修正后的Python代码在base64.b64decode
之前手动添加了=="
填充。 这确保了输入字符串长度是4的倍数,从而解决了解码错误。 如果仍然出现问题,请检查JS代码的Base64编码过程,确保与Python代码保持一致。 添加错误处理机制,可以更优雅地处理潜在的解码错误。
记住,在进行跨语言代码转换时,要格外注意数据类型的处理和编码方式的差异,避免因细微的差别导致程序错误。