I am writing a program here and I am trying to use AES encryption in python and store the encrypted hex in arduino’s EEPROM. The EEPROM is large enough, but i cannot for the life of me get it to write it to the memory. Here is the code I have made with AES
Python
import hashlib
import base58
import serial
import qrcode
import ecdsa
import ecdsa.der
import ecdsa.util
import time
import base64
import md5
from Crypto.Cipher import AES
import binascii
def privateKeyGenerator():
hexstart = "80" + start[0:64]
bytehex = str(bytearray.fromhex(hexstart))
hash1 = hashlib.sha256(bytehex).hexdigest()
bytehex2 = str(bytearray.fromhex(hash1))
hash2 = hashlib.sha256(bytehex2).hexdigest()
checksum = hash2[0:8]
hexfinal = hexstart + checksum
unencoded_final = str(bytearray.fromhex(hexfinal))
encoded_final = base58.b58encode(unencoded_final)
privFile = encoded_final[-8:]
print("Private Key: " + encoded_final)
img = qrcode.make(encoded_final)
img.save("Key_" + privFile + ".png")
return
def publicKeyGenerator():
sk = ecdsa.SigningKey.from_string(start[0:64].decode('hex'), curve=ecdsa.SECP256k1)
vk = sk.verifying_key
publicKey = ('\04' + sk.verifying_key.to_string()).encode('hex')
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(publicKey.decode('hex')).digest())
address = base58.b58encode_check(chr(0) + ripemd160.digest())
print("Public Address: " + address)
addrFile = address[-8:]
img2 = qrcode.make(address)
img2.save("Address_" + addrFile + ".png")
def read_selection():
while True:
selection = raw_input("Generate (Pri)vate Key or (Pub)lic Key or E(x)it: ")
if selection.lower() == "pri":
privateKeyGenerator()
elif selection.lower() == "pub":
publicKeyGenerator()
elif selection.lower() == "x":
break
else:
print("Please Choose Pri or Pub")
port = raw_input("Please enter port Arduino is connected to: ")
ser = serial.Serial(port, 9600)
while True:
selection = raw_input("Do you want to (Gen)erate a new address or (Re)ad out an existing one or E(x)it: ")
if selection.lower() == "gen":
confirmation = raw_input("Are you sure? THIS WILL OVERWRITE ANY EXITSTING ADDRESS AND IT CAN NEVER EVER BE RECOVERED (Y/N): ")
if confirmation.lower() == "y":
ser.write(b'1')
print("Generating...")
unencrypted = ser.readline()
time.sleep(1)
print unencrypted
password = raw_input("Enter a password for encryption: ")
BLOCK_SIZE = 32
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
secret = md5.new(password).digest()
cipher = AES.new(secret)
encrypted = EncodeAES(cipher, unencrypted)
byte = base64.b64decode(encrypted)
hexencrypted = binascii.hexlify(bytearray(byte))
print hexencrypted
ser.write(hexencrypted)
ser.write('/n')
print("Done")
ser.close()
ser.open()
else:
break
elif selection.lower() == "re":
ser.write(b'2')
start = ser.readline()
read_selection()
elif selection.lower() == "x":
break
else:
print("Please choose Gen or Re")
Arduino
#include "EEPROMAnything.h"
#include "Entropy.h"
#include "EEPROM.h"
int bitcount = 0;
int hexcount;
String privHex;
int privBin;
String privBinFinal;
int address = 0;
int value;
String hex;
char incoming;
void read_address()
{
while(address < 92)
{
value = EEPROM.read(address);
hex += String(value, HEX);
address = address + 1;
delay(10);
}
Serial.flush();
Serial.print(hex);
Serial.flush();
Serial.print("\n");
}
void new_address()
{
Entropy.initialize();
while(bitcount < 256)
{
hexcount = 0;
while(hexcount < 3)
{
privBin += (Entropy.random(2));
hexcount = hexcount + 1;
}
privHex += String(privBin, HEX);
bitcount = bitcount + 7;
}
Serial.print(privHex);
Serial.flush();
Serial.print("\n");
Serial.end();
Serial.begin(9600);
char encrypted[100];
while (Serial.available())
{
delay(1);
if (Serial.available() >0)
{
encrypted = Serial.read();
}
}
Serial.print(encrypted);
EEPROM_writeAnything(0, encrypted);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() != 0) {
incoming = Serial.read();
if(incoming == '1')
{
new_address();
}
if(incoming == '2')
{
delay(100);
read_address();
}
}
}
Please help.