Example #1
0
def test_astronaut():
    psf = np.ones((5, 5)) / 25
    image_noise = np.load(dirname(abspath(__file__)) + '/astronaut_noise.npy')
    image_desired = np.load(dirname(abspath(__file__)) + '/astronaut.npy')
    deconvolved = wiener.wiener(image_noise, psf, 1)
    np.testing.assert_allclose(deconvolved, image_desired, rtol=1e-3)
    deconvolved = wiener.wiener(image_noise, psf, 1, is_real=False)
    np.testing.assert_allclose(np.real(deconvolved),
                               image_desired,
                               rtol=1e-3,
                               atol=1e-3)
Example #2
0
def execute(e, n, method, f):
    if method == '(p-1)-метод':
        p, q = pollard_p_1(n)
        f.write("The result of the algorithm is: {}*{}".format(p, q))
    elif method == 'ро-Поллард':
        p, q = po_pollard_brent(n)
        f.write("The result of the algorithm is: {}*{}".format(p, q))
    elif method == 'малый-модуль-шифрования':
        m_1 = random.randint(2, n - 2)
        a = random.randint(2, n - 2)
        b = random.randint(2, n - 2)
        m_2 = (a * m_1 + b) % n
        c_1 = pow(m_1, e, n)
        c_2 = pow(m_2, e, n)
        l = [b, a]
        f.write("Selected parameters:\n")
        f.write("First cipher-text: {}\n".format(c_1))
        f.write("Second cipher-text: {}\n".format(c_2))
        f.write("message relations: m_1 = {} * m_2 + {} mod {}\n".format(
            a, b, n))
        try:
            m_1, m_2 = small_exp(c_1, c_2, l, e, n)
        except CustomException as e:
            f.write(
                "Found factor during the algorithm performance: {}*{}".format(
                    e.message, n // e.message))
        else:
            f.write("The result of the algorithm is: {} and {}".format(
                m_1, m_2))
    elif method == 'атака-Винера':
        p, q = wiener(e, n)
        if p:
            f.write("The result of the algorithm is: {}*{}".format(p, q))
        else:
            f.write(
                "Unable to achieve the result among the successive convergents."
            )
    elif method == 'итерация':
        m = random.randint(2, n - 2)
        c = pow(m, e, n)
        p, q = step(c, e, n)
        f.write("Used the cipher-text: {}\n".format(c))
        if q:
            f.write("The result of the algorithm is: {}*{}".format(p, q))
        else:
            f.write("The result is {}".format(p))
    else:
        exit("Unknown method")
    return 0
Example #3
0
def main():
    psf = np.ones((5, 5)) / 25
    #get the noised image
    img_noise, origin = get_imgs(psf)
    fig = plt.figure(figsize=(10, 5))
    #apply wiener deconvolution to noised image
    img_denoised = wiener(img_noise, psf, 0.7)
    #show result and comparison
    fig.add_subplot(1, 3, 1)
    plt.title("noised")
    plt.imshow(img_noise)
    fig.add_subplot(1, 3, 2)
    plt.title("denoised")
    plt.imshow(img_denoised)
    fig.add_subplot(1, 3, 3)
    plt.title("original")
    plt.imshow(origin)
    #plt.show()
    plt.savefig('camera.png')
Example #4
0
    ]).split('=')[1], 16)
l3c = libnum.s2n(open('almost_almost_there.encrypted', 'rb').read())

# small q, factored using ECM method or any simple method
l3q = 54311
l3p = l3n / l3q
l3phi = (l3p - 1) * (l3q - 1)
l3d = libnum.invmod(e, l3phi)
l3m = pow(l3c, l3d, l3n)
l3zippass = libnum.n2s(l3m)

################### LAYER 4 ######################
print "[*] Solving layer 4: Wieners attack!"
unzip = subprocess.check_output(
    ['unzip', '-o', '-P', l3zippass, 'almost_almost_there.zip'])
l4key = [(int(x.split(':')[3], 16)) for x in subprocess.check_output([
    'openssl', 'asn1parse', '-in', 'almost_there.pub', '-strparse', '19'
]).splitlines() if 'INTEGER' in x]
l4c = libnum.s2n(open('almost_there.encrypted', 'rb').read())

# use Wiener attack to find d from n and e
from wiener import wiener
l4d = wiener(l4key[1], l4key[0])
l4m = pow(l4c, l4d, l4key[0])
l4zippass = libnum.n2s(l4m)

############## GET FLAG ###############
unzip = subprocess.check_output(
    ['unzip', '-o', '-P', l4zippass, 'almost_there.zip'])
print "[+] Flag: " + open('FLAG', 'r').read()
Example #5
0
                imsave('denoise/tv/%s.png' % name, img)

        if (sys.argv[2] == 'wavelet'):

            for image in sys.argv[3:]:

                filename = os.path.basename(image)
                name, ext = os.path.splitext(filename)

                img = wavelet(image)
                os.makedirs('denoise/wavelet', exist_ok=True)

                imsave('denoise/wavelet/%s.png' % name, img)

        if (sys.argv[2] == 'wiener'):
            for image in sys.argv[3:]:

                filename = os.path.basename(image)
                name, ext = os.path.splitext(filename)

                img = wiener(image)

                os.makedirs('denoise/wiener', exist_ok=True)

                imsave('denoise/wiener/%s.png' % name, img)

    else:

        print("NO ARGUMENT PROVIDED")
Example #6
0
print "[*] Solving layer 3: Small q "
unzip = subprocess.check_output(['unzip','-o','-P',l2zippass,'almost_almost_almost_there.zip'])
l3n = int(subprocess.check_output(['openssl', 'rsa', '-noout', '-modulus', '-pubin', '-in', 'almost_almost_there.pub']).split('=')[1],16)
l3c = libnum.s2n(open('almost_almost_there.encrypted','rb').read())


# small q, factored using ECM method or any simple method
l3q = 54311
l3p = l3n / l3q
l3phi = (l3p - 1) * (l3q - 1)
l3d = libnum.invmod(e, l3phi)
l3m = pow(l3c,l3d,l3n)
l3zippass = libnum.n2s(l3m)

################### LAYER 4 ######################
print "[*] Solving layer 4: Wieners attack!"
unzip = subprocess.check_output(['unzip','-o','-P',l3zippass,'almost_almost_there.zip'])
l4key = [(int(x.split(':')[3],16)) for x in subprocess.check_output(['openssl', 'asn1parse', '-in', 'almost_there.pub','-strparse','19']).splitlines() if 'INTEGER' in x]
l4c = libnum.s2n(open('almost_there.encrypted','rb').read())

# use Wiener attack to find d from n and e
from wiener import wiener
l4d = wiener(l4key[1],l4key[0])
l4m = pow(l4c, l4d, l4key[0])
l4zippass = libnum.n2s(l4m)

############## GET FLAG ###############
unzip = subprocess.check_output(['unzip','-o','-P',l4zippass,'almost_there.zip'])
print "[+] Flag: " + open('FLAG','r').read()

Example #7
0
        x1 = x2
        y = y1
        y1 = y2
    return a, x, y


def modinv(x, N):
    g, alpha, _ = ex_gcd(x, N)
    assert alpha * x % N == 1
    return alpha


def powmod(x, e, N):
    if e >= 0:
        return pow(x, e, N)
    inv = modinv(x, N)
    return pow(inv, -e, N)


with open('captured_a4ff19205b4a6b0a221111296439b9c7') as f:
    next(f)
    for l in f:
        N, e, c = [literal_eval(x.strip()) for x in l.strip()[1:-1].split(':')]
        print "Checking..."
        res = wiener.wiener(e, N)
        if res:
            print "YES"
            P, Q, D = res
            m = powmod(c, D, N)
            print hex(m)[2:-1].decode("hex")
Example #8
0
    x1 = 0; y1 = 1
    x = 1; y = 0
    while b:
        q = a / b; r = a % b
        x2 = x - q * x1; y2 = y - q * y1
        a = b; b = r; x = x1; x1 = x2; y = y1; y1 = y2
    return a, x, y

def modinv(x, N):
    g, alpha, _ = ex_gcd(x, N)
    assert alpha * x % N == 1
    return alpha

def powmod(x, e, N):
    if e >= 0:
        return pow(x, e, N)
    inv = modinv(x, N)
    return pow(inv, -e, N)

with open('captured_a4ff19205b4a6b0a221111296439b9c7') as f:
    next(f)
    for l in f:
        N, e, c = [literal_eval(x.strip()) for x in l.strip()[1:-1].split(':')]
        print "Checking..."
        res = wiener.wiener(e, N)
        if res:
            print "YES"
            P, Q, D = res
            m = powmod(c, D, N)
            print hex(m)[2:-1].decode("hex")