예제 #1
0
def main():
    certs = (load_cert('demo2_user1.crt.pem'), )
    datau = open('smime-unsigned.txt', 'rb').read()
    datae = email.encrypt(datau, certs, 'aes256_ofb')
    open('smime-encrypted.txt', 'wt').write(datae)
    datae = email.encrypt(datau, certs, 'aes256_ofb', True)
    open('smime-encrypted-oaep.txt', 'wt').write(datae)
예제 #2
0
    def test_email_ssl_decrypt(self):
        def load_cert(fname):
            with open(fname, 'rb') as f:
                cert_bytes = f.read()
                return crypto.load_certificate(crypto.FILETYPE_PEM, cert_bytes)

        certs = (load_cert(fixture('demo2_user1.crt.pem')), )
        with open(fixture('smime-unsigned.txt'), 'rb') as fh:
            datau = fh.read()
        datae = email.encrypt(datau, certs, 'aes256_ofb')
        fname = fixture('smime-encrypted.txt')
        with open(fname, 'wt') as fh:
            fh.write(datae)

        cmd = [
            'openssl',
            'smime',
            '-decrypt',
            '-recip',
            fixture('demo2_user1.crt.pem'),
            '-inkey',
            fixture('demo2_user1.key.pem'),
            '-in',
            fname,
        ]
        process = Popen(cmd, stdout=PIPE, stderr=PIPE)
        stdout, stderr = process.communicate()

        assert stderr == b''
        lastbyte = stdout[-1]
        stdout = stdout[:len(stdout) - lastbyte]
        assert stdout == datau
예제 #3
0
    def test_email_ssl_decrypt(self):
        def load_cert(relative_path):
            with open(relative_path, 'rb') as f:
                return x509.load_pem_x509_certificate(f.read(), backends.default_backend())
        certs = (
            load_cert(fixture('demo2_user1.crt.pem')),
        )
        with open(fixture('smime-unsigned.txt'), 'rb') as fh:
            datau = fh.read()
        datae = email.encrypt(datau, certs, 'aes256_ofb')
        fname = fixture('smime-encrypted.txt')
        with open(fname, 'wt') as fh:
            fh.write(datae)

        cmd = [
            'openssl', 'smime', '-decrypt',
            '-recip', fixture('demo2_user1.crt.pem'),
            '-inkey', fixture('demo2_user1.key.pem'),
            '-passin', 'pass:1234',
            '-in', fname,
        ]
        process = Popen(cmd, stdout=PIPE, stderr=PIPE)
        stdout, stderr = process.communicate()

        assert stderr == b''
        lastbyte = stdout[-1]
        stdout = stdout[:len(stdout)-lastbyte]
        assert stdout == datau
예제 #4
0
    def test_email_crypt(self):
        def load_cert(fname):
            with open(fname, 'rb') as f:
                cert_bytes = f.read()
                return crypto.load_certificate(crypto.FILETYPE_PEM, cert_bytes)

        certs = (load_cert(fixture('demo2_user1.crt.pem')), )
        datau = open(fixture('smime-unsigned.txt'), 'rb').read()
        datae = email.encrypt(datau, certs, 'aes256_ofb')
        fname = fixture('smime-encrypted.txt')
        open(fname, 'wt').write(datae)

        key = crypto.load_privatekey(
            crypto.FILETYPE_PEM,
            open(fixture('demo2_user1.key.pem'), 'rb').read(), b'1234')
        key = key.to_cryptography_key()
        datae = io.open(fname, 'rt', encoding='utf-8').read()
        datad = email.decrypt(datae, key)

        assert datau == datad
예제 #5
0
    def test_email_crypt(self):
        def load_cert(relative_path):
            with open(relative_path, 'rb') as f:
                return x509.load_pem_x509_certificate(f.read(), backends.default_backend())
        certs = (
            load_cert(fixture('demo2_user1.crt.pem')),
        )
        with open(fixture('smime-unsigned.txt'), 'rb') as fh:
            datau = fh.read()
        datae = email.encrypt(datau, certs, 'aes256_ofb')
        fname = fixture('smime-encrypted.txt')
        with open(fname, 'wt') as fh:
            fh.write(datae)

        with open(fixture('demo2_user1.key.pem'), 'rb') as fh:
            key = load_pem_private_key(fh.read(), b'1234', backends.default_backend())
        with io.open(fname, 'rt', encoding='utf-8') as fh:
            datae = fh.read()
        datad = email.decrypt(datae, key)

        assert datau == datad