1encrypt(3) Library Functions Manual encrypt(3)
2
3
4
6 encrypt, setkey, encrypt_r, setkey_r - encrypt 64-bit messages
7
9 Password hashing library (libcrypt, -lcrypt)
10
12 #define _XOPEN_SOURCE /* See feature_test_macros(7) */
13 #include <unistd.h>
14
15 [[deprecated]] void encrypt(char block[64], int edflag);
16
17 #define _XOPEN_SOURCE /* See feature_test_macros(7) */
18 #include <stdlib.h>
19
20 [[deprecated]] void setkey(const char *key);
21
22 #define _GNU_SOURCE /* See feature_test_macros(7) */
23 #include <crypt.h>
24
25 [[deprecated]] void setkey_r(const char *key, struct crypt_data *data);
26 [[deprecated]] void encrypt_r(char *block, int edflag,
27 struct crypt_data *data);
28
30 These functions encrypt and decrypt 64-bit messages. The setkey()
31 function sets the key used by encrypt(). The key argument used here is
32 an array of 64 bytes, each of which has numerical value 1 or 0. The
33 bytes key[n] where n=8*i-1 are ignored, so that the effective key
34 length is 56 bits.
35
36 The encrypt() function modifies the passed buffer, encoding if edflag
37 is 0, and decoding if 1 is being passed. Like the key argument, also
38 block is a bit vector representation of the actual value that is en‐
39 coded. The result is returned in that same vector.
40
41 These two functions are not reentrant, that is, the key data is kept in
42 static storage. The functions setkey_r() and encrypt_r() are the reen‐
43 trant versions. They use the following structure to hold the key data:
44
45 struct crypt_data {
46 char keysched[16 * 8];
47 char sb0[32768];
48 char sb1[32768];
49 char sb2[32768];
50 char sb3[32768];
51 char crypt_3_buf[14];
52 char current_salt[2];
53 long current_saltbits;
54 int direction;
55 int initialized;
56 };
57
58 Before calling setkey_r() set data->initialized to zero.
59
61 These functions do not return any value.
62
64 Set errno to zero before calling the above functions. On success, er‐
65 rno is unchanged.
66
67 ENOSYS The function is not provided. (For example because of former
68 USA export restrictions.)
69
71 For an explanation of the terms used in this section, see at‐
72 tributes(7).
73
74 ┌───────────────────────────────┬───────────────┬──────────────────────┐
75 │Interface │ Attribute │ Value │
76 ├───────────────────────────────┼───────────────┼──────────────────────┤
77 │encrypt(), setkey() │ Thread safety │ MT-Unsafe race:crypt │
78 ├───────────────────────────────┼───────────────┼──────────────────────┤
79 │encrypt_r(), setkey_r() │ Thread safety │ MT-Safe │
80 └───────────────────────────────┴───────────────┴──────────────────────┘
81
83 encrypt()
84 setkey()
85 POSIX.1-2008.
86
87 encrypt_r()
88 setkey_r()
89 None.
90
92 Removed in glibc 2.28.
93
94 Because they employ the DES block cipher, which is no longer considered
95 secure, these functions were removed from glibc. Applications should
96 switch to a modern cryptography library, such as libgcrypt.
97
98 encrypt()
99 setkey()
100 POSIX.1-2001, SUS, SVr4.
101
102 Availability in glibc
103 See crypt(3).
104
105 Features in glibc
106 In glibc 2.2, these functions use the DES algorithm.
107
109 #define _XOPEN_SOURCE
110 #include <crypt.h>
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <unistd.h>
114
115 int
116 main(void)
117 {
118 char key[64];
119 char orig[9] = "eggplant";
120 char buf[64];
121 char txt[9];
122
123 for (size_t i = 0; i < 64; i++) {
124 key[i] = rand() & 1;
125 }
126
127 for (size_t i = 0; i < 8; i++) {
128 for (size_t j = 0; j < 8; j++) {
129 buf[i * 8 + j] = orig[i] >> j & 1;
130 }
131 setkey(key);
132 }
133 printf("Before encrypting: %s\n", orig);
134
135 encrypt(buf, 0);
136 for (size_t i = 0; i < 8; i++) {
137 for (size_t j = 0, txt[i] = '\0'; j < 8; j++) {
138 txt[i] |= buf[i * 8 + j] << j;
139 }
140 txt[8] = '\0';
141 }
142 printf("After encrypting: %s\n", txt);
143
144 encrypt(buf, 1);
145 for (size_t i = 0; i < 8; i++) {
146 for (size_t j = 0, txt[i] = '\0'; j < 8; j++) {
147 txt[i] |= buf[i * 8 + j] << j;
148 }
149 txt[8] = '\0';
150 }
151 printf("After decrypting: %s\n", txt);
152 exit(EXIT_SUCCESS);
153 }
154
156 cbc_crypt(3), crypt(3), ecb_crypt(3)
157
158
159
160Linux man-pages 6.05 2023-07-20 encrypt(3)