home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / ni_crypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-02  |  2.3 KB  |  77 lines

  1. /*
  2.   Two routines to encrypt/decrypt values for storage in NetInfo.
  3.     The encrypting / decrypting algorithms used in the binary distributions have
  4.     been removed and the routines have been set up to just do the hex encoding / 
  5.     decoding.
  6. */
  7.  
  8.  
  9. static char           toHex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
  10.                                    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  11.        
  12. static unsigned char  fromHex[128] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  13.                                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  14.                                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  15.                                         0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,
  16.                                         0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
  17.                                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  18.                                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  19.                                                                             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  20.  
  21.  
  22. /*
  23.   ni_encrypt will encrypt 16 bytes data from source,
  24.   returning the hex encoded value in a 34 byte array target.
  25.     The first character of the target will be a '+', to distinguish
  26.     it from an unencoded password entry.
  27. */
  28. void ni_encrypt(char *source, char *target) {
  29. int    i, v;
  30.  
  31. // To signal a crypted entry, uncomment this:
  32. //    *target++ = '+';
  33.   for (i = 0; i < 16; i++) {
  34.         // Put you crypting code here! Replacing the line below or the whole loop.
  35.     v = (unsigned char)source[i];
  36.     *target++ = toHex[v>>4];
  37.     *target++ = toHex[v&0x0F];
  38.   }
  39.   *target = '\0';
  40. }
  41.  
  42. /*
  43.   ni_decrypt will decrypt 33 bytes data from source,
  44.   returning the original 16 byte value in target.
  45.     If the first character is not a '+', it will only
  46.     convert from hex to binary.
  47. */
  48. void ni_decrypt(char *source, char *target) {
  49. int        i, v, decode;
  50.  
  51.     if (decode = (*source == '+'))
  52.         source++;    /* Skip the leading + */
  53.   for (i = 0; i < 16; i++) {
  54.     v = fromHex[((unsigned char)*source++)&0x7F] << 4;
  55.     v|= fromHex[((unsigned char)*source++)&0x7F];
  56.         if (decode)
  57.         // Put you decrypting code here, or replace the whole loop!
  58.         else
  59.             target[i] = v;
  60.   }
  61. }
  62.  
  63.  
  64. #ifdef TESTCRYPT
  65. void main(int argc, char **argv) {
  66. char  buffer[33], result[17];
  67.  
  68.   if (argc > 1) {
  69.     ni_encrypt(argv[1], buffer);
  70.     printf("encoding: %s --> %s\n", argv[1], buffer);
  71.     ni_decrypt(buffer, result);
  72.     result[16] = '\0';
  73.     printf("decoding: %s --> %s\n", buffer, result);
  74.   }
  75. }
  76. #endif
  77.