Why are the RSA-SHA256 signatures I generate with OpenSSL and Java different?

openssl dgst -sha256 < data.txt produces something like: (stdin)= b39eaeb437e33087132f01c2abc60c6a16904ee3771cd7b0d622d01061b40729 notice the (stdin)=‘? you don’t want that to be part of your hash, if you need to create a digest, use the -binary option. try using this to sign your data: openssl sha -sha256 -sign private.pem < data.txt This does everything you need. edit – … Read more

How to generate RSA private key using OpenSSL?

#include <openssl/rsa.h> #include <openssl/pem.h> const int kBits = 1024; const int kExp = 3; int keylen; char *pem_key; RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0); /* To get the C-string PEM form: */ BIO *bio = BIO_new(BIO_s_mem()); PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL); keylen = BIO_pending(bio); pem_key = calloc(keylen+1, 1); /* Null-terminate */ BIO_read(bio, … Read more

Differences between “BEGIN RSA PRIVATE KEY” and “BEGIN PRIVATE KEY”

See https://polarssl.org/kb/cryptography/asn1-key-structures-in-der-and-pem (search the page for “BEGIN RSA PRIVATE KEY”) (archive link for posterity, just in case). BEGIN RSA PRIVATE KEY is PKCS#1 and is just an RSA key. It is essentially just the key object from PKCS#8, but without the version or algorithm identifier in front. BEGIN PRIVATE KEY is PKCS#8 and indicates that … Read more

Load RSA public key from file

Below is the relevant information from the link which Zaki provided. Generate a 2048-bit RSA private key $ openssl genrsa -out private_key.pem 2048 Convert private Key to PKCS#8 format (so Java can read it) $ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_key.der -nocrypt Output public key portion in DER format (so … Read more

How to read a PEM RSA private key from .NET

Update 03/03/2021 .NET 5 now supports this out of the box. To try the code snippet below, generate a keypair and encrypt some text at http://travistidwell.com/jsencrypt/demo/ var privateKey = @”—–BEGIN RSA PRIVATE KEY—– { the full PEM private key } —–END RSA PRIVATE KEY—–“; var rsa = RSA.Create(); rsa.ImportFromPem(privateKey.ToCharArray()); var decryptedBytes = rsa.Decrypt( Convert.FromBase64String(“{ base64-encoded … Read more

Encrypt and Decrypt text with RSA in PHP

You can use phpseclib, a pure PHP RSA implementation: <?php include(‘Crypt/RSA.php’); $privatekey = file_get_contents(‘private.key’); $rsa = new Crypt_RSA(); $rsa->loadKey($privatekey); $plaintext = new Math_BigInteger(‘aaaaaa’); echo $rsa->_exponentiate($plaintext)->toBytes(); ?>