1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
main ()
{
SHA_CTX *c;
long erreur = 0;
char *message="vatos locos para siempre";
char *encrypt, *decrypt, *sign, hash[20],hash1[20];
int longmsg,i,random,ok;
int siglong;
RSA *rsa;
random= RAND_load_file("/dev/urandom ",1024);//seeding the Pseudo random generator
rsa = RSA_generate_key(128,13,NULL,NULL);//generating a key having a 128 bits modulus
/*cryptage-decryptage
encrypt= (unsigned char*)malloc(RSA_size(rsa));
longmsg= strlen(message)*sizeof(unsigned char);
RSA_public_encrypt(longmsg, message, encrypt, rsa, RSA_PKCS1_OAEP_PADDING);
decrypt= (unsigned char*)malloc(RSA_size(rsa));
RSA_private_decrypt(RSA_size(rsa), encrypt, decrypt, rsa, RSA_PKCS1_OAEP_PADDING);
*/
SHA1(message,strlen(message),hash); //computing a digest using SHA1
for(i=0; i<20; ++i){
printf("%x",hash[i]);}
printf("\n");
SHA1_Init(c); //2 method to compute SHA1
SHA1_Update(c,message,strlen(message));
SHA1_Final(hash1,c);
for(i=0; i<20; ++i){
printf("%x",hash1[i]);}
printf("\n");
sign=(unsigned char*)malloc(RSA_size(rsa)); //generating the signature using RSA
ok= RSA_sign(NID_sha1,hash,strlen(hash),sign,&siglong,rsa);
erreur= ERR_get_error();
printf("signature = %d \n",ok);
printf("longueur de la signature =%d \n",siglong);
printf("erreur =%d \n",erreur);
printf("PRNG random =%d \n",random);
RSA_free(rsa);
free(sign);
//free(encrypt);
//free(decrypt);
} |