ValueError:Python不支持RSA密钥格式

时间:2018-01-22 19:58:23

标签: python ubuntu rsa

你好当我的钱包系统出现此错误时ı将另一个钱包发送到某些硬币时它无法发送并且我收到此错误

我的代码:

def sendcloudcoin(request):
allify = {}
data = {}
if request.method == 'POST':
    senderpubkey = request.POST.get('spubkey')
    senderprivatekey = request.POST.get('sprikey')
    senderwalletid = request.POST.get('swid')
    receiver = request.POST.get('pubkey').strip()
    receiverhex  = hashlib.sha256(base64.b64decode(receiver)).hexdigest()
    amount = request.POST.get('amount').strip()

    if int(amount) <= 0:
        allify['response'] = "not enough amount"
        return HttpResponse(json.dumps(allify), content_type = "application/json")

    balance = getbalance(senderwalletid)
    if balance is None:
        balance = 0
    if int(amount) > int(balance):
        allify['response'] = "not enough amount"
        return HttpResponse(json.dumps(allify), content_type = "application/json")
    else:
        first_timestamp = time.time()
        data['senderpublickey'] = str(senderwalletid) #1
        data['receiverhex'] = str(receiverhex)      #2
        data['previous_hash'] = str(transaction.objects.all().last().blockhash) #3
        data['amount'] = str(amount) #4
        data['timestamp'] = str(first_timestamp) #5
        perfect =  miner(first_timestamp, senderwalletid, receiverhex, amount)
        data["nonce"] = str(perfect)
        data = collections.OrderedDict(sorted(data.items()))


        datashash  = hashlib.sha256(json.dumps(data).encode('utf-8')).hexdigest()
        # print(datashash)
        # print(datashash.encode('utf-8'))

        rsakey = RSA.importKey(senderprivatekey)
        digitalSignature = rsakey.sign(datashash.encode('utf-8'),'')
        digitalSignature = json.dumps(digitalSignature)

        newtrans = transaction(sender=base64.b64encode(senderpubkey.encode('utf-8')),
        senderhexdigest=senderwalletid,
        receiver=receiver,
        receiverhexdigest=receiverhex,
        prevblockhash=transaction.objects.all().last().blockhash,
        blockhash=datashash,
        amount=amount,
        nonce=perfect,
        first_timestamp=first_timestamp,
        P2PKH=digitalSignature,
        verification=True
        )
        newtrans.save()


        allify['response'] = "ok"
        allify['datashash'] = datashash
        allify['datastring'] = json.dumps(allify)
        return HttpResponse(json.dumps(allify), content_type = "application/json")


        allify['response'] = "ok"
        allify['datastring'] = json.dumps(allify)
        return HttpResponse(json.dumps(allify), content_type = "application/json")

我的追溯:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/venv/cloudbank/cloudbank/views.py", line 174, in sendcloudcoin
    rsakey = RSA.importKey(senderprivatekey)
  File "/usr/local/lib/python3.5/dist-packages/Crypto/PublicKey/RSA.py", line 682, in importKey
    raise ValueError("RSA key format is not supported")
ValueError: RSA key format is not supported

0 个答案:

没有答案