コード例 #1
0
ファイル: vote_security.py プロジェクト: aayn/secure-voting
def decrypt_vote(encrypted_vote, rsa_modulus, priv_exp):
    encrypted_vote = encrypted_vote.to_bytes(256, 'little')
    priv_key = import_key(construct((rsa_modulus, 65537, priv_exp)).exportKey())

    cipher_rsa = PKCS1_OAEP.new(priv_key)
    decrypted_vote = str(cipher_rsa.decrypt(encrypted_vote), 'utf-8')[:66]

    return decrypted_vote
コード例 #2
0
def decrypt_with_key(message, key):
    s = decode64(message)
    i = int(s)
    private_key = "-----BEGIN RSA PRIVATE KEY-----\n" + key + "\n-----END RSA PRIVATE KEY-----"
    private_key = import_key(private_key.encode("utf-8"))
    m = decrypt(i, private_key.d, private_key.n)
    m = int_to_string(m)

    return m
コード例 #3
0
ファイル: manager.py プロジェクト: olethanh/pyaleph
async def initialize_host(key, host='0.0.0.0', port=4025, listen=True, protocol_active=True):
    from .peers import publish_host, monitor_hosts
    from .protocol import AlephProtocol
    from .jobs import reconnect_p2p_job, tidy_http_peers_job

    assert key, "Host cannot be initialized without a key"

    tasks: List[Coroutine]

    priv = import_key(key)
    private_key = RSAPrivateKey(priv)
    public_key = private_key.get_public_key()
    keypair = KeyPair(private_key, public_key)
        
    transport_opt = f"/ip4/{host}/tcp/{port}"
    host = await new_node(transport_opt=[transport_opt],
                          key_pair=keypair)
    protocol = None
    # gossip = gossipsub.GossipSub([GOSSIPSUB_PROTOCOL_ID], 10, 9, 11, 30)
    # psub = Pubsub(host, gossip, host.get_id())
    flood = floodsub.FloodSub([FLOODSUB_PROTOCOL_ID, GOSSIPSUB_PROTOCOL_ID])
    psub = Pubsub(host, flood, host.get_id())
    if protocol_active:
        protocol = AlephProtocol(host)
    tasks = [
        reconnect_p2p_job(),
        tidy_http_peers_job(),
    ]
    if listen:
        from aleph.web import app
        
        await host.get_network().listen(multiaddr.Multiaddr(transport_opt))
        LOGGER.info("Listening on " + f'{transport_opt}/p2p/{host.get_id()}')
        ip = await get_IP()
        public_address = f'/ip4/{ip}/tcp/{port}/p2p/{host.get_id()}'
        http_port = app['config'].p2p.http_port.value
        public_http_address = f'http://{ip}:{http_port}'
        LOGGER.info("Probable public on " + public_address)
        # TODO: set correct interests and args here
        tasks += [
            publish_host(public_address, psub, peer_type="P2P"),
            publish_host(public_http_address, psub, peer_type="HTTP"),
            monitor_hosts(psub),
        ]

        # Enable message exchange using libp2p
        # host.set_stream_handler(PROTOCOL_ID, stream_handler)
        
    return (host, psub, protocol, tasks)
コード例 #4
0
def encrypt_with_key(message, key):
    public_key = "-----BEGIN PUBLIC KEY-----\n" + key + "\n-----END PUBLIC KEY-----"
    public_key = import_key(public_key.encode("utf-8"))

    max_length = public_key.n.bit_length() // 8

    if (len(message) > max_length):
        return "Tidak bisa melakukan proses enkripsi, maksimal panjang karakter adalah " + str(
            max_length) + " untuk RSA Key yang digunakan"

    c = encrypt(message, public_key.e, public_key.n)
    c = str(c)
    c = encode64(c).decode("utf-8")

    return c
