]> git.ipfire.org Git - thirdparty/openssl.git/blob - demos/maurice/example3.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / demos / maurice / example3.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
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <evp.h>
14
15 #define STDIN 0
16 #define STDOUT 1
17 #define BUFLEN 512
18 #define INIT_VECTOR "12345678"
19 #define ENCRYPT 1
20 #define DECRYPT 0
21 #define ALG EVP_des_ede3_cbc()
22
23 static const char *usage = "Usage: example3 [-d] password\n";
24
25 void do_cipher(char *,int);
26
27 int main(int argc, char *argv[])
28 {
29 if ((argc == 2))
30 {
31 do_cipher(argv[1],ENCRYPT);
32 }
33 else if ((argc == 3) && !strcmp(argv[1],"-d"))
34 {
35 do_cipher(argv[2],DECRYPT);
36 }
37 else
38 {
39 fprintf(stderr,"%s", usage);
40 exit(1);
41 }
42
43 return 0;
44 }
45
46 void do_cipher(char *pw, int operation)
47 {
48 char buf[BUFLEN];
49 char ebuf[BUFLEN + 8];
50 unsigned int ebuflen, rc;
51 unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH];
52 unsigned int ekeylen, net_ekeylen;
53 EVP_CIPHER_CTX ectx;
54
55 memcpy(iv, INIT_VECTOR, sizeof(iv));
56
57 EVP_BytesToKey(ALG, EVP_md5(), "salu", pw, strlen(pw), 1, key, iv);
58
59 EVP_CipherInit(&ectx, ALG, key, iv, operation);
60
61 while(1)
62 {
63 int readlen = read(STDIN, buf, sizeof(buf));
64
65 if (readlen <= 0)
66 {
67 if (!readlen)
68 break;
69 else
70 {
71 perror("read");
72 exit(1);
73 }
74 }
75
76 EVP_CipherUpdate(&ectx, ebuf, &ebuflen, buf, readlen);
77
78 write(STDOUT, ebuf, ebuflen);
79 }
80
81 EVP_CipherFinal(&ectx, ebuf, &ebuflen);
82
83 write(STDOUT, ebuf, ebuflen);
84 }
85
86