Monday, April 25, 2016

[Python] Pycrypto: Python Cryptography Toolkit - Crypto.Cipher

Encryption Algorithms, menggunakan input string dan key untuk menghasilkan ciphertext. Chipertext memiliki 2 type antara lain Block Cipher dan Stream, untuk Block Ciphers bekerja pada ukuran 8 atau 16 byte, sedangkan stream ciphers bekerja pada bit-by-bit.
1. Block Cipher
Cipher
Key Size/Block Size
AES
16, 24, or 32 bytes/16 bytes
ARC2
Variable/8 bytes
Blowfish
Variable/8 bytes
CAST
Variable/8 bytes
DES
8 bytes/8 bytes
DES3 (Triple DES)
16 bytes/8 bytes
IDEA
16 bytes/8 bytes
RC5
Variable/8 bytes

DES (8 bytes)
pada DES/ECB menggunakan kunci 12345678 8-bytes dan plain text testtest


Python 2.7.9 (default, Mar  1 2015, 18:22:53) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from Crypto.Cipher import DES
>>> key = '12345678'
>>> des = DES.new(key, DES.MODE_ECB)
>>> plain = 'testtest'
>>> cipher = des.encrypt(plain)
>>> cipher
'{b\xaa\x0b\x15;\xc9e'
>>> cipher.encode('hex')
'7b62aa0b153bc965'
>>> cipher.encode('base64')
'e2KqCxU7yWU=\n'
>>> des.decrypt(cipher)
'testtest'
>>> 

Untuk cipher.encode('hex') mengubah hasil menjadi bentuk hexadecimal, kemudian cipher.encode('base64') menjadikan bentuk base64.

Blowfish

Python 2.7.9 (default, Mar  1 2015, 18:22:53) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import Blowfish
>>> key = '12345678'
>>> blowfish = Blowfish.new(key, Blowfish.MODE_ECB)
>>> plain = 'testtest'
>>> cipher = blowfish.encrypt(plain)
>>> cipher
'\\"r\x97\xcdVW\xc0'
>>> cipher.encode('hex')
'5c227297cd5657c0'
>>> cipher.encode('base64')
'XCJyl81WV8A=\n'
>>> blowfish.decrypt(cipher)
'testtest'
>>> 

AES (16 bytes)
key untuk AES di sini digunakan 16 bytes yaitu ini kuncinya mas dan plain ini isi pesannya.


Python 2.7.9 (default, Mar  1 2015, 18:22:53) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import AES
>>> key = 'ini kuncinya mas'
>>> aes = AES.new(key, AES.MODE_ECB)
>>> plain = "ini isi pesannya"
>>> cipher = aes.encrypt(plain)
>>> cipher
'\xd8\x8a\xb8\xe0\xed\xefK8\\\x13\xf5m\xf2\xd0\xaf5'
>>> cipher.encode('hex')
'd88ab8e0edef4b385c13f56df2d0af35'
>>> cipher.encode('base64')
'2Iq44O3vSzhcE/Vt8tCvNQ==\n'
>>> aes.decrypt(cipher)
'ini isi pesannya'
>>>

DES3 (16 bytes)

Python 2.7.9 (default, Mar  1 2015, 18:22:53) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import DES3
>>> key = 'ini kuncinya bro'
>>> des3 = DES3.new(key, DES3.MODE_ECB)
>>> plain = "ini isi pesannya"
>>> cipher = des3.encrypt(plain)
>>> cipher
'b\xb6\x0e\x96\xbefF\x15\x15\\Q\xc5\xac\x0b%b'
>>> cipher.encode('hex')
'62b60e96be664615155c51c5ac0b2562'
>>> cipher.encode('base64')
'YrYOlr5mRhUVXFHFrAslYg==\n'
>>> des3.decrypt(cipher)
'ini isi pesannya'
>> 

2. Stream Cipher
Cipher
Key Size
ARC4
Variable
XOR
Variable


ARC4

Python 2.7.9 (default, Mar  1 2015, 18:22:53) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import ARC4
>>> key1 = 'ganteng'
>>> key2 = 'ganteng'
>>> obj1 = ARC4.new(key1)
>>> obj2 = ARC4.new(key2)
>>> plain = 'aku ganteng banget'
>>> cipher = obj1.encrypt(plain)
>>> cipher
'\x9e}a"\xa0\xaaK\xfd\x94\x87\xd2\xac\x0ehW\xc9\x7fV'
>>> obj2.decrypt(cipher)
'aku ganteng banget'
>>>

XOR

Python 2.7.9 (default, Mar  1 2015, 18:22:53) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import XOR
>>> obj1 = XOR.new('ganteng')
>>> obj2 = XOR.new('ganteng')
>>> plain = 'aku sangat ganteng banget'
>>> cipher = obj1.encrypt(plain)
>>> cipher
'\x06\n\x1bT\x16\x0f\t\x00\x00\x1aT\x02\x0f\t\x13\x04\x00\x13E\x0c\x06\t\x06\x0b\x00'
>>> obj2.decrypt(cipher)
'aku sangat ganteng banget'
>>> cipher.encode('hex')
'060a1b54160f0900001a54020f0913040013450c0609060b00'
>>> cipher.encode('base64')
'BgobVBYPCQAAGlQCDwkTBAATRQwGCQYLAA==\n'
>>>

Referensi :

No comments:

Post a Comment