views:

131

answers:

2

I am trying to figure out how to make public/private keys that are AES encrypted. I'd like to be able to use it like so:

byte[] BytesToEncrypt = { 0x01, 0x02, 0x03, 0x04, 0x05 };
byte[] PublicKey;
byte[] PrivateKey;
byte[] EncryptedBytes;
byte[] UnencryptedBytes;

PrivateKey = CreatePrivateKey();
PublicKey = CreatePublicKey(PrivateKey);
EncryptedBytes = EncryptBytes(PrivateKey);
// This line should return unencrypted bytes
UnencryptedBytes = UnencryptBytes(EncryptedBytes, PrivateKey);
// This line should also return the unencrypted bytes
UnencryptedBytes = UnencryptBytes(EncryptedBytes, PublicKey);

How can I implement something like this? I've seen public/private encryption, but all the examples I've seen seem to use RSA encryption. I want to use AES. Is this possible?

+5  A: 

AES is an algorithm for symmetric-key encryption, so it doesn't make sense to talk about public and private AES keys. If your AES key is public there is no security at all.

You can encrypt an RSA public key using AES encryption if you wish, but it is unnecessary as it is not something you need to keep secret.

You can encrypt an RSA private key using AES encryption. This could be useful if you want to password protect your key so that if your computer is stolen they cannot use your key.

You can also use an asymmetric encryption to transfer a symmetric key to another party and then afterwards communicate with them using symmetric encryption. TLS uses this principle. It can be useful because symmetric algorithms can be faster to compute, but sending a symmetric key to someone in plain text would be insecure so you need the asymmetric encryption to keep the symmetric key safe.

Mark Byers
So in order to implement Private and Public Keys, one must use RSA?
icemanind
What I'm trying to do specifically is encrypt a file. But I only want people who have the public key to be able to decrypt the file. Also, the file contains sensative materials, so the encryption must be a fairly good one.
icemanind
It sounds like you don't need public/private key encryption (RSA) at all. You just need AES symmetric encryption with a single secret key to do what you just described.
StefanO
A: 

While AES encryption is symmetric, I assume that your goal is to encrypt data (and have to use AES for whatever reason), give that data to someone else, allow them to read it, but only if they have your public key.

You could consider this workflow:

  1. Generate an AES encryption key.
  2. Encrypt your data using AES encryption key.
  3. Generate a public and private key.
  4. Use the private key to encrypt your AES encryption key.
  5. Distribute the AES encrypted data, your public key, and your AES key as encrypted by the private key.

This is similar to what SSL does (not exactly though because the there is a handshake procedure with data being encrypted using public keys, not private keys), but might meet your needs.

This workflow ensures that your AES key can only be discovered if someone has your public key, but given that they then have the correct AES key, doesn't prevent someone from replacing your original data with other data. That's what public/private key data signing is for.

userx