コード例 #5
0
ファイル: manager.py プロジェクト: antonioandara/pyaleph
async def initialize_host(host='0.0.0.0',
                          port=4025,
                          key=None,
                          listen=True,
                          protocol_active=True):
    from .peers import publish_host, monitor_hosts
    from .protocol import PROTOCOL_ID, AlephProtocol
    from .jobs import reconnect_p2p_job, tidy_http_peers_job
    if key is None:
        keypair = generate_keypair(print_info=listen)
    else:
        priv = import_key(key)
        private_key = RSAPrivateKey(priv)
        public_key = private_key.get_public_key()
        keypair = KeyPair(private_key, public_key)

    transport_opt = f"/ip4/{host}/tcp/{port}"
    host = await new_node(transport_opt=[transport_opt], key_pair=keypair)
    protocol = None
    # gossip = gossipsub.GossipSub([GOSSIPSUB_PROTOCOL_ID], 10, 9, 11, 30)
    # psub = Pubsub(host, gossip, host.get_id())
    flood = floodsub.FloodSub([FLOODSUB_PROTOCOL_ID, GOSSIPSUB_PROTOCOL_ID])
    psub = Pubsub(host, flood, host.get_id())
    if protocol_active:
        protocol = AlephProtocol(host)
    asyncio.create_task(reconnect_p2p_job())
    asyncio.create_task(tidy_http_peers_job())
    if listen:
        from aleph.web import app

        await host.get_network().listen(multiaddr.Multiaddr(transport_opt))
        LOGGER.info("Listening on " + f'{transport_opt}/p2p/{host.get_id()}')
        ip = await get_IP()
        public_address = f'/ip4/{ip}/tcp/{port}/p2p/{host.get_id()}'
        http_port = app['config'].p2p.http_port.value
        public_http_address = f'http://{ip}:{http_port}'
        LOGGER.info("Probable public on " + public_address)
        # TODO: set correct interests and args here
        asyncio.create_task(publish_host(public_address, psub,
                                         peer_type="P2P"))
        asyncio.create_task(
            publish_host(public_http_address, psub, peer_type="HTTP"))
        asyncio.create_task(monitor_hosts(psub))
        # host.set_stream_handler(PROTOCOL_ID, stream_handler)

    return (host, psub, protocol)
コード例 #6
0
ファイル: solve.py プロジェクト: JaysesS/ctf_writeups
from Crypto.Util.number import long_to_bytes
from Crypto.PublicKey.RSA import import_key
from gmpy2 import lcm

c = 16267540901004879123859424672087486188548628828063789528428674467464407443871599865993337555869530486241139138650641838377419734897801380883629894166353225288006148210453677023750688175192317241440457768788267270422857060534261674538755743244831152470995124962736526978165448560149498403762447372653982922113772190234143253450918953235222315161964539311032659628670417496174123483045439359846360048774164337257829398345686635091862306204455687347443958931441225500856408331795261329035072585605404416473987280037959184981453888701567175803979981461050532113072292714696752692872526424122826696681194705563391161137426703690900733706866842363055967856443765215723398555522126909749236759332964873221973970368877565410624895160438695006432021529071866881905134494489266801004903504121740435965696128048690741210812963902631391765192187570107372453917327060678806282122942318369245760773848604249664378721970318257356486696764545

# Use RSACTFTOOL with e,n create private.key

pk = import_key(open('private.key', 'r').read())

# See note (p > q)
q, p = pk.p, pk.q

# See https://en.wikipedia.org/wiki/Schmidt-Samoa_cryptosystem

n = p * p * q
d = pow(n, -1, lcm(p - 1, q - 1))
m = long_to_bytes(pow(c, d, p * q)).decode()
print(m)
コード例 #7
0
ファイル: server.py プロジェクト: soreatu/My-CTF-Challenge
import os
import json
import time

from Crypto.PublicKey.RSA import import_key
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import hashes, hmac

from secret import FLAG, SALT

# generated by `openssl genrsa -out n1ogin.pem 2048`
PRIV_KEY = import_key(open("n1ogin.pem", "r").read())

# nonce for replay attack
Nonces = set()


def cal_password_hash(password):
    hash = password.encode() + SALT
    for _ in range(7777):  # enhanced secure
        digest = hashes.Hash(hashes.MD5())
        digest.update(hash)
        hash = digest.finalize()
    return hash


