📄 dlock.c
字号:
} } else { lite_encrypt_block(plaintext, seed); for (i=0; i<blocksize; i++) { if (seed[i] != ciphertext[i]) { puts("Encryption error."); exit(8); } } lite_decrypt_block(ciphertext, seed); for (i=0; i<blocksize; i++) { if (seed[i] != plaintext[i]) { puts("Decryption error."); exit(8); } } } } fclose(f); }static void get_pass_phrase(char *key, uint maxlength) { int ch; uint keypos; puts("Please enter your pass phrase (case sensitive):"); keypos = 0; key[0] = 0; do {#ifdef UNIX ch = getchar();#else ch = getch(); if (!ch) ch = 0x100 + getch();#endif if (((ch == 8) || (ch == 127)) && keypos) { key[--keypos] = 0; printf("\x08 \x08"); } else if ((ch == 27) || (ch == 3)) { key[0] = 0; } else if ((ch != 13) && (ch != 10) && (ch != 8) && (ch != 127)) { printf("*"); key[keypos++] = (char) ch; key[keypos] = 0; } } while ((ch != 13) && (ch != 10) && (ch != 3) && (ch != 27) && (keypos < (maxlength-1))); puts("\r "); }void help(void) { puts("\nCopyright (C) 1994 Michael Paul Johnson.\n" "All rights reserved. No warranty.\n\n" "To test Diamond and Diamond Lite against validation data:\n" " DLOCK /T\n"#ifdef MPJs_Copy "To generate new validation data set:\n" " DLOCK /G\n"#endif "To encrypt a file:\n" " DLOCK /E [/S] infilename outfilename [/Ppass phrase | /Kkeyfile]\n" "To decrypt a file:\n" " DLOCK /D [/S] infilename outfilename [/Ppass phrase | /Kkeyfile]\n" "/S = Silent mode (minimal screen output).\n"); exit(1); }int encrypt_file(char *inname, char *outname, uint keysize, byte *key) { int infile, outfile, bytesinbuf, i, blockpos; byte *buf; byte lastciphertext[16]; ulong bytesdone; buf = (byte*) malloc(CHUNK); if (!buf) { puts("Out of memory."); return(3); } infile = open(inname, O_RDONLY | O_BINARY); if (infile < 0) { printf("Can't open %s\n", inname); return(13); } outfile = open(outname,O_WRONLY|O_EXCL|O_CREAT|O_BINARY,S_IREAD|S_IWRITE); if (outfile < 0) { printf("Can't open %s\n", outname); puts("Output file cannot already exist."); return(14); } bytesdone = 0; set_diamond_key(key, keysize, 10, false, 16); memset(lastciphertext, 0, 16); diamond_encrypt_block(lastciphertext, lastciphertext); blockpos = 0; do { bytesinbuf = read(infile, (char *)buf, CHUNK); for (i = 0; i<bytesinbuf; i++) { buf[i] ^= lastciphertext[blockpos++]; if (blockpos >= 16) { blockpos = 0; diamond_encrypt_block(buf + i - 15, lastciphertext); } } if (bytesinbuf) { if (write(outfile, (char *)buf, bytesinbuf) != bytesinbuf) { printf("Error writing to %s\n", outname); return(15); } bytesdone += bytesinbuf; if (verbose) printf("\r%lu bytes encrypted. ", bytesdone); } } while (bytesinbuf == CHUNK); if (close(outfile)) printf("Error closing %s\n", outname); close(infile); diamond_done(); if (verbose) puts(""); return 0; }int decrypt_file(char *inname, char *outname, uint keysize, byte *key) { int infile, outfile, bytesinbuf, i, blockpos; byte *buf; byte lastciphertext[16]; byte thisciphertext[16]; ulong bytesdone; buf = (byte*) malloc(CHUNK); if (!buf) { puts("Out of memory."); return(3); } infile = open(inname, O_RDONLY | O_BINARY); if (infile < 0) { printf("Can't open %s\n", inname); return(13); } outfile = open(outname,O_WRONLY|O_EXCL|O_CREAT|O_BINARY,S_IREAD|S_IWRITE); if (outfile < 0) { printf("Can't open %s\n", outname); puts("Output file cannot already exist."); return(14); } bytesdone = 0; set_diamond_key(key, keysize, 10, false, 16); memset(lastciphertext, 0, 16); diamond_encrypt_block(lastciphertext, lastciphertext); blockpos = 0; do { bytesinbuf = read(infile, (char *)buf, CHUNK); for (i = 0; i<bytesinbuf; i++) { thisciphertext[blockpos] = buf[i]; buf[i] ^= lastciphertext[blockpos++]; if (blockpos >= 16) { blockpos = 0; diamond_encrypt_block(thisciphertext, lastciphertext); } } if (bytesinbuf) { if (write(outfile, (char *)buf, bytesinbuf) != bytesinbuf) { printf("Error writing to %s\n", outname); return(15); } bytesdone += bytesinbuf; printf("\r%lu bytes decrypted. ", bytesdone); } } while (bytesinbuf == CHUNK); if (close(outfile)) printf("Error closing %s\n", outname); close(infile); diamond_done(); if (verbose) puts(""); return 0; }int main(int argc, char** argv) { int i, c, keyfile; uint keysize; char *p; char keyfilename[PATHSIZE]; generate = validate_file = startpass = encryptit = decryptit = false; verbose = true; keysize = 0; keyfilename[0] = 0; passphrase[0] = 0; for (i=1;i<argc;i++) { if ((!startpass) && (argv[i][0] == '-') || (argv[i][0] == '/')) { c = argv[i][1] | 0x20; switch (c) { case 'd': decryptit = true; encryptit = false; break; case 'e': decryptit = false; encryptit = true; break;#ifdef MPJs_Copy case 'g': generate = true; break;#endif case 'k': strcpy(keyfilename, argv[i] + 2); break; case 'p': startpass = true; strcpy((char *) passphrase, argv[i] + 2); keysize = strlen((char *)passphrase); break; case 's': verbose = false; break; case 't': validate_file = true; break; default: help(); break; } } else if (startpass) { strcat((char *) passphrase, " "); strcat((char *) passphrase, argv[i]); keysize = strlen((char *)passphrase); } else if (infilename[0] == 0) { strcpy(infilename, argv[i]); } else if (outfilename[0] == 0) { strcpy(outfilename, argv[i]); } else { help(); } } if (!(generate || validate_file || encryptit || decryptit)) help();#ifdef MPJs_Copy if (generate) generate_test_data();#endif if (generate || validate_file) validate_test_data(); if (encryptit || decryptit) { if (!outfilename[0]) help(); if (keysize) { if (verbose) puts("Using pass phrase from command line."); } else if (keyfilename[0]) { keyfile = open(keyfilename, O_RDONLY|O_BINARY); if (keyfile >= 0) { keysize = read(keyfile, (char *)passphrase, PASS_SIZE); close(keyfile); if (verbose) { if (keysize) printf("Using pass phrase from %s\n", keyfilename); else printf("No key read from file %s\n", keyfilename); } } else { printf("Can't open %s\n", keyfilename); } } if (!keysize) { p = getenv("DLOCK_KEY"); if (p) { strcpy((char *) passphrase, p); keysize = strlen((char *)passphrase); if (verbose) puts("Using pass phrase from DLOCK_KEY environment variable."); } else { get_pass_phrase((char *) passphrase, PASS_SIZE); keysize = strlen((char *)passphrase); } } if (!keysize) { puts("Pass phrase required to encrypt or decrypt."); return(10); } if (keysize < 8) { puts("WARNING: PASS PHRASE IS TOO SHORT FOR SECURITY!"); } if (encryptit) { if (verbose) printf("Encrypting %s to %s\n", infilename, outfilename); return(encrypt_file(infilename, outfilename, keysize, passphrase)); } else { if (verbose) printf("Decrypting %s to %s\n", infilename, outfilename); return(decrypt_file(infilename, outfilename, keysize, passphrase)); } } return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -