development
, C
, tool
, linux
, AES
, CBC
, ECB
, OpenSSL
, openssl
, openssl API
, openssl binary
, hexdump
, od
, xxd
In this note, on one hand, I will show how to use the openssl binary to encrypt and decrypt a file. On the other hand, I will demo how to encrypt a file through openssl binary and decrypt it through openssl API in c code.
In this case, I encrypt a file in a x86-based system, and decrypt it in a mips-based system which is a switch here.
In this case, it is intuitive. The command is mostly the same including parameters like -K, -k, -iv, -nosalt, -pbkdf2, -iter and so on. The differences are -d/-e and -in/-out.
I gave an example below.
In this case, I encrypt a file by openssl binary and decrypt it by API imported in my code.
The key point here is the key/iv in C code are strings while in parameter are hex. Therefore, we have to convert the key/iv in C code to hex digits as a parameter when launching openssl binary. This can be done through an online tool here or others.
For instance,
Here, we just demo simple ECB.
Below gist shows:
File Name | Description |
---|---|
AES_example.c | The source code for demo |
Makefile | For build binary |
org.txt | The original plaint text |
gen_cypher.sh | The command to generate cypher text |
plain.txt | The outcome of aes_example |
When we implement features like AES encryption/decryption or MD5/SHA checksum in a file, we usually need to deubg it to check whether our encrypting, decrypting or checksum calculating are correct. So, we need some tools to inspect it.
We can utilize tools such as hexdump, od, and xxd to dump hex values in a file or binary.
Below shows some examples of them.
https://onlinestringtools.com/convert-string-to-ascii
http://aes.online-domain-tools.com/
https://emn178.github.io/online-tools/md5_checksum.html
https://my.oschina.net/u/2539854/blog/1559531
https://stackoverflow.com/questions/2537271/compile-openssl-with-the-shared-option
http://www.361way.com/hexdump/749.html