Exemple #1
0
    def IsValid(self, ciphernum):
        plainnum = rsa.Decrypt(self._key, ciphernum)
        plainbytes = IntegerToBytes(plainnum)

        # Pads the message in front, if necessary. This is because if the number
        # had a leading zero, it would not be translated into bytes.
        while len(plainbytes) < KEY_BYTESIZE:
            plainbytes = b'\x00' + plainbytes

        return plainbytes[0] == 0 and plainbytes[1] == 2
Exemple #2
0
def RSASign(message, privKey):
    """Compute a proper signature for the message, with the private key."""
    n, d = privKey

    encodedMessage = PKCS1_v1_5_ENCODE(message)

    # Occasionally the string encoded in s turns out to be 257 bytes, instead
    # of the 256 we expect, and this throws an error. I suspect the key pair
    # generation is a bit wonky...
    m = BytesToInteger(encodedMessage)
    s = rsa.Decrypt(privKey, m)
    signature = IntegerToBytes(s, KEY_BYTESIZE)
    return signature
Exemple #3
0
 def IsEven(self, ciphernum):
     plainnum = rsa.Decrypt(self._key, ciphernum)
     return (plainnum % 2 == 0)
Exemple #4
0
#coding=UTF-8

import sys
import rsa
import genkey

savedStdout = sys.stdout  #保存标准输出流
with open('out.txt', 'w+') as file:
    sys.stdout = file  #标准输出重定向至文件
    dist = genkey.GenerateKey(1024)
    print(dist)
    print("nlen: ", len(str(bin(dist["n"]))))

    M = "I Love You! 我爱你 Я люблю тебя. " * 1
    C = rsa.Encrypt(dist, M, "UTF-8")
    Mr = rsa.Decrypt(dist, C, "UTF-8")

    print(C)
    print(Mr)
Exemple #5
0
    def decrypt(self, ciphertext):
        if ciphertext in self._past_ciphertexts:
            raise ValueError('Ciphertext was already decrypted')

        self._past_ciphertexts[ciphertext] = True
        return rsa.Decrypt(self._privateKey, ciphertext)