diff --git a/ATMegaCard/Makefile b/ATMegaCard/Makefile
index cb0f90049378c8d8999afceb40a7ca7e8439b566..4288b3519326f3857feba8828ef383dea1203916 100644
--- a/ATMegaCard/Makefile
+++ b/ATMegaCard/Makefile
@@ -54,7 +54,7 @@ OPT = s
 # If there is more than one source file, append them above, or modify and
 # uncomment the following:
 
-SRC = main.c software_serial.c ISO7816-card.c keystore.c rng.c ../common/sha1/sha1.c applications/portal/portal.c
+SRC = main.c software_serial.c ISO7816-card.c rng.c ../common/sha1/sha1.c applications/portal/keystore.c applications/portal/portal.c
 
 # List Assembler source files here.
 # Make them always end in a capital .S.  Files ending in a lowercase .s
diff --git a/ATMegaCard/keystore.c b/ATMegaCard/applications/portal/keystore.c
similarity index 100%
rename from ATMegaCard/keystore.c
rename to ATMegaCard/applications/portal/keystore.c
diff --git a/ATMegaCard/keystore.h b/ATMegaCard/applications/portal/keystore.h
similarity index 100%
rename from ATMegaCard/keystore.h
rename to ATMegaCard/applications/portal/keystore.h
diff --git a/ATMegaCard/eeprom_gen.rb b/ATMegaCard/eeprom_gen.rb
new file mode 100755
index 0000000000000000000000000000000000000000..aa0e0db1a64f34306de28f071982ce4b20efd91b
--- /dev/null
+++ b/ATMegaCard/eeprom_gen.rb
@@ -0,0 +1,74 @@
+#!/usr/bin/ruby
+
+KEYSTORE_OFFSET=127
+KEY_LENGTH=64
+
+class IntelHexWriter
+	
+	RECORD_LENGTH=0x20
+	
+	RECORD_TYPE_DATA = 0x00
+	
+	def initialize(filename)
+		@file = File.open(filename, "w")
+		@offset = 0
+		@record = ""
+	end
+	
+	public; def write_data(data)
+		while(data && data.length > 0)
+			bytesleft = RECORD_LENGTH - @record.length
+			@record += data[0, bytesleft]
+			data = data[bytesleft..-1]
+			
+			write_record() if (data && data.length > 0)
+		end
+	end
+	
+	private; def write_record()
+		r = (":%02X%04X%02X" % [@record.length, @offset, RECORD_TYPE_DATA])
+		checksum = @record.length + (@offset & 0xFF) + (@offset >> 8) + RECORD_TYPE_DATA
+		r += @record.bytes.map{|b| checksum=(checksum+b) & 0xFF; "%02X" % b}.join()
+		r += "%02X" % ((((checksum & 0xFF) ^ 0xFF) + 1) & 0xFF)
+		r += "\r\n"
+		@file.write(r)
+		
+		@record = ""
+		@offset += RECORD_LENGTH
+	end
+	
+	public; def close()
+		write_record() if (@record.length > 0)
+		@file.write(":00000001FF\n")
+		@file.close
+	end
+	
+end
+
+def hex2bin(hex)
+	c = hex.length / 2
+	bin = ('.'.encode('ASCII-8BIT'))*c
+	(0...c).each{|i|
+		bin.setbyte(i,hex[i << 1, 2].hex)
+	}
+	return bin
+end
+
+eeprom_file = ARGV[0]
+keystring = ARGV[1]
+
+if ((!eeprom_file.kind_of?(String)) || (!keystring.kind_of?(String)) || keystring.length != (2*KEY_LENGTH))
+	puts "Usage: #{$0} hexfilename keystring"
+	exit 1
+end
+
+print("Writing EEPROM key to #{eeprom_file}...")
+
+f = IntelHexWriter.new(eeprom_file)
+f.write_data("\xFF"*KEYSTORE_OFFSET)
+f.write_data("\x00")
+f.write_data(hex2bin(keystring))
+f.write_data("\xFF"*KEY_LENGTH)
+f.close
+
+puts("OK")
\ No newline at end of file