Last active
September 30, 2018 10:30
-
-
Save jachermocilla/0729cc45d7d62c8bc775f5dd6b686fd0 to your computer and use it in GitHub Desktop.
RSA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
An answer to Task3 by [email protected] | |
rsa.c | |
$gcc -o rsa.exe rsa.c -lcrypto | |
*http://www.cis.syr.edu/~wedu/seed/Labs_16.04/Crypto/Crypto_RSA/Crypto_RSA.pdf | |
*https://goo.gl/HuwEPn | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <openssl/bn.h> | |
void hex_encode(char *hex, char *text){ | |
int i, len; | |
len = strlen(text); | |
for(i = 0; i<len; i++) | |
sprintf(hex+i*2, "%02X", text[i]); | |
} | |
void hex_decode(char *text, char *hex){ | |
unsigned int ch ; | |
for( ; sscanf( (const char*)hex, "%02X", &ch ) == 1 ; hex += 2 ) | |
*text++ = ch ; | |
*text = '\0'; | |
} | |
BIGNUM *encrypt(BN_CTX *ctx, BIGNUM *M, BIGNUM *e, BIGNUM *n){ | |
BIGNUM *C = BN_new(); | |
BN_mod_exp(C,M,e,n,ctx); | |
return C; | |
} | |
BIGNUM *decrypt(BN_CTX *ctx, BIGNUM *C, BIGNUM *d, BIGNUM *n){ | |
BIGNUM *M = BN_new(); | |
BN_mod_exp(M,C,d,n,ctx); | |
return M; | |
} | |
void printBN(char *msg, BIGNUM * a) | |
{ | |
/* Use BN_bn2hex(a) for hex string | |
* Use BN_bn2dec(a) for decimal string */ | |
char * number_str = BN_bn2hex(a); | |
printf("%s %s\n", msg, number_str); | |
OPENSSL_free(number_str); | |
} | |
int main () | |
{ | |
char text[256]="Password is dees"; | |
char hex[1024]=""; | |
char tmp[1024]=""; | |
BN_CTX *ctx = BN_CTX_new(); | |
BIGNUM *n = BN_new(); | |
BIGNUM *e = BN_new(); | |
BIGNUM *d = BN_new(); | |
BIGNUM *C = BN_new(); | |
BIGNUM *M = BN_new(); | |
BN_hex2bn(&n, "DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5"); | |
BN_hex2bn(&e, "010001"); | |
BN_hex2bn(&d, "74D806F9F3A62BAE331FFE3F0A68AFE35B3D2E4794148AACBC26AA381CD7D30D"); | |
//BN_hex2bn(&C, "8C0F971DF2F3672B28811407E2DABBE1DA0FEBBBDFC7DCB67396567EA1E2493F"); | |
printf("Converting message to hex and bignum..\n"); | |
hex_encode(hex,text); | |
printf("Hex: %s\n", hex); | |
BN_hex2bn(&M,hex); | |
printf("Encrypting message..\n"); | |
//printBN("M=", decrypt(ctx,C,d,n)); | |
printBN("C=",C=encrypt(ctx,M,e,n)); | |
printf("Decrypting message..\n"); | |
printBN("M=", M=decrypt(ctx,C,d,n)); | |
hex_decode(tmp,BN_bn2hex(M)); | |
printf("Text: %s\n", tmp); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* An answer to Task3 by [email protected] | |
rsadec.c | |
$gcc -o rsadec.exe rsadec.c -lcrypto | |
*http://www.cis.syr.edu/~wedu/seed/Labs_16.04/Crypto/Crypto_RSA/Crypto_RSA.pdf | |
*https://goo.gl/HuwEPn | |
*/ | |
#include <stdio.h> | |
#include <openssl/bn.h> | |
void printBN(char *msg, BIGNUM * a) | |
{ | |
/* Use BN_bn2hex(a) for hex string | |
* Use BN_bn2dec(a) for decimal string */ | |
char * number_str = BN_bn2hex(a); | |
printf("%s %s\n", msg, number_str); | |
OPENSSL_free(number_str); | |
} | |
int main () | |
{ | |
BN_CTX *ctx = BN_CTX_new(); | |
BIGNUM *n = BN_new(); | |
BIGNUM *e = BN_new(); | |
BIGNUM *d = BN_new(); | |
BIGNUM *C = BN_new(); | |
BIGNUM *M = BN_new(); | |
BN_hex2bn(&n, "DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5"); | |
BN_hex2bn(&e, "010001"); | |
BN_hex2bn(&d, "74D806F9F3A62BAE331FFE3F0A68AFE35B3D2E4794148AACBC26AA381CD7D30D"); | |
BN_hex2bn(&C, "8C0F971DF2F3672B28811407E2DABBE1DA0FEBBBDFC7DCB67396567EA1E2493F"); | |
BN_mod_exp(M,C,d,n,ctx); | |
printBN("M=", M); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment