示例#1
0
文件: head.c 项目: mooseman/pdcore
int main(int argc, char **argv)
{
    extern int opterr, optind;
    extern char *optarg;
    int c, fd, many, first;
    char *fn;
    long lines = 10;

    setlocale(LC_ALL, "");
    opterr = 0;

    while ((c = getopt(argc, argv, "n:")) != -1)
        switch (c)
        {
        case 'n':
            if (*optarg && (lines = ctol(optarg)) > 0)
                break;
            /* else fall through */
        default:
            fprintf(stderr, USAGE);
            exit(1);
        }

    if (optind >= argc)
        headfile(STDIN_FILENO, "stdin", lines);
    else
    {
        many  = (optind + 1 < argc) ? 1 : 0;
        first = 1;

        while (optind < argc)
        {
            fn = argv[optind++];

            if (many)
            {
                if (first)
                    first = 0;
                else
                    printf("\n");
                printf("==> %s <==\n", fn);
            }

            if (strcmp(fn, "-") == 0)
                headfile(STDIN_FILENO, "stdin", lines);
            else
                if ((fd = open(fn, O_RDONLY)) == -1)
                    error(fn);
                else
                {
                    headfile(fd, fn, lines);
                    if (close(fd) == -1)
                        error(fn);
                }
        }
    }

    return(exitstatus);
}
示例#2
0
/**	
 * Encrypts a plaintext string
 * @param plaintext - the plaintext string
 * @return - the encrypted ciphertext string
 */
string DES::encrypt(const string& plaintext)
{
	//LOGIC:
	//1. Check to make sure that the block is exactly 8 characters (i.e. 64 bits)
	//2. Declare an array DES_LONG block[2];
	//3. Use ctol() to convert the first 4 chars into long; store the result in block[0]
	//4. Use ctol() to convert the second 4 chars into long; store the resul in block[1]
	//5. Perform des_encrypt1 in order to encrypt the block using this->key (see sample codes for details)
	//6. Convert the first ciphertext long to 4 characters using ltoc()
	//7. Convert the second ciphertext long to 4 characters using ltoc()
	//8. Save the results in the resulting 8-byte string (e.g. bytes[8])
	//9. Convert the string (e.g. bytes[8]) to a C++ string.
	//10.Return the C++ string
	
	string ptext = plaintext;
	
  size_t number_of_padding = 0;

	while (ptext.size() < 8)
	{
		ptext.append("0");
    number_of_padding++;
	}
	
  // convert int to a character in ASCII
  char number_of_padding_char = '0' + number_of_padding;
  
  /**
   * put the number of padding at the end of padded plaintext block
   * pad only one zero may be a special case?
   * std::string.back() should not be used on the empty string
   * it's important to do the bound checking when dealing with end of string
   * we will remove the padding in the cipher.cpp
   */
  if (number_of_padding > 0 && number_of_padding < 8) {
    size_t last_index = ptext.length() - 1;
    ptext.at(last_index) = number_of_padding_char;
  }


	DES_LONG block[2];
	
	//Convert C++ string to c-string
	char cstr1[4];
	char cstr2[4];

	ptext.copy(cstr1, 4, 0);
	cstr1[4] = '\0';

	ptext.copy(cstr2, 4, 4);
	cstr2[4] = '\0';

	unsigned char ucstr1[4];
	memset(ucstr1, 0, sizeof(ucstr1));

	for (int i = 0; i < sizeof(cstr1); i++) {
	ucstr1[i] = cstr1[i];
	}
	ucstr1[sizeof(cstr1)] = '\0';

	unsigned char ucstr2[4];
	memset(ucstr2, 0, sizeof(ucstr2));

	for (int i = 0; i < sizeof(cstr2); i++) {
	ucstr2[i] = cstr2[i];
	}
	ucstr2[sizeof(cstr2)] = '\0';
  

	//Convert first 4 chars into Long Int
	block[0] = ctol(ucstr1);
	block[1] = ctol(ucstr2);
	
	//Encrypt
	des_encrypt1(block,key,ENC);
	
	
	//Convert long to c string
	unsigned char txtText1[4];
	unsigned char txtText2[4];
	
	ltoc(block[0], txtText1);
	ltoc(block[1], txtText2);
	
	//Convert c string to C++ string
	string convertcstr1 = "";
	for (int i = 0; i < sizeof(txtText1); i++) {
	convertcstr1.push_back(txtText1[i]); 
	}

	string convertcstr2 = "";
	for (int i = 0; i < sizeof(txtText2); i++) {
	convertcstr2.push_back(txtText2[i]); 
	}

  //cout << "ciphertext: " << convertcstr1 + convertcstr2 << endl;
	
	//cout << "decrypt test: " << decrypt(convertcstr1 + convertcstr2) << "|" << endl;
	
	return convertcstr1 + convertcstr2;
}
示例#3
0
/**
 * Decrypts a string of ciphertext
 * @param ciphertext - the ciphertext
 * @return - the plaintext
 */
string DES::decrypt(const string& ciphertext)
{
	//LOGIC:
	// Same logic as encrypt(), except in step 5. decrypt instead of encrypting
	
	DES_LONG block[2];
	
	//Convert C++ string to c-string
	/*const char* cstrText1 = ciphertext.substr(0,4).c_str();
	const char* cstrText2 = ciphertext.substr(4,4).c_str();
	
	char cstr1[4], cstr2[4];
	strncpy (cstr1, cstrText1, sizeof(cstrText1));
	strncpy (cstr2, cstrText2, sizeof(cstrText2));
	*/
	
	char cstr1[4];
	char cstr2[4];

	ciphertext.copy(cstr1, 4, 0);
	cstr1[4] = '\0';

	ciphertext.copy(cstr2, 4, 4);
	cstr2[4] = '\0';
	
	unsigned char ucstr1[4];
	memset(ucstr1, 0, sizeof(ucstr1));

	for (int i = 0; i < sizeof(cstr1); i++) {
	ucstr1[i] = cstr1[i];
	}
	ucstr1[sizeof(cstr1)] = '\0';

	unsigned char ucstr2[4];
	memset(ucstr2, 0, sizeof(ucstr2));

	for (int i = 0; i < sizeof(cstr2); i++) {
	ucstr2[i] = cstr2[i];
	}
	ucstr2[sizeof(cstr2)] = '\0';
	
	//Convert first 4 chars into Long Int
	block[0] = ctol(ucstr1);
	block[1] = ctol(ucstr2);
	
	//Decrypt
	des_encrypt1(block,key,DEC);
	
	//Convert Long back to c string
	unsigned char txtText1[4];
	unsigned char txtText2[4];
	
	ltoc(block[0], txtText1);
	ltoc(block[1], txtText2);
	
	//Convert c string to C++ string
	string convertcstr1 = "";
	for (int i = 0; i < sizeof(txtText1); i++) {
	convertcstr1.push_back(txtText1[i]); 
	}

	string convertcstr2 = "";
	for (int i = 0; i < sizeof(txtText2); i++) {
	convertcstr2.push_back(txtText2[i]); 
	}
	
	//cout << "length of plaintext: " << test.length() << endl;
	
	return convertcstr1 + convertcstr2;
}