Example #1
0
def encrypt(N, e, message, L = ""):
    lHash = SHA.new(L).digest()
    hLen = len(lHash)
    mLen = len(message)
    k = numOctets(N)

    if mLen > k - (2*hLen) - 2:
        print "Message too long"
        return -1

    lPS = (k - mLen - 2*hLen - 2)
    PS = '\x00' * lPS
    DB = ''.join((lHash, PS, '\x01', message))

    seed = I2OSP(utils.gen_random(hLen * 8), hLen)

    dbMask = MGF(seed, k - hLen - 1)
    maskedDB = stringXOR(DB, dbMask)

    seedMask = MGF(maskedDB, hLen)
    maskedSeed = stringXOR(seed, seedMask)

    EM = ''.join(('\x00', maskedSeed, maskedDB))

    m = OS2IP(EM)

    c = RSAEP(N, e, m)

    cipherText = I2OSP(c, k)

    return cipherText
Example #2
0
def encrypt(N, e, message, L=""):
    lHash = SHA.new(L).digest()
    hLen = len(lHash)
    mLen = len(message)
    k = numOctets(N)

    if mLen > k - (2 * hLen) - 2:
        print "Message too long"
        return -1

    lPS = (k - mLen - 2 * hLen - 2)
    PS = '\x00' * lPS
    DB = ''.join((lHash, PS, '\x01', message))

    seed = I2OSP(utils.gen_random(hLen * 8), hLen)

    dbMask = MGF(seed, k - hLen - 1)
    maskedDB = stringXOR(DB, dbMask)

    seedMask = MGF(maskedDB, hLen)
    maskedSeed = stringXOR(seed, seedMask)

    EM = ''.join(('\x00', maskedSeed, maskedDB))

    m = OS2IP(EM)

    c = RSAEP(N, e, m)

    cipherText = I2OSP(c, k)

    return cipherText
Example #3
0
def generateKeys(security_level = 1):
    prime_size, random_size = 0, 0

    if security_level == 1: prime_size, random_size = 160, 864

    elif security_level == 2: prime_size, random_size = 224, 1824

    elif security_level == 3: prime_size, random_size = 256, 2816

    else: return

    q = number.getPrime(prime_size)
    k = number.getRandomInteger(random_size)
    p = k * q + 1

    while utils.rm_primality(p) != True:
        k = utils.gen_random(random_size)
        p = k * q + 1

    a = number.getRandomRange(0, p)
    g = utils.mod_exp(a, (p - 1) / q, p)

    while g == 1:
        a = number.getRandomRange(0, p)
        g = utils.mod_exp(a, (p - 1) / q, p)

    x = number.getRandomRange(1, q - 2)
    y = utils.mod_exp(g, x, p)

    return p, q, g, x, y
Example #4
0
def EMSAPSSENC(M, emBits, sLen = 16):
    mHash = SHA.new(M).digest()
    hLen = len(mHash)
    emLen = utils.ceil(emBits, 8)
    
    if emLen < hLen + sLen + 2:
        print "Encoding error"
        return
    
    salt = I2OSP(utils.gen_random(sLen * 8), sLen)
    m_prime = '\x00' * 8 + mHash + salt
    H = SHA.new(m_prime).digest()
    PS = '\x00' * (emLen - sLen - hLen - 2)
    DB = PS + '\x01' + salt
    dbMask = MGF(H, emLen - hLen - 1)
    maskedDB = stringXOR(DB, dbMask)
    octets, bits = (8 * emLen - emBits) / 8, (8 * emLen - emBits) % 8
    maskedDB = ('\x00' * octets) + maskedDB[octets:]
    newByte = chr(ord(maskedDB[octets]) & (255 >> bits))
    maskedDB = maskedDB[:octets] + newByte + maskedDB[octets+1:]
    EM = maskedDB + H + '\xbc'

    return EM
Example #5
0
def EMSAPSSENC(M, emBits, sLen=16):
    mHash = SHA.new(M).digest()
    hLen = len(mHash)
    emLen = utils.ceil(emBits, 8)

    if emLen < hLen + sLen + 2:
        print "Encoding error"
        return

    salt = I2OSP(utils.gen_random(sLen * 8), sLen)
    m_prime = '\x00' * 8 + mHash + salt
    H = SHA.new(m_prime).digest()
    PS = '\x00' * (emLen - sLen - hLen - 2)
    DB = PS + '\x01' + salt
    dbMask = MGF(H, emLen - hLen - 1)
    maskedDB = stringXOR(DB, dbMask)
    octets, bits = (8 * emLen - emBits) / 8, (8 * emLen - emBits) % 8
    maskedDB = ('\x00' * octets) + maskedDB[octets:]
    newByte = chr(ord(maskedDB[octets]) & (255 >> bits))
    maskedDB = maskedDB[:octets] + newByte + maskedDB[octets + 1:]
    EM = maskedDB + H + '\xbc'

    return EM
Example #6
0
                    # image caption
                    img_caption = ''
                    textfile = np.random.choice(text_list)
                    with open(args.pathText + textfile) as f:
                        text = f.read()
                        text_length = np.random.randint(
                            args.capLengthMin, args.capLengthMax)
                        text_starter = np.random.randint(
                            len(text) - text_length)
                        text = text[text_starter:(text_starter + text_length)]
                        section = img_caption

                    # img path
                    img_path = args.pathFig + np.random.choice(chart_list)

                img_width_ratio = utils.gen_random(args.imgWidthMinRatio,
                                                   args.imgWidthMaxRatio)
                img_width = int(img_width_ratio * column_width)

                img = Image.open(img_path)
                img_height = img_width * img.size[1] / img.size[0]
                if img_height > args.imgHeightMaxRatio * column_width:
                    img_height = img_height * 0.5
                    img_width = img_width * 0.5
                if img_height > args.pageHeight - offset_y - marginPt:
                    continue

                # caption
                if img_width_ratio > 0.8 or num_columns > 1:
                    caption_location = np.random.choice(['top', 'bottom'])
                else:
                    caption_location = np.random.choice(