mirror of
https://github.com/billbuchanan/appliedcrypto.git
synced 2026-02-21 19:27:58 +00:00
Update possible_ans.md
This commit is contained in:
@@ -1,96 +1,5 @@
|
||||
|
||||
|
||||
## Q1
|
||||
You may need to install "Crypto" with:
|
||||
<pre>
|
||||
pip install pycrypto
|
||||
</pre>
|
||||
And Padding with:
|
||||
<pre>
|
||||
pip install padding
|
||||
</pre>
|
||||
|
||||
Note: The Padding library has not implemented the fully range of padding methods.
|
||||
|
||||
```python
|
||||
from Crypto.Cipher import AES
|
||||
import hashlib
|
||||
import sys
|
||||
import binascii
|
||||
import Padding
|
||||
|
||||
val='hello'
|
||||
password='hello'
|
||||
|
||||
plaintext=val
|
||||
|
||||
def encrypt(plaintext,key, mode):
|
||||
encobj = AES.new(key,mode)
|
||||
return(encobj.encrypt(plaintext))
|
||||
|
||||
def decrypt(ciphertext,key, mode):
|
||||
encobj = AES.new(key,mode)
|
||||
return(encobj.decrypt(ciphertext))
|
||||
|
||||
key = hashlib.sha256(password.encode()).digest()
|
||||
|
||||
|
||||
plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode='CMS')
|
||||
|
||||
print ("After padding (CMS): ",binascii.hexlify(bytearray(plaintext.encode())))
|
||||
|
||||
ciphertext = encrypt(plaintext.encode(),key,AES.MODE_ECB)
|
||||
print ("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
|
||||
|
||||
plaintext = decrypt(ciphertext,key,AES.MODE_ECB)
|
||||
plaintext = Padding.removePadding(plaintext.decode(),mode='CMS')
|
||||
print (" decrypt: ",plaintext)
|
||||
|
||||
|
||||
plaintext=val
|
||||
|
||||
### Other padding methods have not been implemented in the library
|
||||
|
||||
plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode='ZeroLen')
|
||||
|
||||
print ("After padding (CMS): ",binascii.hexlify(bytearray(plaintext.encode())))
|
||||
|
||||
ciphertext = encrypt(plaintext.encode(),key,AES.MODE_ECB)
|
||||
print ("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
|
||||
|
||||
plaintext = decrypt(ciphertext,key,AES.MODE_ECB)
|
||||
plaintext = Padding.removePadding(plaintext.decode(),mode='ZeroLen')
|
||||
print (" decrypt: ",plaintext)
|
||||
|
||||
plaintext=val
|
||||
|
||||
plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode='Space')
|
||||
|
||||
print ("After padding (CMS): ",binascii.hexlify(bytearray(plaintext.encode())))
|
||||
|
||||
ciphertext = encrypt(plaintext.encode(),key,AES.MODE_ECB)
|
||||
print ("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
|
||||
|
||||
plaintext = decrypt(ciphertext,key,AES.MODE_ECB)
|
||||
plaintext = Padding.removePadding(plaintext.decode(),mode='Space')
|
||||
print (" decrypt: ",plaintext)
|
||||
|
||||
|
||||
plaintext=val
|
||||
|
||||
plaintext = Padding.appendPadding(plaintext,blocksize=Padding.AES_blocksize,mode='Random')
|
||||
|
||||
print ("After padding (Random): ",binascii.hexlify(bytearray(plaintext.encode())))
|
||||
|
||||
ciphertext = encrypt(plaintext.encode(),key,AES.MODE_ECB)
|
||||
print ("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
|
||||
|
||||
plaintext = decrypt(ciphertext,key,AES.MODE_ECB)
|
||||
plaintext = Padding.removePadding(plaintext.decode(),mode='Random')
|
||||
print (" decrypt: ",plaintext)
|
||||
```
|
||||
An example is [here](https://repl.it/@billbuchanan/ch02ans01#main.py).
|
||||
|
||||
|
||||
|
||||
## Q2
|
||||
|
||||
Reference in New Issue
Block a user