home *** CD-ROM | disk | FTP | other *** search
- /*
- Two routines to encrypt/decrypt values for storage in NetInfo.
- The encrypting / decrypting algorithms used in the binary distributions have
- been removed and the routines have been set up to just do the hex encoding /
- decoding.
- */
-
-
- static char toHex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
-
- static unsigned char fromHex[128] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,
- 0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
-
-
- /*
- ni_encrypt will encrypt 16 bytes data from source,
- returning the hex encoded value in a 34 byte array target.
- The first character of the target will be a '+', to distinguish
- it from an unencoded password entry.
- */
- void ni_encrypt(char *source, char *target) {
- int i, v;
-
- // To signal a crypted entry, uncomment this:
- // *target++ = '+';
- for (i = 0; i < 16; i++) {
- // Put you crypting code here! Replacing the line below or the whole loop.
- v = (unsigned char)source[i];
- *target++ = toHex[v>>4];
- *target++ = toHex[v&0x0F];
- }
- *target = '\0';
- }
-
- /*
- ni_decrypt will decrypt 33 bytes data from source,
- returning the original 16 byte value in target.
- If the first character is not a '+', it will only
- convert from hex to binary.
- */
- void ni_decrypt(char *source, char *target) {
- int i, v, decode;
-
- if (decode = (*source == '+'))
- source++; /* Skip the leading + */
- for (i = 0; i < 16; i++) {
- v = fromHex[((unsigned char)*source++)&0x7F] << 4;
- v|= fromHex[((unsigned char)*source++)&0x7F];
- if (decode)
- // Put you decrypting code here, or replace the whole loop!
- else
- target[i] = v;
- }
- }
-
-
- #ifdef TESTCRYPT
- void main(int argc, char **argv) {
- char buffer[33], result[17];
-
- if (argc > 1) {
- ni_encrypt(argv[1], buffer);
- printf("encoding: %s --> %s\n", argv[1], buffer);
- ni_decrypt(buffer, result);
- result[16] = '\0';
- printf("decoding: %s --> %s\n", buffer, result);
- }
- }
- #endif
-