Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have seen many examples of AES encryption & decryption coding. These also includes the key & iv setup. But, is it possible to export the key to an external file and then use the similar to do a decryption process?

I'm now using the Crypto++ library. Thanks you.
Posted

fboyixiz wrote:
It seems that the byte array 'key' holds a different value from the external file "sosemanuk.key".


I think you have one of two options here.
1. Leave your code as it is using the Crypto++ library, and try to debug your problems with reference to the library documentation.
2. Forget the sosemanuk (whatever that is) stuff and just use ordinary File Management[^] to write and read your byte array. Either way you need to spend some time reading one or other sets of documentation.
 
Share this answer
 
fboyixiz wrote:
is it possible to export the key to an external file


Assuming you can access the key as a byte array, then yes you can do anything you want with it. However if the key is merely a handle to some internal structure within the library it may not be so easy. Without seeing a sppecification of the key itself I cannot offer anything more.
 
Share this answer
 
Sorry for the incomplete question.

Below is my sampel coding (It's is not an AES but Sosemanuk):

// Initialize key & iv
byte key[16], iv[16];
AutoSeededRandomPool rng;
rng.GenerateBlock(key, 16);
rng.GenerateBlock(iv, Sosemanuk::IV_LENGTH);
ArraySource	(key, sizeof(key), true, new FileSink ("sosemanuk.key"))
;

Did I correctly export/save the key?
Thanks.
 
Share this answer
 
fboyixiz wrote:
Did I correctly export/save the key?


I have no idea what Sosemanuk is, but looking at your code I don't see you exporting the key to a file. The last statement in your code does not seem to make much sense.
 
Share this answer
 
This last statement is based on the Crypto++ library, filter.h.
ArraySource(key, sizeof(key), true, new FileSink ("sosemanuk.key"));


Can you show me the process of writing the byte array key (shown below) to a external file.

byte key[16];
AutoSeededRandomPool rng;
rng.GenerateBlock(key, 16);


Thanks,
fboyixiz
 
Share this answer
 
fboyixiz wrote:
This last statement is based on the Crypto++ library, filter.h.

ArraySource(key, sizeof(key), true, new FileSink ("sosemanuk.key"));


Fine, I just have no idea what it means. Does this function write the key to the file sosemanuk.key or what?


fboyixiz wrote:
Can you show me the process of writing the byte array key (shown below) to a external file.
byte key[16];
AutoSeededRandomPool rng;
rng.GenerateBlock(key, 16);


Assuming that key now contains the information you want then use an ordinary file (via CreatFile()) to write it out.
 
Share this answer
 
Richard MacCutchan wrote:
Fine, I just have no idea what it means. Does this function write the key to the file sosemanuk.key or what?


Yes. It is suppose to write array type source to a file.

Anyway thanks for your help. I will look into createfile().
 
Share this answer
 
fboyixiz wrote:
Yes. It is suppose to write array type source to a file.


Well then you should not need anything else. What happens when you run your code?
 
Share this answer
 
v2
Richard MacCutchan wrote:
Well then you should not need anything else. What happens when you run your code?


It did write the external file but when I try to load and read the file. I can't decrypt the crypted file. I used the codings below:

C#
byte key[16],iv[16];

// This is using the Crypto++ library to read the key and write a
// new string called 'sosemanukKey'.
string sosemanukKey;
FileSource("sosemanuk.key", true,
        new StringSink (sosemanukKey));

// I try to create a byte array called 'key' from the string 'sosemanukKey'

memcpy(key,sosemanukKey.c_str(),sosemanukKey.length());


It seems that the byte array 'key' holds a different value from the external file "sosemanuk.key".

Maybe you can show me another alternative to read the external file and write a new byte array variable. Thank you.
 
Share this answer
 
I'm actually comfortable using the current library. I have no problem export and import asymmetric keys generated using RSA or DSA as the library provide special function call save() & load () to persist the key to/from disk in the most interoperable manner.

Anyway, I will try to use the simple file management operation for my problem.

Thank you all for your help.
 
Share this answer
 
I agree with what Richard says, if you want to know what library does, you have to check functions. On the other hand, you can create your own method to write your key to a file and in this case i recommend you not to use extensions such as ".key" that will explicitly reveal what the file is (for) or its purpose for security issues. In library that you are using they use that kind of extension to create a standarts and integrity withing the library methods but you wont need that if you are using your own method.

You could write your own method by simple file management operations since you can write a byte directly to a file without a need of convertion and you can also read your data directly.

Lastly i would like to recommend you to use Openssl library but if you started with current library and gone too deep to change you could use current library without hesitate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900