Python S/MIME Toolkit
Project description
Python S/MIME Toolkit
This library implements a S/MIME handler. It supports only S/MIME messages encryption using a public RSA key, in AES128-CBC, AES192-CBC or AES256-CBC modes.
The ASN.1 implementation does not depend on pyasn1, as it showed too broken for creating and reading CMS (Cryptographic Message Syntax). Instead, the asn1crypto was used in this project.
This implementation does not use the deprecated pycrypto anymore; instead it was switched to the more modern cryptography library. It is not ‘pure python’ anymore (because of the latter dependency), but at least works.
Requirements
Python 2.7 or Python 3.5+
cryptography
asn1crypto
Example
The code below loads Alice’s public key in PEM format and uses it to encrypt the e-mail in S/MIME format:
import sys import smime message = [ 'To: "Alice" <alice@foo.com>', 'From: "Bob" <bob@bar.com>', 'Subject: A message from python', '', 'Now you see me.' ] with open('alice-public-key.pem', 'rb') as pem: print(smime.encrypt('\n'.join(message), pem.read()))
Output:
To: "Alice" <alice@foo.com> From: "Bob" <bob@bar.com> Subject: A message from python MIME-Version: 1.0 Content-Type: application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=smime.p7m <base64-enveloped-data>
The same can be decrypted using OpenSSL from the command line:
$ openssl smime -decrypt -in smime.p7m -inkey alice-private-key.pem
OpenSSL expects that the smime.p7m file above should be in DER or PEM format. The latter should be enclosed in —–BEGIN PKCS7—– and —–END PKCS7—– and the content should be in base64 encoding, just like the output of the command above. Example:
-----BEGIN PKCS7----- MIIBdgYJKoZIhvcNAQcDoIIBZzCCAWMCAQAxgb4wgbsCAQAwJjASMRAwDgYDVQQD EwdDYXJsUlNBAhBGNGvHgABWvBHTbi7EELOwMAsGCSqGSIb3DQEBAQSBgCVAQwNg LmJ5ESYxOM1YbOLz2gvzWY1Fk+LZZiylYe7+o1/e/MjtzNwhnu+8vziFwHbXEH1Y jndIbUxiLyXb3omtNDunRICQin5bdo6BI7oE0MufUSqMjk0YUk8UQeNCiUfK89PR RfDclb1/sM3XZ7mUJa2OzpnuQIWec3MuJ3k4MIGcBgkqhkiG9w0BBwEwHQYJYIZI AWUDBAEqBBCVZVOt2lxSzmd+Ti1M372xgHDR0+ToLk1MJeTTtmJdnnNNH6631PN0 i3NJeJBKDDs4onI8xywqFtJP0of6GPoTGV/7D2vkgO2+jhCBTrzjYczbdOhh6Z5X o0i/81NPSoaLhrfwKMQvT7sXX7c9YdbTjyglyGqhXUN8h+mIRlP9IStD -----END PKCS7-----
Remember that the above formatting serves only for the purpose of testing the encryption with OpenSSL. Do not make such enclosing in e-mails.
License
This software is licensed under the Apache License 2.0. See the LICENSE file in the top distribution directory for the full license text.
Versioning
This software follows Semantic Versioning
CHANGES
0.1.0b1 (2019-01-06)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file smime-py23-0.1.0b1.tar.gz
.
File metadata
- Download URL: smime-py23-0.1.0b1.tar.gz
- Upload date:
- Size: 93.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15rc1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4a736d2cbfdeda9717dacd5f3e7a1f785a0e00a59d38906fb14444968111e66 |
|
MD5 | 5359226e83a3d433e561fd47a7e22bbf |
|
BLAKE2b-256 | 0b2935c7d188b35af6819301b2909581d258e712df60c965f4ee50ac01a0f99d |
File details
Details for the file smime_py23-0.1.0b1-py2.py3-none-any.whl
.
File metadata
- Download URL: smime_py23-0.1.0b1-py2.py3-none-any.whl
- Upload date:
- Size: 22.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15rc1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 140482a271904dcf2e12439397fb6d737300c2fdac0adf407060a5b5c1fbf5fc |
|
MD5 | e6f2dc5be2eaa4ff6623f42c813c1a41 |
|
BLAKE2b-256 | a736a5d12a3973bade2e979123557cf072142f054e67c8d9c932e6a8dd7e2455 |