def RSA_decrypt(rsa_data):
    cc = int.from_bytes(rsa_data, 'big')
    mm = pow(cc, PRIV_KEY.d, PRIV_KEY.n)
    message = mm.to_bytes(2048 // 8, 'big')
コード例 #8
0
from Crypto.PublicKey.RSA import import_key
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import hashes, hmac
from pwn import *



admin_data = {"rsa_data": "391b06a1740b8c9cf1c8d2bb66ba5b191caa8534b4be18c22ce81069658dd2cd3ca3a8d1a3fc8dfab4b68a6b076bf89be807404e0a98dd1bf9daaf8ba34e0556131d3e56cae61c0302d24a177481209e82de7ecf91c2fe66aa39162d7af9c2fdabaf0c444badfc6b82b071fda8e3b26d4d3e57dba25c36298601ae0153c73b7469c472ac4702531c38849772e7c6e24313e6eb7def64a7bec1c21150c1fded52b3ca716d4444b4d75836dff8c92a371f6256ee7a48034f6d5ea949d982f9f05c04d3d7cce10bd11b806cc02088b42fa0cb069390700fb586287ba224ea0b210ebd0479a4f1d2ef5f914bcc861125b7d8d714cf0feecb515c1b1ef869e91ca179", "aes_data": "1709bf9489f6df6dc31491cee4711f7a2a3e050f1ed3e9772442e8a8483e341313713383dd31fbf0133d55e977b8edf54ba832002ee4ee52da32c260b083a35b01626201c36dad6fca7b2be2aa03d90bf5c9a601a24149f55cdcd39f0bf6a032bfabeebee5259a21e188f5c5f8776cd9d7c072054781169174bddbc390e6da21bd7b85f76c93f48914fb1958ac89e464511d9a17fb2174aab825cb13eb3f0dfa"}


conn = remote("43.155.59.224", 7777)
conn.readline()


PUB_KEY = import_key(open("n1ogin.pub", "r").read())

def send_data(data):
    envelope = json.dumps(data)
    st = time.time()
    conn.sendlineafter(b"> ", envelope.encode())
    res = conn.recvline().decode()
    en = time.time()
    return en - st

aesdata = bytes.fromhex(admin_data["aes_data"])
iv, cipher, mac = aesdata[:16], aesdata[16:-16], aesdata[-16:]
res = iv + cipher 


conn.sendline(b"asdf")
コード例 #9
0
async def initialize_host(key,
                          host="0.0.0.0",
                          port=4025,
                          listen=True,
                          protocol_active=True
                          ) -> Tuple[BasicHost, Pubsub, Any, List]:
    from .protocol import AlephProtocol
    from .jobs import reconnect_p2p_job, tidy_http_peers_job

    assert key, "Host cannot be initialized without a key"

    tasks: List[Coroutine]

    priv = import_key(key)
    private_key = RSAPrivateKey(priv)
    public_key = private_key.get_public_key()
    keypair = KeyPair(private_key, public_key)

    transport_opt = f"/ip4/{host}/tcp/{port}"
    host: BasicHost = await new_node(transport_opt=[transport_opt],
                                     key_pair=keypair)
    protocol = None
    # gossip = gossipsub.GossipSub([GOSSIPSUB_PROTOCOL_ID], 10, 9, 11, 30)
    # psub = Pubsub(host, gossip, host.get_id())
    flood = floodsub.FloodSub([FLOODSUB_PROTOCOL_ID, GOSSIPSUB_PROTOCOL_ID])
    psub = Pubsub(host, flood, host.get_id())
    if protocol_active:
        protocol = AlephProtocol(host)
    tasks = [
        reconnect_p2p_job(),
        tidy_http_peers_job(),
    ]
    if listen:
        from aleph.web import app

        await host.get_network().listen(multiaddr.Multiaddr(transport_opt))
        LOGGER.info("Listening on " + f"{transport_opt}/p2p/{host.get_id()}")
        ip = await get_IP()
        public_address = f"/ip4/{ip}/tcp/{port}/p2p/{host.get_id()}"
        http_port = app["config"].p2p.http_port.value
        public_adresses.append(public_address)

        public_http_address = f"http://{ip}:{http_port}"

        LOGGER.info("Probable public on " + public_address)
        # TODO: set correct interests and args here
        tasks += [
            publish_host(
                public_address,
                psub,
                peer_type="P2P",
                use_ipfs=app["config"].ipfs.enabled.value,
            ),
            publish_host(
                public_http_address,
                psub,
                peer_type="HTTP",
                use_ipfs=app["config"].ipfs.enabled.value,
            ),
            monitor_hosts_p2p(psub),
        ]

        if app["config"].ipfs.enabled.value:
            tasks.append(monitor_hosts_ipfs(app["config"]))
            try:
                public_ipfs_address = await get_public_address()
                tasks.append(
                    publish_host(public_ipfs_address,
                                 psub,
                                 peer_type="IPFS",
                                 use_ipfs=True))
            except Exception:
                LOGGER.exception("Can't publish public IPFS address")

        # Enable message exchange using libp2p
        # host.set_stream_handler(PROTOCOL_ID, stream_handler)

    return (host, psub, protocol, tasks)
コード例 #10
0
ファイル: pem.py プロジェクト: thesaltysleuth/Cryptohack
from Crypto.PublicKey.RSA import import_key

f = open('/root/Downloads/keypair_1f696c053d76a78c2c531bb013a92d4a.pem', 'r')

contents = f.read()

key = import_key(contents)

print(int(key.d))
コード例 #11
0
 def load_keys(extern_key: str or bytes, passphrase: str) -> RsaKey:
     return import_key(extern_key=extern_key, passphrase=passphrase)