Python小白使用Crypto.Cipher遇到的问题

Python小白使用Crypto.Cipher遇到的问题

之前找了一个下载网易云音乐歌曲的文件,自己修改了某些参数还能用
后来换了一台电脑再运行的时候就报错了
求助一下大佬

下面是Traceback:

Traceback (most recent call last):
  File "H:\rpi\mu_code\NeteaseMusicDownloader(1).py", line 77, in 
    get_mp3(music.text, song_id)
  File "H:\rpi\mu_code\NeteaseMusicDownloader(1).py", line 48, in get_mp3
    "params": get_params(first_param).encode('utf-8'),
  File "H:\rpi\mu_code\NeteaseMusicDownloader(1).py", line 24, in get_params
    h_encText = AES_encrypt(text, first_key)
  File "H:\rpi\mu_code\NeteaseMusicDownloader(1).py", line 36, in AES_encrypt
    encryptor = AES.new(key, AES.MODE_CBC, iv)
  File "C:\Users\lgy\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\AES.py", line 232, in new
    return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "C:\Users\lgy\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\__init__.py", line 79, in _create_cipher
    return modes[mode](factory, **kwargs)
  File "C:\Users\lgy\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 274, in _create_cbc_cipher
    cipher_state = factory._create_base_cipher(kwargs)
  File "C:\Users\lgy\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\AES.py", line 103, in _create_base_cipher
    result = start_operation(c_uint8_ptr(key),
  File "C:\Users\lgy\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Util\_raw_api.py", line 235, in c_uint8_ptr
    raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type  cannot be passed to C code

源文件在这里:

from Crypto.Cipher import AES
from bs4 import BeautifulSoup
import base64
import requests
import urllib.request
import os
import time

path = 'C:/Users/lgy/Desktop'
flag = os.path.exists(path)
if not flag:
    os.mkdir(path)

headers = {
        'Referer': 'http://music.163.com/',
        'Host': 'music.163.com',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    }

def get_params(text):
    first_key = "0CoJUm6Qyw8W8jud"
    second_key = "FFFFFFFFFFFFFFFF"
    h_encText = AES_encrypt(text, first_key)
    h_encText = AES_encrypt(h_encText, second_key)
    return h_encText

def get_encSecKey():
    encSecKey = "257348aecb5e556c066de214e531faadd1c55d814f9be95fd06d6bff9f4c7a41f831f6394d5a3fd2e3881736d94a02ca919d952872e7d0a50ebfa1769a7a62d512f5f1ca21aec60bc3819a9c3ffca5eca9a0dba6d6f7249b06f5965ecfff3695b54e1c28f3f624750ed39e7de08fc8493242e26dbc4484a01c76f739e135637c"
    return encSecKey

def AES_encrypt(text, key):
    iv = "0102030405060708"
    pad = 16 - len(text) % 16
    text = text + pad * chr(pad)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    encrypt_text = encryptor.encrypt(text)
    encrypt_text = base64.b64encode(encrypt_text)
    encrypt_text = str(encrypt_text, encoding="utf-8")    
    return encrypt_text

def get_mp3(name, song_id):

    url = "http://music.163.com/weapi/song/enhance/player/url?csrf_token="

    first_param = '{ids:"[%s]", br:"128000", csrf_token:""}' % song_id
    data = {
        "params": get_params(first_param).encode('utf-8'),
        "encSecKey": get_encSecKey()
    }

    try:
        response = requests.post(url, headers=headers, data=data)
        result = response.json()

        if result['code'] != 200:
            print('!!! 歌曲[%s]下载失败...' % name)
            return

        mp3_url = result['data'][0]['url']
        urllib.request.urlretrieve(mp3_url, os.path.join('C:/Users/lgy/Desktop', name + '.mp3'))
        print('歌曲[%s]下载完成...' % name)
    except:
        print('!!!歌曲[%s]下载出现异常...' % name)

if __name__ == "__main__":
    print('开始下载歌曲...\n================================================')
    start_time = time.time()
    # url = input("输入歌单id:\n")
    play_url = 'http://music.163.com/playlist?id=2130557497'  # +url
    s = requests.session()
    s = BeautifulSoup(s.get(play_url, headers=headers).content, 'lxml')
    main = s.select('ul.f-hide li a')

    for music in main:
        song_id = music['href'][music['href'].find('id=') + len('id='):]
        get_mp3(music.text, song_id)

    end_time = time.time()
    print("程序耗时%f秒." % (end_time - start_time))

你可能感兴趣的:(Python)