Newer
Older
WickedDocs / Crypto / rsa-tests / tests.sh
#!/bin/bash

openssl genrsa -out mykey.pem 2048
openssl rsa -in mykey.pem -pubout -out mykey.pub
openssl rsautl -sign -inkey mykey.pem -in message.txt -out secret.bin
openssl rsautl -verify -pubin -inkey mykey.pub -in secret.bin -out recovered.txt

# cat << EOF > convert.c
# #include <stdio.h>
# int main() { for (int i = 0; i < 256; i++) printf("%02x", (unsigned)getchar()); }
# EOF
# g++ convert.c -o convert
# rm convert.c
# echo -n "const char* secretMessage = \"" > secret.txt
# cat secret.bin | ./convert >> secret.txt
# echo "\";" >> secret.txt
# echo >> secret.txt
# echo "Public Modulus and Exponent:" >> secret.txt
# echo >> secret.txt
# openssl asn1parse -in mykey.pub -strparse 19 >> secret.txt

echo "const char* secretMessage = " > secret.cpp
cat secret.bin | xxd -p | sed 's/^/   "/' | sed 's/$/"/' | head -c -1 >> secret.cpp
echo -e ";\n" >> secret.cpp
echo "const char* publicModulus = " >> secret.cpp
# Dump the contents of the public key
openssl asn1parse -in mykey.pub -strparse 19 -offset 4 -length 261 | cut -d ':' -f 4 | fold -w 60 | sed 's/^/   "/' | sed 's/$/"/' | head -c -1 >> secret.cpp
echo -e ";\n" >> secret.cpp
echo "const char* publicExponent = " >> secret.cpp
# Dump the contents of the public key
openssl asn1parse -in mykey.pub -strparse 19 -offset 265 | cut -d ':' -f 4 | fold -w 60 | sed 's/^/   "/' | sed 's/$/"/' | head -c -1 >> secret.cpp
echo -e ";\n" >> secret.cpp

# Dump the contents of the private key
# openssl asn1parse -in mykey.pem

#RSAPrivateKey ::= SEQUENCE {
#  version           Version,
#  modulus           INTEGER,  -- n
#  publicExponent    INTEGER,  -- e
#  privateExponent   INTEGER,  -- d
#  prime1            INTEGER,  -- p
#  prime2            INTEGER,  -- q
#  exponent1         INTEGER,  -- d mod (p-1)
#  exponent2         INTEGER,  -- d mod (q-1)
#  coefficient       INTEGER,  -- (inverse of q) mod p
#  otherPrimeInfos   OtherPrimeInfos OPTIONAL
#}