Last active
October 9, 2017 22:16
-
-
Save vayn/007368db97e1a8930496 to your computer and use it in GitHub Desktop.
Example of PyCrypto AES decryption
This file contains hidden or 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
import base64 | |
import hashlib | |
import hmac | |
from Crypto.Cipher import AES | |
key = base64.decodebytes(b'v4QC6l4ttEogiBYvjLyvbA==') | |
nonce = base64.decodebytes(b'3iNVHJXuCfYoU9QP49DGqw==') | |
ct = base64.decodebytes(b'x9WM3Qy15Xw/2Z6pGVKXVA==') | |
cipher = AES.new(key, mode=AES.MODE_CTR, counter=lambda: nonce) | |
print(cipher.decrypt(ct)) | |
key = base64.decodebytes(b'esU5jdGCbM7E/ME5WBECJ+BdX3kt7bcQ3HkeEK+W6ZQ=') | |
message = b'Ceterum censeo Carthaginem esse delendam' | |
expected = 'b3240371a17e1e9755b89b23449f0d85c4c361e94e081c7adbe5a89c2d901aaa' | |
h = hmac.new(key, message, hashlib.sha256) | |
print(hmac.compare_digest(h.hexdigest(), expected)) |
This file contains hidden or 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
extern crate crypto; | |
extern crate rustc_serialize as serialize; | |
extern crate rand; | |
use crypto::digest::Digest; | |
use crypto::sha2::Sha256; | |
use crypto::aes::{self, KeySize}; | |
use crypto::symmetriccipher::SynchronousStreamCipher; | |
use crypto::hmac::Hmac; | |
use crypto::mac::Mac; | |
use serialize::hex::ToHex; | |
use serialize::base64::{STANDARD, ToBase64}; | |
use std::iter::repeat; | |
use rand::{OsRng, Rng}; | |
fn main() { | |
let input = "Hello world"; | |
let mut sha = Sha256::new(); | |
sha.input_str(input); | |
println!("{}", sha.result_str()); | |
println!(""); | |
let mut bytes: Vec<u8> = repeat(0u8).take(sha.output_bytes()).collect(); | |
sha.result(&mut bytes[..]); | |
println!("{}", bytes.to_base64(STANDARD)); | |
println!(""); | |
/* =========================== */ | |
let mut gen = OsRng::new().ok().expect("Failed to get OS random generator"); | |
let mut key: Vec<u8> = repeat(0u8).take(16).collect(); | |
gen.fill_bytes(&mut key); | |
let mut nonce: Vec<u8> = repeat(0u8).take(16).collect(); | |
gen.fill_bytes(&mut nonce); | |
println!("Key: {}", key.to_base64(STANDARD)); | |
println!("Nonce: {}", nonce.to_base64(STANDARD)); | |
let mut cipher = aes::ctr(KeySize::KeySize128, &key, &nonce); | |
let secret = "I like Nicelback"; | |
let mut output: Vec<u8> = repeat(0u8).take(secret.len()).collect(); | |
cipher.process(secret.as_bytes(), &mut output); | |
println!("Ciphertext: {}", output.to_base64(STANDARD)); | |
println!(""); | |
/* =========================== */ | |
let mut hmac_key: Vec<u8> = repeat(0u8).take(32).collect(); | |
gen.fill_bytes(&mut hmac_key); | |
let message = "Ceterum censeo Carthaginem esse delendam"; | |
println!("Message: {}", message); | |
println!("HMAC key: {}", hmac_key.to_base64(STANDARD)); | |
let mut hmac = Hmac::new(Sha256::new(), &hmac_key); | |
hmac.input(message.as_bytes()); | |
println!("HMAC digest: {}", hmac.result().code().to_hex()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment