generate_rsa_keypair.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. function usage(){
  3. cat << _EOT_
  4. Usage :
  5. $0 [-g] [-]
  6. Description:
  7. Generate 2048 bit Rsa key pair and Display modulus and public exponent
  8. Options:
  9. -g generate rsa key pair, default on
  10. -s only show modulus and public exponent
  11. _EOT_
  12. exit 1
  13. }
  14. FLAG_G="on"
  15. FLAG_S="off"
  16. while getopts gsh OPT
  17. do
  18. case $OPT in
  19. g)
  20. FLAG_G="on"
  21. ;;
  22. s)
  23. FLAG_S="on"
  24. FLAG_G="off"
  25. ;;
  26. h)
  27. usage
  28. ;;
  29. \?)
  30. usage
  31. ;;
  32. esac
  33. done
  34. if [ $FLAG_G = "on" ]; then
  35. # generate 2048bit Rsa private key
  36. openssl genrsa 2048 2> /dev/null > private-key.pem
  37. # expose public key
  38. openssl rsa -in private-key.pem -pubout -out public-key.pem 2> /dev/null
  39. fi
  40. if [ $FLAG_S = "on" ]; then
  41. # display modulus and exponent
  42. openssl rsa -modulus < private-key.pem 2>/dev/null | grep Modulus=
  43. openssl rsa -text < private-key.pem 2> /dev/null | grep publicExponent
  44. fi