crypt_rsa_sign.pl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/perl
  2. # Using Crypt::RSA to sign a message, using an explicitly given private key.
  3. use 5.014;
  4. use Crypt::RSA;
  5. my $rsa = Crypt::RSA->new;
  6. my $key = Crypt::RSA::Key->new;
  7. my ($public, $private) =
  8. $key->generate(
  9. p => "99554414790940424414351515490472769096534141749790794321708050837",
  10. q => "104593961812606247801193807142122161186583731774511103180935025763",
  11. e => 65537,
  12. )
  13. or die "error";
  14. my $message = "Hello world!";
  15. my $signature = $rsa->sign(
  16. Message => $message,
  17. Key => $private,
  18. Armour => 1,
  19. )
  20. || die $rsa->errstr();
  21. say $signature;
  22. my $plaintext = $rsa->verify(
  23. Signature => $signature,
  24. Message => $message,
  25. Key => $public,
  26. Armour => 1,
  27. )
  28. || die $rsa->errstr();
  29. say ("Valid signature: ", ($plaintext ? "yes" : "no"));
  30. #~ $public->write ( Filename => 'cicada.public' );
  31. #~ $private->write ( Filename => 'cicada.private' );
  32. __END__
  33. -----BEGIN RSA SIGNATURE-----
  34. Version: 1.99
  35. Scheme: Crypt::RSA::SS::PSS
  36. OQA1NABTaWduYXR1cmXV7WLmAUf4OxAMgmi7zg+29Mkp01JE3wrrBFoycR4q5WuaHyahJUwugSW/
  37. 84TmB5IZ2Z3TPEw=
  38. =8zvlSo9cU7Merm4hrEJboQ==
  39. -----END RSA SIGNATURE-----