Update possible_ans.md

This commit is contained in:
Bill Buchanan
2022-02-03 19:04:25 +00:00
committed by GitHub
parent 08521e41c9
commit 9bc6da5818

View File

@@ -1,11 +1,71 @@
## A.1
openssl list-cipher-commands
openssl version
openssl prime hex 1111
openssl enc -aes-256-cbc -in myfile.txt -out encrypted.bin
openssl enc -aes-256-cbc -in myfile.txt -out encrypted.bin base64
openssl enc -d -aes-256-cbc -in encrypted.bin -pass pass:napier -base64
## D1
```python
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
import hashlib
import sys
import binascii
val='hello'
password='hello'
plaintext=val
def encrypt(plaintext,key, mode):
method=algorithms.AES(key)
cipher = Cipher(method, mode)
encryptor = cipher.encryptor()
ct = encryptor.update(plaintext) + encryptor.finalize()
return(ct)
def decrypt(ciphertext,key, mode):
method=algorithms.AES(key)
cipher = Cipher(method, mode)
decryptor = cipher.decryptor()
pl = decryptor.update(ciphertext) + decryptor.finalize()
return(pl)
def pad(data,size=128):
padder = padding.PKCS7(size).padder()
padded_data = padder.update(data)
padded_data += padder.finalize()
return(padded_data)
def unpad(data,size=128):
padder = padding.PKCS7(size).unpadder()
unpadded_data = padder.update(data)
unpadded_data += padder.finalize()
return(unpadded_data)
key = hashlib.sha256(password.encode()).digest()
plaintext=pad(plaintext.encode())
print("After padding (CMS): ",binascii.hexlify(bytearray(plaintext)))
ciphertext = encrypt(plaintext,key,modes.ECB())
print("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
plaintext = decrypt(ciphertext,key,modes.ECB())
plaintext = unpad(plaintext)
print(" decrypt: ",plaintext.decode())
```
## D2
@@ -97,61 +157,7 @@ Cipher (ECB): b'b2a350deec0b0718'
## D.3
Answer:
* /vA6BD+ZXu8j6KrTHi1Y+w== - italy
```python
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.backends import default_backend
import hashlib
import binascii
import base64
password='hello'
def encrypt(plaintext,key, mode):
method=algorithms.AES(key)
cipher = Cipher(method,mode, default_backend())
encryptor = cipher.encryptor()
ct = encryptor.update(plaintext) + encryptor.finalize()
return(ct)
def decrypt(ciphertext,key, mode):
method=algorithms.AES(key)
cipher = Cipher(method, mode, default_backend())
decryptor = cipher.decryptor()
pl = decryptor.update(ciphertext) + decryptor.finalize()
return(pl)
def pad(data,size=128):
padder = padding.PKCS7(size).padder()
padded_data = padder.update(data)
padded_data += padder.finalize()
return(padded_data)
def unpad(data,size=128):
padder = padding.PKCS7(size).unpadder()
unpadded_data = padder.update(data)
unpadded_data += padder.finalize()
return(unpadded_data)
key = hashlib.sha256(password.encode()).digest()
cipher='/vA6BD+ZXu8j6KrTHi1Y+w=='
ciphertext = base64.b64decode(cipher)
print("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
plaintext = decrypt(ciphertext,key,modes.ECB())
plaintext = unpad(plaintext)
print(" decrypt: ",plaintext.decode())
```
A sample is [here](https://repl.it/@billbuchanan/ch02ans05#main.py).
## E.1
Answers:
@@ -268,35 +274,60 @@ A sample is [here](https://repl.it/@billbuchanan/ch02ans07#main.py).
## E.3
In this case we will convert from Base-64 into a byte array and then try to decrypt:
Answer:
* /vA6BD+ZXu8j6KrTHi1Y+w== - italy
```python
from Crypto.Cipher import AES
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.backends import default_backend
import hashlib
import sys
import binascii
import Padding
import base64
password='hello'
def encrypt(plaintext,key, mode):
method=algorithms.AES(key)
cipher = Cipher(method,mode, default_backend())
encryptor = cipher.encryptor()
ct = encryptor.update(plaintext) + encryptor.finalize()
return(ct)
def decrypt(ciphertext,key, mode):
encobj = AES.new(key,mode)
return(encobj.decrypt(ciphertext))
method=algorithms.AES(key)
cipher = Cipher(method, mode, default_backend())
decryptor = cipher.decryptor()
pl = decryptor.update(ciphertext) + decryptor.finalize()
return(pl)
password = "hello"
c='1jDmCTD1IfbXbyyHgAyrdg=='
ciphertext = base64.b64decode(c)
print ("Cipher (ECB): ",binascii.hexlify(ciphertext))
def pad(data,size=128):
padder = padding.PKCS7(size).padder()
padded_data = padder.update(data)
padded_data += padder.finalize()
return(padded_data)
def unpad(data,size=128):
padder = padding.PKCS7(size).unpadder()
unpadded_data = padder.update(data)
unpadded_data += padder.finalize()
return(unpadded_data)
key = hashlib.sha256(password.encode()).digest()
plaintext = decrypt(ciphertext,key,AES.MODE_ECB)
plaintext = Padding.removePadding(plaintext.decode(),blocksize=Padding.AES_blocksize,mode='CMS')
print (" decrypt: ",plaintext)
print (" Key found: ",password)
```
A sample is [here](https://repl.it/@billbuchanan/ch02sample01#main.py).
cipher='/vA6BD+ZXu8j6KrTHi1Y+w=='
ciphertext = base64.b64decode(cipher)
print("Cipher (ECB): ",binascii.hexlify(bytearray(ciphertext)))
plaintext = decrypt(ciphertext,key,modes.ECB())
plaintext = unpad(plaintext)
print(" decrypt: ",plaintext.decode())
```
A sample is [here](https://repl.it/@billbuchanan/ch02ans05#main.py).
## F.1
Plaintext: norway