Last active
August 29, 2015 14:26
-
-
Save kaito834/cbe5f18b44ef12eea671 to your computer and use it in GitHub Desktop.
This is snippet to encrypt or decrypt string by pyCrypto.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# | |
# I tested on Python 3.4.3/Windows 8.1 and Window 10. | |
# Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 | |
# https://github.com/dlitz/pycrypto | |
# Get Windows binary from http://www.voidspace.org.uk/python/pycrypto-2.6.1/ | |
from Crypto.Cipher import AES | |
import traceback | |
def main(): | |
print('Crypto.Cipher.AES: {0}'.format(AES.__file__)) | |
plaintext = input("Please input plain text:\n> ") | |
try: | |
# AES-CBC encryption | |
aesObj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') | |
ciphertext = aesObj.encrypt(plaintext) | |
print('Encrypted by AES-CBC: {0}'.format(ciphertext)) | |
# AES-CBC decryption | |
aesObj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') | |
print('Decrypted by AES-CBC: {0}'.format(aesObj.decrypt(ciphertext))) | |
except ValueError as e: | |
# ValueError will raise if length of plaintext is not multiple of 16 bytes. | |
# | |
# p.181 5.5 例外処理, ”Python文法詳解", O'REILLY | |
# https://docs.python.org/3/library/traceback.html#traceback.print_exc | |
print('[!] ValueError raised: {0}'.format(e)) | |
traceback.print_exc() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Git Bash on Windows 10 | |
$ git clone [email protected]:dlitz/pycrypto.git | |
Cloning into 'pycrypto'... | |
Enter passphrase for key '(snip)': | |
remote: Counting objects: 6554, done. | |
remote: Total 6554 (delta 0), reused 0 (delta 0), pack-reused 6554 | |
Receiving objects: 100% (6554/6554), 3.78 MiB | 1.37 MiB/s, done. | |
Resolving deltas: 100% (3401/3401), done. | |
Checking connectivity... done. | |
## Command Prompt on Windows 10 | |
$> C:\Python34\python.exe setup.py build bdist_wininst | |
running build | |
running build_py | |
creating build | |
creating build\lib.win32-3.4 | |
creating build\lib.win32-3.4\Crypto | |
copying lib\Crypto\pct_warnings.py -> build\lib.win32-3.4\Crypto | |
copying lib\Crypto\__init__.py -> build\lib.win32-3.4\Crypto | |
creating build\lib.win32-3.4\Crypto\Hash | |
copying lib\Crypto\Hash\CMAC.py -> build\lib.win32-3.4\Crypto\Hash | |
copying lib\Crypto\Hash\HMAC.py -> build\lib.win32-3.4\Crypto\Hash | |
copying lib\Crypto\Hash\MD5.py -> build\lib.win32-3.4\Crypto\Hash | |
copying lib\Crypto\Hash\RIPEMD.py -> build\lib.win32-3.4\Crypto\Hash | |
copying lib\Crypto\Hash\SHA.py -> build\lib.win32-3.4\Crypto\Hash | |
copying lib\Crypto\Hash\SHA1.py -> build\lib.win32-3.4\Crypto\Hash | |
copying lib\Crypto\Hash\__init__.py -> build\lib.win32-3.4\Crypto\Hash | |
creating build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\AES.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\ARC2.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\ARC4.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\blockalgo.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\Blowfish.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\CAST.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\DES.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\DES3.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\PKCS1_OAEP.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\PKCS1_v1_5.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\XOR.py -> build\lib.win32-3.4\Crypto\Cipher | |
copying lib\Crypto\Cipher\__init__.py -> build\lib.win32-3.4\Crypto\Cipher | |
creating build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\asn1.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\Counter.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\number.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\Padding.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\py3compat.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\randpool.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\RFC1751.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\winrandom.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\_number_new.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\_time.py -> build\lib.win32-3.4\Crypto\Util | |
copying lib\Crypto\Util\__init__.py -> build\lib.win32-3.4\Crypto\Util | |
creating build\lib.win32-3.4\Crypto\Random | |
copying lib\Crypto\Random\random.py -> build\lib.win32-3.4\Crypto\Random | |
copying lib\Crypto\Random\_UserFriendlyRNG.py -> build\lib.win32-3.4\Crypto\Random | |
copying lib\Crypto\Random\__init__.py -> build\lib.win32-3.4\Crypto\Random | |
creating build\lib.win32-3.4\Crypto\Random\Fortuna | |
copying lib\Crypto\Random\Fortuna\FortunaAccumulator.py -> build\lib.win32-3.4\Crypto\Random\Fortuna | |
copying lib\Crypto\Random\Fortuna\FortunaGenerator.py -> build\lib.win32-3.4\Crypto\Random\Fortuna | |
copying lib\Crypto\Random\Fortuna\SHAd256.py -> build\lib.win32-3.4\Crypto\Random\Fortuna | |
copying lib\Crypto\Random\Fortuna\__init__.py -> build\lib.win32-3.4\Crypto\Random\Fortuna | |
creating build\lib.win32-3.4\Crypto\Random\OSRNG | |
copying lib\Crypto\Random\OSRNG\fallback.py -> build\lib.win32-3.4\Crypto\Random\OSRNG | |
copying lib\Crypto\Random\OSRNG\nt.py -> build\lib.win32-3.4\Crypto\Random\OSRNG | |
copying lib\Crypto\Random\OSRNG\posix.py -> build\lib.win32-3.4\Crypto\Random\OSRNG | |
copying lib\Crypto\Random\OSRNG\rng_base.py -> build\lib.win32-3.4\Crypto\Random\OSRNG | |
copying lib\Crypto\Random\OSRNG\__init__.py -> build\lib.win32-3.4\Crypto\Random\OSRNG | |
creating build\lib.win32-3.4\Crypto\SelfTest | |
copying lib\Crypto\SelfTest\st_common.py -> build\lib.win32-3.4\Crypto\SelfTest | |
copying lib\Crypto\SelfTest\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest | |
creating build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\common.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_AES.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_ARC2.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_ARC4.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_Blowfish.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_CAST.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_DES.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_DES3.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_pkcs1_15.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_pkcs1_oaep.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\test_XOR.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
copying lib\Crypto\SelfTest\Cipher\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Cipher | |
creating build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\common.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_CMAC.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_HMAC.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_MD2.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_MD4.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_MD5.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_RIPEMD160.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_SHA1.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_SHA224.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_SHA256.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_SHA384.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\test_SHA512.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
copying lib\Crypto\SelfTest\Hash\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Hash | |
creating build\lib.win32-3.4\Crypto\SelfTest\Protocol | |
copying lib\Crypto\SelfTest\Protocol\test_AllOrNothing.py -> build\lib.win32-3.4\Crypto\SelfTest\Protocol | |
copying lib\Crypto\SelfTest\Protocol\test_chaffing.py -> build\lib.win32-3.4\Crypto\SelfTest\Protocol | |
copying lib\Crypto\SelfTest\Protocol\test_KDF.py -> build\lib.win32-3.4\Crypto\SelfTest\Protocol | |
copying lib\Crypto\SelfTest\Protocol\test_rfc1751.py -> build\lib.win32-3.4\Crypto\SelfTest\Protocol | |
copying lib\Crypto\SelfTest\Protocol\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Protocol | |
creating build\lib.win32-3.4\Crypto\SelfTest\PublicKey | |
copying lib\Crypto\SelfTest\PublicKey\test_DSA.py -> build\lib.win32-3.4\Crypto\SelfTest\PublicKey | |
copying lib\Crypto\SelfTest\PublicKey\test_ElGamal.py -> build\lib.win32-3.4\Crypto\SelfTest\PublicKey | |
copying lib\Crypto\SelfTest\PublicKey\test_import_DSA.py -> build\lib.win32-3.4\Crypto\SelfTest\PublicKey | |
copying lib\Crypto\SelfTest\PublicKey\test_import_RSA.py -> build\lib.win32-3.4\Crypto\SelfTest\PublicKey | |
copying lib\Crypto\SelfTest\PublicKey\test_RSA.py -> build\lib.win32-3.4\Crypto\SelfTest\PublicKey | |
copying lib\Crypto\SelfTest\PublicKey\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\PublicKey | |
creating build\lib.win32-3.4\Crypto\SelfTest\Random | |
copying lib\Crypto\SelfTest\Random\test_random.py -> build\lib.win32-3.4\Crypto\SelfTest\Random | |
copying lib\Crypto\SelfTest\Random\test_rpoolcompat.py -> build\lib.win32-3.4\Crypto\SelfTest\Random | |
copying lib\Crypto\SelfTest\Random\test__UserFriendlyRNG.py -> build\lib.win32-3.4\Crypto\SelfTest\Random | |
copying lib\Crypto\SelfTest\Random\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Random | |
creating build\lib.win32-3.4\Crypto\SelfTest\Random\Fortuna | |
copying lib\Crypto\SelfTest\Random\Fortuna\test_FortunaAccumulator.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\Fortuna | |
copying lib\Crypto\SelfTest\Random\Fortuna\test_FortunaGenerator.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\Fortuna | |
copying lib\Crypto\SelfTest\Random\Fortuna\test_SHAd256.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\Fortuna | |
copying lib\Crypto\SelfTest\Random\Fortuna\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\Fortuna | |
creating build\lib.win32-3.4\Crypto\SelfTest\Random\OSRNG | |
copying lib\Crypto\SelfTest\Random\OSRNG\test_fallback.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\OSRNG | |
copying lib\Crypto\SelfTest\Random\OSRNG\test_generic.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\OSRNG | |
copying lib\Crypto\SelfTest\Random\OSRNG\test_nt.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\OSRNG | |
copying lib\Crypto\SelfTest\Random\OSRNG\test_posix.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\OSRNG | |
copying lib\Crypto\SelfTest\Random\OSRNG\test_winrandom.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\OSRNG | |
copying lib\Crypto\SelfTest\Random\OSRNG\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Random\OSRNG | |
creating build\lib.win32-3.4\Crypto\SelfTest\Util | |
copying lib\Crypto\SelfTest\Util\test_asn1.py -> build\lib.win32-3.4\Crypto\SelfTest\Util | |
copying lib\Crypto\SelfTest\Util\test_Counter.py -> build\lib.win32-3.4\Crypto\SelfTest\Util | |
copying lib\Crypto\SelfTest\Util\test_number.py -> build\lib.win32-3.4\Crypto\SelfTest\Util | |
copying lib\Crypto\SelfTest\Util\test_Padding.py -> build\lib.win32-3.4\Crypto\SelfTest\Util | |
copying lib\Crypto\SelfTest\Util\test_winrandom.py -> build\lib.win32-3.4\Crypto\SelfTest\Util | |
copying lib\Crypto\SelfTest\Util\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Util | |
creating build\lib.win32-3.4\Crypto\SelfTest\Signature | |
copying lib\Crypto\SelfTest\Signature\test_pkcs1_15.py -> build\lib.win32-3.4\Crypto\SelfTest\Signature | |
copying lib\Crypto\SelfTest\Signature\test_pkcs1_pss.py -> build\lib.win32-3.4\Crypto\SelfTest\Signature | |
copying lib\Crypto\SelfTest\Signature\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\Signature | |
creating build\lib.win32-3.4\Crypto\SelfTest\IO | |
copying lib\Crypto\SelfTest\IO\test_PKCS8.py -> build\lib.win32-3.4\Crypto\SelfTest\IO | |
copying lib\Crypto\SelfTest\IO\__init__.py -> build\lib.win32-3.4\Crypto\SelfTest\IO | |
creating build\lib.win32-3.4\Crypto\Protocol | |
copying lib\Crypto\Protocol\AllOrNothing.py -> build\lib.win32-3.4\Crypto\Protocol | |
copying lib\Crypto\Protocol\Chaffing.py -> build\lib.win32-3.4\Crypto\Protocol | |
copying lib\Crypto\Protocol\KDF.py -> build\lib.win32-3.4\Crypto\Protocol | |
copying lib\Crypto\Protocol\__init__.py -> build\lib.win32-3.4\Crypto\Protocol | |
creating build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\DSA.py -> build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\ElGamal.py -> build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\pubkey.py -> build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\RSA.py -> build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\_DSA.py -> build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\_RSA.py -> build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\_slowmath.py -> build\lib.win32-3.4\Crypto\PublicKey | |
copying lib\Crypto\PublicKey\__init__.py -> build\lib.win32-3.4\Crypto\PublicKey | |
creating build\lib.win32-3.4\Crypto\Signature | |
copying lib\Crypto\Signature\PKCS1_PSS.py -> build\lib.win32-3.4\Crypto\Signature | |
copying lib\Crypto\Signature\PKCS1_v1_5.py -> build\lib.win32-3.4\Crypto\Signature | |
copying lib\Crypto\Signature\__init__.py -> build\lib.win32-3.4\Crypto\Signature | |
creating build\lib.win32-3.4\Crypto\IO | |
copying lib\Crypto\IO\PEM.py -> build\lib.win32-3.4\Crypto\IO | |
copying lib\Crypto\IO\PKCS8.py -> build\lib.win32-3.4\Crypto\IO | |
copying lib\Crypto\IO\_PBES.py -> build\lib.win32-3.4\Crypto\IO | |
copying lib\Crypto\IO\__init__.py -> build\lib.win32-3.4\Crypto\IO | |
Skipping implicit fixer: buffer | |
Skipping implicit fixer: idioms | |
Skipping implicit fixer: set_literal | |
Skipping implicit fixer: ws_comma | |
running build_ext | |
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath. | |
building 'Crypto.Random.OSRNG.winrandom' extension | |
error: Unable to find vcvarsall.bat | |
--- | |
I found Crypto.Random.OSRNG.winrandom after installing https://github.com/axper/python3-pycrypto-windows-installer. And, the file, winrandom.pyd, is same as Windows DLL. I will need to install Visual C++ Compiler 10; according to references, Windows SDK 7.1 contains the compiler. Visual Studio 2010 Express contains the compiler too, but it is difficult to find the installer now. | |
## References | |
- "Is a *.pyd file the same as a DLL?", Python on Windows FAQ | |
- https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll | |
- Windows での Python 2.7/3.4 の拡張モジュールビルド環境, Qiita (in Japanese) | |
- http://qiita.com/methane/items/2210712763b91e75fdf0 | |
- オレはこの OSS を Windows で使いたいだけなんじゃ@python (in Japanese) | |
- http://hhsprings.pinoko.jp/site-hhs/2015/05/%E3%82%AA%E3%83%AC%E3%81%AF%E3%81%93%E3%81%AE-oss-%E3%82%92-windows-%E3%81%A7%E4%BD%BF%E3%81%84%E3%81%9F%E3%81%84%E3%81%A0%E3%81%91%E3%81%AA%E3%82%93%E3%81%98%E3%82%83%EF%BC%A0python/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$> C:\Python34\python.exe pycrypto-aes.py | |
Crypto.Cipher.AES: C:\Python34\lib\site-packages\Crypto\Cipher\AES.py | |
Please input plain text: | |
> 1234567890123456 | |
Encrypted by AES-CBC: b'\xd5\x84~bj\xdf\xe7\xf9`L\x7f\xcf\xdb\xe2\xe17' | |
Decrypted by AES-CBC: b'1234567890123456' | |
$> C:\Python34\python.exe pycrypto-aes.py | |
Crypto.Cipher.AES: C:\Python34\lib\site-packages\Crypto\Cipher\AES.py | |
Please input plain text: | |
> 12345 | |
[!] ValueError raised: Input strings must be a multiple of 16 in length | |
Traceback (most recent call last): | |
File "pycrypto-aes.py", line 20, in main | |
ciphertext = aesObj.encrypt(plaintext) | |
File "C:\Python34\lib\site-packages\Crypto\Cipher\blockalgo.py", line 244, in | |
encrypt | |
return self._cipher.encrypt(plaintext) | |
ValueError: Input strings must be a multiple of 16 in length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment