Sponsored Content
Full Discussion: C++ help
Top Forums Programming C++ help Post 302466121 by Corona688 on Monday 25th of October 2010 02:06:08 PM
Old 10-25-2010
A quick update since I slightly misread your original problem. I had it generate strings of 1-5 characters by putting outlen in a for-loop.

Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
	// String of what letters can be used for input
	unsigned char *charset=	"abcdefghijklmnopqrstuvwxyz";
	// Might as well count combinations while we're at it
	unsigned long int loopcount=0;
	// We need this in order to shrink the list efficiently
	const int maxpos=strlen(charset);
	// How long an output string do we want?
	// can be a maximum of 16 in this implementation)
	int outlen;


	for(outlen=1; outlen<=5; outlen++)
	{
		// Set this to 0 to stop the main loop
		int running=1;
		// What letters have already been used.
		// Every time outlen changes, we blank it to all zeros.
		int chosen[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

		while(running)
		{
			// Just a counter for loops.
			int pos;	
			// A blank C-string, the current set of allowed characters.
			char ltrs[64];
			// A blank C-string, the current output string.
			char out[17];

			// Fill the output set with a-z.
			strcpy(ltrs, charset);

			loopcount++;	// Count another loop.

			// Generate the output string char by char.
			// out[0]=x, out[1]=y, ...out[outlen-1]=q,
			// out[outlen]='\0'
			for(pos=0; pos<outlen; pos++)
			{
				// Add the chosen letter to the output string
				out[pos]=ltrs[ chosen[pos] ];

				// Remove the character from the input set.
				// We do this by replacing it by the
				// char at the end, and shrinking by one.
				ltrs[ chosen[pos] ] = ltrs[ maxpos-(pos+1) ];
				ltrs[ maxpos-(pos+1) ] = '\0';
			}

			out[outlen]='\0';// NULL-terminate it so we can print it

			printf("%s\n", out);

			// Advance to the next combination.
			for(pos=0; pos<outlen; pos++)
			{
				// Pick one letter ahead.
				chosen[pos]++;

				// Are we beyond the max?  letter 0 can choose
				// 0-25, letter 1 can choose 0-24, etc, etc.
				if(chosen[pos] >= (maxpos - pos))
				{
					// If we are, reset it to 0 and advance
					// the next chosen character.
					chosen[pos]=0;

					// Out of digits!  We are finished.
					if(pos == (outlen - 1))
					{
						running=0;
						break;
					}
					else
					{
						// We must advance the next
						// digit.  while loop keeps going
						continue;
					}
				}
				// No overflowing digits?  We're done.
				break;
			}
		}
	}

	fprintf(stderr, "%lu combinations\n", loopcount);
	return(0);
}

The result is 8,268,676 combinations, which doesn't take much longer at all to do.

Last edited by Corona688; 10-25-2010 at 03:10 PM.. Reason: removed redundant variable
 
EVP_PKEY_encrypt(3)						      OpenSSL						       EVP_PKEY_encrypt(3)

NAME
EVP_PKEY_encrypt_init, EVP_PKEY_encrypt - encrypt using a public key algorithm SYNOPSIS
#include <openssl/evp.h> int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen); DESCRIPTION
The EVP_PKEY_encrypt_init() function initializes a public key algorithm context using key pkey for an encryption operation. The EVP_PKEY_encrypt() function performs a public key encryption operation using ctx. The data to be encrypted is specified using the in and inlen parameters. If out is NULL then the maximum size of the output buffer is written to the outlen parameter. If out is not NULL then before the call the outlen parameter should contain the length of the out buffer, if the call is successful the encrypted data is written to out and the amount of data written to outlen. NOTES
After the call to EVP_PKEY_encrypt_init() algorithm specific control operations can be performed to set any appropriate parameters for the operation. The function EVP_PKEY_encrypt() can be called more than once on the same context if several operations are performed using the same parameters. RETURN VALUES
EVP_PKEY_encrypt_init() and EVP_PKEY_encrypt() return 1 for success and 0 or a negative value for failure. In particular a return value of -2 indicates the operation is not supported by the public key algorithm. EXAMPLE
Encrypt data using OAEP (for RSA keys): #include <openssl/evp.h> #include <openssl/rsa.h> EVP_PKEY_CTX *ctx; unsigned char *out, *in; size_t outlen, inlen; EVP_PKEY *key; /* NB: assumes key in, inlen are already set up * and that key is an RSA public key */ ctx = EVP_PKEY_CTX_new(key); if (!ctx) /* Error occurred */ if (EVP_PKEY_encrypt_init(ctx) <= 0) /* Error */ if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_OAEP_PADDING) <= 0) /* Error */ /* Determine buffer length */ if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0) /* Error */ out = OPENSSL_malloc(outlen); if (!out) /* malloc failure */ if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0) /* Error */ /* Encrypted data is outlen bytes written to buffer out */ SEE ALSO
EVP_PKEY_CTX_new(3), EVP_PKEY_decrypt(3), EVP_PKEY_sign(3), EVP_PKEY_verify(3), EVP_PKEY_verify_recover(3), EVP_PKEY_derive(3) HISTORY
These functions were first added to OpenSSL 1.0.0. 1.0.1e 2013-02-11 EVP_PKEY_encrypt(3)
All times are GMT -4. The time now is 09:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy