Takes a RSA public key modulus and exponent in base64 encoding and produces a public key file in PEM format
Makefile
CC = clang |
CFLAGS = |
DEPS = |
OBJ = modexp2pubkey.o |
LIBS = -lssl -lcrypto |
%.o: %.c $(DEPS) |
$(CC) -c -o $@$<$(CFLAGS) |
modexp2pubkey: $(OBJ) |
$(CC) -o $@$^$(CFLAGS)$(LIBS) |
.PHONY: clean |
clean: |
rm -f *.o |
- How to generate public/private key in C#. Asymmetric cryptography also known as public-key encryption uses a public/private key pair to encrypt and decrypt data. In.NET, the RSACryptoServiceProvider and DSACryptoServiceProvider classes are used for asymmetric encryption.
- Was trying to generate a RSA public key with RSA modulus(n) and RSA public exponent(e). I have tried to use SCZ-BasicEncodingRules-iOS, but unfortunately SCZ-BasicEncodingRules-iOS has wrong decoding algorithm for iOS 8 and above. It outputs key with incorrect prefix.
- Is it possible to calculate the private exponent when only the RSA public key (e;N) = (37; 481) is given? Stack Exchange Network Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge,.
- Being this an RSA key the fields represent specific components of the algorithm. We find in order the modulus n = pq, the public exponent e, the private exponent d, the two prime numbers p and q, and the values dp, dq, and qinv (for the Chinese remainder theorem speed-up).
modexp2pubkey.c
#include<string.h> |
#include<openssl/rsa.h> |
#include<openssl/evp.h> |
#include<openssl/bn.h> |
#include<openssl/pem.h> |
// cheating, .. ignoring deprecation warnings |
#pragma GCC diagnostic ignored '-Wdeprecated-declarations' |
unsignedchar *base64_decode(constchar* base64data, int* len) { |
BIO *b64, *bmem; |
size_t length = strlen(base64data); |
unsignedchar *buffer = (unsignedchar *)malloc(length); |
b64 = BIO_new(BIO_f_base64()); |
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
bmem = BIO_new_mem_buf((void*)base64data, length); |
bmem = BIO_push(b64, bmem); |
*len = BIO_read(bmem, buffer, length); |
BIO_free_all(bmem); |
return buffer; |
} |
BIGNUM* bignum_base64_decode(constchar* base64bignum) { |
BIGNUM* bn = NULL; |
int len; |
unsignedchar* data = base64_decode(base64bignum, &len); |
if (len) { |
bn = BN_bin2bn(data, len, NULL); |
} |
free(data); |
return bn; |
} |
EVP_PKEY* RSA_fromBase64(constchar* modulus_b64, constchar* exp_b64) { |
BIGNUM *n = bignum_base64_decode(modulus_b64); |
BIGNUM *e = bignum_base64_decode(exp_b64); |
if (!n) printf('Invalid encoding for modulusn'); |
if (!e) printf('Invalid encoding for public exponentn'); |
if (e && n) { |
EVP_PKEY* pRsaKey = EVP_PKEY_new(); |
RSA* rsa = RSA_new(); |
rsa->e = e; |
rsa->n = n; |
EVP_PKEY_assign_RSA(pRsaKey, rsa); |
return pRsaKey; |
} else { |
if (n) BN_free(n); |
if (e) BN_free(e); |
returnNULL; |
} |
} |
voidassert_syntax(int argc, char** argv) { |
if (argc != 4) { |
fprintf(stderr, 'Description: %s takes a RSA public key modulus and exponent in base64 encoding and produces a public key file in PEM format.n', argv[0]); |
fprintf(stderr, 'syntax: %s <modulus_base64> <exp_base64> <output_file>n', argv[0]); |
exit(1); |
} |
} |
intmain(int argc, char** argv) { |
assert_syntax(argc, argv); |
constchar* modulus = argv[1]; |
constchar* exp = argv[2]; |
constchar* filename = argv[3]; |
EVP_PKEY* pkey = RSA_fromBase64(modulus, exp); |
if (pkey NULL) { |
fprintf(stderr, 'an error occurred :(n'); |
return2; |
} else { |
printf('success decoded into RSA public keyn'); |
FILE* file = fopen(filename, 'w'); |
PEM_write_PUBKEY(file, pkey); |
fflush(file); |
fclose(file); |
printf('written to file: %sn', filename); |
} |
return0; |
} |
Rsa Public Key Modulus Exponent
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
crypto.cs
usingSystem; |
usingSystem.Diagnostics; |
usingSystem.Security.Cryptography; |
usingSystem.Text; |
namespaceCrtypto |
{ |
classProgram |
{ |
staticvoidMain(string[] args) |
{ |
// generating public/private keys |
// |
//Debug.WriteLine('private: ' + RSA.ToXmlString(true)); |
//Debug.WriteLine('public: ' + RSA.ToXmlString(false)); |
varpublicKey= |
'<RSAKeyValue><Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>'; |
varprivateKey= |
'<RSAKeyValue><Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus><Exponent>AQAB</Exponent><P>/aULPE6jd5IkwtWXmReyMUhmI/nfwfkQSyl7tsg2PKdpcxk4mpPZUdEQhHQLvE84w2DhTyYkPHCtq/mMKE3MHw</P><Q>3WV46X9Arg2l9cxb67KVlNVXyCqc/w+LWt/tbhLJvV2xCF/0rWKPsBJ9MC6cquaqNPxWWEav8RAVbmmGrJt51Q</Q><DP>8TuZFgBMpBoQcGUoS2goB4st6aVq1FcG0hVgHhUI0GMAfYFNPmbDV3cY2IBt8Oj/uYJYhyhlaj5YTqmGTYbATQ</DP><DQ>FIoVbZQgrAUYIHWVEYi/187zFd7eMct/Yi7kGBImJStMATrluDAspGkStCWe4zwDDmdam1XzfKnBUzz3AYxrAQ</DQ><InverseQ>QPU3Tmt8nznSgYZ+5jUo9E0SfjiTu435ihANiHqqjasaUNvOHKumqzuBZ8NRtkUhS6dsOEb8A2ODvy7KswUxyA</InverseQ><D>cgoRoAUpSVfHMdYXW9nA3dfX75dIamZnwPtFHq80ttagbIe4ToYYCcyUz5NElhiNQSESgS5uCgNWqWXt5PnPu4XmCXx6utco1UVH8HGLahzbAnSy6Cj3iUIQ7Gj+9gQ7PkC434HTtHazmxVgIR5l56ZjoQ8yGNCPZnsdYEmhJWk=</D></RSAKeyValue>'; |
vartestData=Encoding.UTF8.GetBytes('testing'); |
using ( varrsa=newRSACryptoServiceProvider(1024)) |
{ |
try |
{ |
// client encrypting data with public key issued by server |
// |
rsa.FromXmlString(publicKey); |
varencryptedData=rsa.Encrypt(testData, true); |
varbase64Encrypted=Convert.ToBase64String(encryptedData); |
Debug.WriteLine(base64Encrypted); |
// server decrypting data with private key |
// |
rsa.FromXmlString(privateKey); |
varresultBytes=Convert.FromBase64String(base64Encrypted); |
vardecryptedBytes=rsa.Decrypt(resultBytes, true); |
vardecryptedData=Encoding.UTF8.GetString(decryptedBytes); |
Debug.WriteLine(decryptedData); |
} |
finally |
{ |
rsa.PersistKeyInCsp=false; |
} |
} |
} |
} |
} |
commented May 9, 2015
RSA key formats are defined in at least RFC 3447 and RFC 5280.The format is based on ASN.1 and includes more than just the raw modulus and exponent. If you decode the base 64 encoded ASN.1, you will find some wrapping (like an object identifier) as well as an internal ASN.1 bitstring, which decodes as.
Why exactly do you need ToBase64String and FromBase64String? I've seen some people saying this is necessary: http://stackoverflow.com/a/2164186/145349 Can you confirm this? |
commented Jul 26, 2017
@fjsj it's good to use Base64 encoding to encode a byte sequence to a string instead of Unicode encoding, because not every byte sequence has a Unicode representation. In this example, when the RSA encrypts the input bytes you can't be sure if the output bytes have a Unicode representation, but you can be sure that they have a Base64 representation. |
commented Feb 2, 2018
Rsa Public Key Encryption
Thanks for code! |
Rsa Exponent Modulus
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment