Writing 96 byte string to EEPROM

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.

Hi, jujugoboom
Try it in this order maybe :

#include "EEPROM.h"
#include "EEPROMAnything.h"
#include "Entropy.h"

That doesn’t seem to work. Any other suggestions?

First, get rid of the String class and switch to either byte or char arrays. Next, try using the PROGMEM approach:

I would use progmem, but Im looking for long-term data storage.

Hmmm...I thought EEPROM was long term (non-volatile) data storage.

Yes it is, but PROGMEM writes data to the flash memory, where programs are stored, not EEPROM

Opps...my bad. Where's the EEPROM_writeAnything() function and what kind of error messages (if any) are you getting?

econjack:
...what kind of error messages (if any) are you getting?

Thats what I was thinking, just saying

...but i cannot for the life of me get it to write it to the memory.

Doesn't help any of us help you with your problems? What kind of problem? Why do you think you cannot write to it?

BTW, I dont know if you know, but Serial.flush() does not clear out the buffer, it

Waits for the transmission of outgoing serial data to complete.

And why are you you ending, then starting serial in new_address() ? makes no sense

Ps991:
Thats what I was thinking, just saying
Doesn't help any of us help you with your problems? What kind of problem? Why do you think you cannot write to it?

BTW, I dont know if you know, but Serial.flush() does not clear out the buffer, it
And why are you you ending, then starting serial in new_address() ? makes no sense

I am not getting any errors. It all compiles and runs. Whenever I read what is written to the EEPROM, however, it is nothing near what it should be. I use Serial.flush() a little liberally just to try to get it to work, and I have the serial open and close to clear out anything that may be causing an issue.

while(Serial.available() > 0); this will do the same thing as truly flushing the buffer.

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");
}

Why don’t you use EEPROM_readAnything?