rsa_san.cpp in RSA_file_encryption.


RSA public key encryption algorithm will be applied to small-scale file encrypti...Original Link
    Sponsored links

			
#include "stdafx.h"
// This is a slow but easy RSA encryption class
// By sanicle,2005.12 
// 3mn@3mn.net

#include "stdio.h"
#include "rsa_san.h"

class Prime_factory_san
{
  unsigned np;
  unsigned *pl;
  public:
  Prime_factory_san();
  ~Prime_factory_san();
  vlong find_prime( vlong & start );
};

// prime factory implementation

static int is_probable_prime_san( const vlong &p )
{
  // Test based on Fermats theorem a**(p-1) = 1 mod p for prime p
  // For 1000 bit numbers this can take quite a while
  const rep = 4;
  const unsigned any[rep] = { 2,3,5,7 };
  for ( unsigned i=0; i<rep; i+=1 )
    if ( modexp( any[i], p-vlong(1), p ) != vlong(1) )
      return 0;
  return 1;
}

Prime_factory_san::Prime_factory_san()
{
  np = 0;
  unsigned NP = 200;
  pl = new unsigned[NP];

  // Initialise pl
  unsigned SS = 8*NP; // Rough estimate to fill pl
  char * b = new char[SS+1]; // one extra to stop search
  for (unsigned i=0;i<=SS;i+=1) b[i] = 1;
  unsigned p = 2;
  while (1)
  {
    // skip composites
    while ( b[p] == 0 ) p += 1;
    if ( p == SS ) break;
    pl[np] = p;
    np += 1;
    if ( np == NP ) break;
    // cross off multiples
    unsigned c = p*2;
    while ( c < SS )
    {
      b[c] = 0;
      c += p;
    }
    p += 1;
  }
  delete [] b;
}

Prime_factory_san::~Prime_factory_san()
{
  delete [] pl;
}

vlong Prime_factory_san::find_prime( vlong & start )
{
  unsigned SS = 1000; // should be enough unless we are unlucky
  char * b = new char[SS]; // bitset of candidate primes
  unsigned tested = 0;
  while (1)
  {
    unsigned i;
    for (i=0;i<SS;i++)
      b[i] = 1;
    for (i=0;i<np;i++)
    {
      unsigned p = pl[i];
      unsigned r = start % vlong(p); // not as fast as it should be - could do with special routine
      if (r) r = p - r;
      // cross off multiples of p
      while ( r < SS )
      {
        b[r] = 0;
        r += p;
      }
    }
    // now test candidates
    for (i=0;i<SS;i++)
    {
   			

			...
			...
			... to be continued.

  This is a preview. To get the complete source file, 
  please click here to download the whole source code package.

			
			


Project Files

    Sponsored links
NameSizeDate
 ReadMe.txt1,003.00 B18-01-06 12:31
 rsa_draft.cpp3.30 kB18-01-06 23:20
 rsa_draft.h522.00 B18-01-06 23:21
 rsa_san.cpp9.02 kB21-01-06 15:30
 rsa_san.h1.11 kB21-01-06 15:30
 sanpack_rsa.cpp2.92 kB23-01-06 11:43
 sanpack_rsa.h1.56 kB23-01-06 11:48
 stdafx.cpp215.00 B18-01-06 12:31
 stdafx.h263.00 B18-01-06 12:31
 vlong.cpp10.56 kB19-01-06 13:38
 vlong.h1.74 kB19-01-06 13:37
 ReadMe.txt1.04 kB19-01-06 15:14
 rsa_draft.cpp3.30 kB18-01-06 23:20
 rsa_draft.h522.00 B18-01-06 23:21
 rsa_san.cpp9.02 kB21-01-06 15:30
 rsa_san.h1.11 kB21-01-06 15:30
 sanpack_rsa.cpp2.92 kB23-01-06 11:43
 sanpack_rsa.h1.56 kB23-01-06 11:48
 sanpack_rsa_c_debug.cpp90.00 B19-01-06 15:20
 stdafx.cpp223.00 B19-01-06 15:14
 stdafx.h195.00 B19-01-06 15:14
 vlong.cpp10.56 kB19-01-06 13:38
 vlong.h1.74 kB19-01-06 13:37
 <sanpack_rsa>0.00 B24-04-08 09:20
 <sanpack_rsa_c_debug>0.00 B24-04-08 09:21
 <RSA_file_encryption_wangyuqing>0.00 B22-04-09 08:21
...

Related Items

    Sponsored links