Generate Rsa Public Key From Modulus Exponent C

 

Convert Modulus and Exponent to RSA PublicKey. GitHub Gist: instantly share code, notes, and snippets. Modulus was super simple p.q, but exponent I can't figure out. Have searched all the articles and often found how to go opposite way - generating public private key once you pick the exponenet. I have been trying ModInverse from p-1 and q-1, and solve x with GCD on all the componenets, but nothing gave me the right value (I know the value I.

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;
}
}
}
}
}
Generate rsa public key from modulus exponent c in one

commented May 9, 2015

/zend-server-license-key-generator.html. 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

Thanks for code!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Takes a RSA public key modulus and exponent in base64 encoding and produces a public key file in PEM format
Makefile
Exponent
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
modexp2pubkey.c

Generate Rsa Public Key From Modulus Exponent C In 2

#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;
}

Generate Rsa Public Key From Modulus Exponent C In Java

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment