Import of old SSLeay release: SSLeay 0.8.1b
[openssl.git] / demos / maurice / example2.c
1 /* NOCW */
2 /*
3         Please read the README file for condition of use, before
4         using this software.
5
6         Maurice Gittens  <mgittens@gits.nl>   January 1997
7 */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <strings.h>
12
13 #include "rsa.h"
14 #include "evp.h"
15 #include "objects.h"
16 #include "x509.h"
17 #include "err.h"
18 #include "pem.h"
19 #include "ssl.h"
20
21 #include "loadkeys.h"
22
23 #define PUBFILE   "cert.pem"
24 #define PRIVFILE  "privkey.pem"
25 #define STDIN     0
26 #define STDOUT    1 
27
28 int main()
29 {
30         char *ct = "This the clear text";
31         char *buf;   
32         char *buf2;
33         EVP_PKEY *pubKey;
34         EVP_PKEY *privKey;
35         int len;
36         FILE *fp;
37
38         ERR_load_crypto_strings();
39
40         privKey = ReadPrivateKey(PRIVFILE);
41         if (!privKey) 
42         {  
43                 ERR_print_errors_fp (stderr);    
44                 exit (1);  
45         }
46
47         pubKey = ReadPublicKey(PUBFILE);  
48         if(!pubKey)
49         {
50            EVP_PKEY_free(privKey);   
51            fprintf(stderr,"Error: can't load public key");
52            exit(1);
53         }
54
55         /* No error checking */
56         buf = malloc(EVP_PKEY_size(pubKey));
57         buf2 = malloc(EVP_PKEY_size(pubKey));
58
59         len = RSA_public_encrypt(strlen(ct)+1, ct, buf, pubKey->pkey.rsa,RSA_PKCS1_PADDING);
60
61         if (len != EVP_PKEY_size(pubKey))
62         {
63             fprintf(stderr,"Error: ciphertext should match length of key\n");
64             exit(1);
65         }
66
67         RSA_private_decrypt(len, buf, buf2, privKey->pkey.rsa,RSA_PKCS1_PADDING);
68
69         printf("%s\n", buf2);
70
71         EVP_PKEY_free(privKey);
72         EVP_PKEY_free(pubKey);
73         free(buf);
74         free(buf2);
75 }
76
77