How to create a .pem file with aes key - c

i have to create an aes-256 key and store it in a .pem file. I am using RAND_bytes() to simply create a 256 bit random key. After this how can i save this to a pem file.
I have looked at Reading and writing rsa keys to a pem file in C and openssl pem. But i am not working with RSA keys.
I suspect my task is much simpler like create pem from base64 but not much help.
PS: This key will be used to encrypt a self signed digital certificate.
EDIT: looking around more i found that i could use bio to covert to pem, something likePEM_write_bio_PrivateKey but it takes EVP_PKEY format as argument. So how can i convert char buf to EVP_PKEY. i found d2i_PublicKey but it involves RSA and am unsure how RSA would fit into picture here.

AES key is just a random byte array. You can simple store the bytes in a file without any structure.

You can convert aes key to .pem by using PEMWriter class of bouncyCastle library.
http://www.bouncycastle.org/documentation.html

Related

Transferring external images & files to Google Cloud Storage

If I use the Google Cloud Storage File Transfer console
https://console.cloud.google.com/storage/transfer?project=XXXX
How do I generate an MD5 string for my image? Say my image is located at https://www.planwallpaper.com/static/images/desktop-year-of-the-tiger-images-wallpaper.jpg for example.
I can easily get the bytes value, but how would I generate the MD5 for this?
The docs were a bit vague. Any ideas?
An MD5 hash is used to ensure the data transferred into GCS is imported correctly. HTTPS data transfers include a variety of built-in checksums, but for very large imports of many, many files, errors can and do show up, and so GCS wants to be sure that each object that it downloads is exactly what you think it is.
An MD5 is a 128 bit number that is the result of running the MD5 algorithm on an object. This number can be represented in a variety of ways (the popular md5sum command uses hexadecimal strings). GCS asks that you represent this number as a base64 encoding. Here's a command that can generate an MD5 sum in the right format:
openssl md5 -binary NameOfSourceFile | openssl enc -base64
There's a standard GCS object that can be used to validate your MD5 logic. The object https://storage.googleapis.com/md5-test/md5-test has a base64'd MD5 string of BfnRTwvHpofMOn2Pq7EVyQ==.

Input files required to programmatically generate a PKCS#12 certificate

I need to create an application in C to generate a self signed PKCS#12 certificate. I have an application which so far creates a .csr and a .key file.
I also have a program which creates a PKCS#12 object from a supplied .key and .p7 file. So given that I have a .csr and a .key file, I assume my next move would be to create a .p7 file from the .key / .csr file?
I found a sample application here which signs or verifies certificates. At the end of the signing process, we have a PKCS7 object. I assume that is the object I need.
However, the sample application gives the following error when I run it with the key I have generated:
$ ./sign_and_verify_util sign mykey.key.pem
Error reading signer private key in mykey.key.pem
I added the .pem onto the file because I thought that was the issue.
Any shove in the right direction would be a great help and I would really appreciate it!
Edit
I just converted mykey.key to mykey.key.pem correctly using:
openssl rsa -inform DER -outform PEM -in mykey.key -out mykey.key.pem
Now the application I am trying to get the .p7 file from says:
Error reading signer certificate in (null)

Encrypt a string using OpenSSL in C

I have a problem with encrypting a string.
I have the command to encrypt a file using OpenSSL.
But I wanted to know the encrypt the string not the File.
The command for encrypting a file is:
system("openssl des3 -e -nosalt -in %s -out %s -k %s > /tmp/sys; cat /tmp/sys", src, dest, key);
where src and dest are the two file names.
what are the options available with OPEN SSL.
In above in and out are options for encrypting a text file.
I need the option to encrypting character array variable.
As pointed out on another question (by someone who dug through the code more than I did):
https://opensource.conformal.com/viewgit/?a=viewblob&p=cyphertite&h=899259f8ba145c11087088ec83153db524031800&hb=6782c6839d847fbed0aed8c55917e78b5684110f&f=cyphertite/ct_crypto.c
has the code you need to use OpenSSL natively to perform encryption/decryption in your app.
Happy hacking!

Triple DES Decryption of plain text

I have some plain text encrypted with Triple XOR and then Triple DES. I also have the key. How do I manage to uncover the plain text?
Here is a handy tool: Online decrypt tool

append RSA signature to a text file

I have a text file that I have to sign with RSA private key and then append this signature and do an AES encryption over this "text file+signature".
For demonstration reasons I am testing such an encrypted file.
I am writing a simple program in C to do the following:
First do an RSA sign(1024 bit) on a text file.
Then append the signature to the text file
Then do an AES encryption over the file.
Then perform the AES decryption
Then remove the 128 byte signature from the file.
Then do an RSA verification of the original text file and the text file after decryption.
Here are my questions:
Is it a good idea to append a binary signature to a text file?
If no what is the general way this is done?
I tried a simple program to do the above but I always get one or two junk characters on AES decryption and therefore RSA verification fails.
Do please suggest.
an AES decrypt of such a file and then remove the 128 byte(1024 bit modulus) signature.
The ad-hoc standard for embedding crypto information in text files was introduced by Privacy Enhanced Mail some time ago: Basically the binary information is encoded in base-64 and appended to the text file along with a header line to identify the "snip" point for the added content.
Here is a sample of what it typically looks like (this chunk would be added to the end of the existing text file)
-----BEGIN PRIVACY-ENHANCED MESSAGE-----
Proc-Type: 4,ENCRYPTED
Content-Domain: RFC822
DEK-Info: DES-CBC,F8143EDE5960C597
Originator-ID-Symmetric: linn#zendia.enet.dec.com,,
Recipient-ID-Symmetric: linn#zendia.enet.dec.com,ptf-kmc,3
Key-Info: DES-ECB,RSA-MD2,9FD3AAD2F2691B9A,
B70665BB9BF7CBCDA60195DB94F727D3
Recipient-ID-Symmetric: pem-dev#tis.com,ptf-kmc,4
Key-Info: DES-ECB,RSA-MD2,161A3F75DC82EF26,
E2EF532C65CBCFF79F83A2658132DB47
LLrHB0eJzyhP+/fSStdW8okeEnv47jxe7SJ/iN72ohNcUk2jHEUSoH1nvNSIWL9M
8tEjmF/zxB+bATMtPjCUWbz8Lr9wloXIkjHUlBLpvXR0UrUzYbkNpk0agV2IzUpk
J6UiRRGcDSvzrsoK+oNvqu6z7Xs5Xfz5rDqUcMlK1Z6720dcBWGGsDLpTpSCnpot
dXd/H5LMDWnonNvPCwQUHt==
-----END PRIVACY-ENHANCED MESSAGE-----

Resources