コード例 #1
0
def testRSA(rsaKey, numRuns):
    errors = 0
    for i in range(numRuns):
        m = random.randrange(0,255)
        mq = BaseFour(m)
        mqi = mq.getIntVal()
        c = rsaKey.encryptBaseTen(mqi)
        mpi = rsaKey.decryptBaseTen(c)
        mp = quaternary.intValToBaseFour(mpi)

        mf = quaternary.toDecimal(mp)
        #print 'Run  ' + str(i)
        #print 'm:   ' + str(m)
        #print 'mqi: ' + str(mqi)
        #print 'c:   ' + str(c)
        #print 'mpi: ' + str(mpi)

        #print 'mf:  ' + str(mf)
        if m != mf or mqi != mpi:
            print 'ERROR: m=' + str(m) + ', mf=' + str(mf)
            print 'ERROR: mqi=' + str(mqi) + ', mpi=' + str(mpi)
            #i = raw_input("")
            errors = errors + 1
        #print ''
    print "Test completed " + str(numRuns) + " runs with " + str(errors) + " errors"
    if errors > 0:
        print "RSA Key Values:"
        e,d,n = rsaKey.getKeys()
        print 'public key:  ' + str(e)
        print 'private key: ' + str(d)
        print 'mod: ' + str(n)
    print ''
    raw_input("Press enter to continue...")
コード例 #2
0
def decrypt(fname,rsaKey,enableWrite,enableMessages):
    if(enableMessages):
        print("Decrypting " + fname + "..."),
    
    f = open(fname, "r")
    fContents = f.read().split(".")
    fPix = fContents[1].split(",")
    fDimensions = fContents[0].split(",")
    imgwidth = int(fDimensions[0])
    imgheight = int(fDimensions[1])
    
    im = Image.new("RGB", (imgwidth, imgheight))
    pix = im.load()
    
    error = False
    
    tenpercent = imgwidth / 10
    
    for x in range(imgwidth):
        
        if(enableMessages):
            printPercentage(x, tenpercent)
        
        row = fPix[(x*imgheight)-imgheight:x*imgheight]
        for y in range(len(row)):
            rgb = row[y].split('/')
            #print "(" + str(x) + "," + str(y) + ")="+rgb[0]+","+rgb[1]+","+rgb[2]
            #for i in rgb:
            #    if i == '':
            #        i = '0'
            r_i = rsaKey.decryptBaseTen(int("0x" + rgb[0],16))
            g_i = rsaKey.decryptBaseTen(int("0x" + rgb[1],16))
            b_i = rsaKey.decryptBaseTen(int("0x" + rgb[2],16))
        
            r_f = quaternary.intValToBaseFour(r_i)
            g_f = quaternary.intValToBaseFour(g_i)
            b_f = quaternary.intValToBaseFour(b_i)
        
            r = quaternary.toDecimal(r_f)
            g = quaternary.toDecimal(g_f)
            b = quaternary.toDecimal(b_f)
            
            
            if r > 255 or g > 255 or b > 255:
                print ''
                print ''
                print "****************ERROR*****************"
                print "Decrypted RGB values greater than 255 "
                print "RSA  key is probably incorrect.       "
                print "**************************************"
                print ''
                return None
            
            rgb_n = (r,g,b)
            
            pix[x,y] = rgb_n
            
    fNameComp = fname.split(".")
    outfname = fNameComp[0]
    exten = fNameComp[1]
    
    im.show()
    if(enableWrite):
        im.save(outfname + "_OUTPUT." + exten)
    return outfname + "_OUTPUT." + exten