Example #1
0
/*
This method performs all the alice actions. 
*/
BigInt Requester(BigInt message, BigInt signersPubKey, BigInt modulus)
{
    //Initializing all the variables required
    BigInt blindingFactor, encBlindingFactor, blindingFactorInverse, signedMessageWithBlindingFactor, encMessageWithBlindingFactor, messageWithSign,originalMessage;
    
    //RSA which will have signers publicKey and modulus
    RSA requesterRSA;
    requesterRSA.setN(modulus);
    requesterRSA.setPublicKey(signersPubKey);
    
    //Generating a random blindfactor
    blindingFactor = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32);
    //Blind factor modulus inverse is calculated
    blindingFactorInverse = modInverse(blindingFactor, modulus);
    
    
    
    //Encrypting the blinding factor with signers public key
    encBlindingFactor = requesterRSA.encrypt(blindingFactor);
    //appending original message to the encrypted blindfactor
    encMessageWithBlindingFactor = encBlindingFactor*message;
    encMessageWithBlindingFactor = encMessageWithBlindingFactor%modulus;
    
    //sending the encyrpted blindfactor and origin message to signer as product and get back the signed message with blinding factor
    signedMessageWithBlindingFactor = Signer(encMessageWithBlindingFactor);
    
    std::cout<<"Alice's(Sender's) Random Message generated:  " << message.toHexString() <<std::endl;
    std::cout<<"Encrypted Message sent to Bob with blindfactor and original message:" << encMessageWithBlindingFactor.toHexString() <<std::endl;
    
    //removing the blindfactor
    messageWithSign = (signedMessageWithBlindingFactor*blindingFactorInverse) % modulus;
    std::cout<<"Signed Message recieved from Bob with blindfactor and original message:" << signedMessageWithBlindingFactor.toHexString() <<std::endl;
    return messageWithSign;
}  
int main(int argc, char*argv[]){
       BigInt  cipher, deciphered,message;
        unsigned long int *a;
        unsigned long int arr[10];
        a=&arr[0];
        BigInt pubkey, privatekey;
        BigInt gnm;
		
		//message=1000;
       message = int(((double)(std::rand())/RAND_MAX)*RAND_LIMIT32);
        
        for(int i=1;i<=10;i++)
     {
      sleep(1);
      std::cout<<std::endl<<"RSA "<<i<<std::endl;     
      RSA newrsa;    // default , no args, program generates its own p and q
      std::cout<<"Public Key ";
      pubkey= newrsa.getPublicKey();
      pubkey.toULong(a,4);
       std::cout<<*a<<std::endl;
       privatekey=newrsa.getPrivateKey();
       privatekey.toULong(a,4);
       std::cout<<"Private Key : "<<*a<<std::endl;
       gnm = newrsa.getModulus();
       std::cout<<"N  is "<<gnm.toHexString()<<std::endl;
        
       cipher = newrsa.encrypt(message);
       deciphered = newrsa.decrypt(cipher);

       std::cout<<"message: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<std::endl<<std::endl;     
     }
     
     
}
Example #3
0
extern "C" __declspec(dllexport) void __cdecl Encrypt(char *cData)
{
	RSA *rsa = RSA::getInstance();

	rsa->SetKey(tibia_rsa_key);
	rsa->encrypt(cData);
}
Example #4
0
BigInt sendtoAlice(BigInt pk,BigInt mod){
RSA Aliceobj;
Aliceobj.setN(mod);
Aliceobj.setPublicKey(pk);
randomno=int(((double)rand()/RAND_MAX)*LIMIT_RAND);//generate the random number
message=int(((double)rand()/RAND_MAX)*LIMIT_RAND);//generate the message
std::cout<<"\ninitialmessage="<<message.toHexString()<<std::endl;
BigInt encrandom=Aliceobj.encrypt(randomno);//encrypt the number in Bob's public key
BigInt mulmsg=encrandom*message;
mulmsg=mulmsg%mod;
return mulmsg;
}
Example #5
0
void sendtoAlice2(BigInt msg,BigInt pk,BigInt mod){
RSA verifyobj;//verification object
verifyobj.setN(mod);
verifyobj.setPublicKey(pk);
BigInt randomInv=modInverse(randomno,mod);
BigInt mulmsg=msg*randomInv;
std::cout<<"signedmessage="<<mulmsg.toHexString()<<std::endl;
BigInt signedmsg=mulmsg%mod;//the actual signature of the message
BigInt finalmessage=verifyobj.encrypt(signedmsg);
std::cout<<"finalmessage="<<finalmessage.toHexString()<<std::endl;
//check if matches with original message after decrypting 
if(finalmessage==message){
std::cout<<"Initial and final messages are matching , hence verified\n\n"<<std::endl;

}
}
Example #6
0
int main(int argc, char*argv[]){      /*********************************************************
 * A Simple Driver
 * *******************************************************
 */
     unsigned long int *a;
     unsigned long int arr[4];
     a=&arr[0];

       RSA myRSA;
	BigInt message, cipher, deciphered;

       message = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32);
       message.toULong(a,4);
       cipher = myRSA.encrypt(message);
       deciphered = myRSA.decrypt(cipher);
       std::cout<<*a<<std::endl;
  //  std::cout<<"message "<<message.toString();
std::cout<<"message: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<std::endl;

}
Example #7
0
/**
testing.
*/
int mainCryptopp(const char* encryptPath, const char* decryptPath)
{
    printf("/***************************************************************************\n");
    printf("C++ crypto, wrap cryptopp interface, reference to www.cryptopp.com\n");
    printf("[email protected] 2006-5-25\n");
    printf("***************************************************************************/\n");

    // base64 testing.
    {
        printf("\n=======================base64=====================\n");
        // input data.
        const char * indata = "bsmith is a good guy.";
        int inlen = (int)strlen(indata);
        printf("inlen=%d\n", inlen);
        printf("indata(hex)=");
        dump(indata, inlen);
        
        // encoding ...
        int maxoutlen = Base64::getCipherLen(inlen);
        printf("maxoutlen=%d\n", maxoutlen);
        char * outdata = new char[maxoutlen];
        int outlen = Base64::encode(indata, inlen, outdata);
        printf("outlen=%d\n", outlen);
        printf("outdata(hex)=");
        dump(outdata, outlen);
        // encoded base64 string.
        char * outstr = new char[outlen+1];
        memcpy(outstr, outdata, outlen);
        outstr[outlen] = 0x00;
        printf("outstr=%s\n", outstr);

        // decoding ...
        int maxinlen = Base64::getPlainLen(outlen);
        printf("maxinlen=%d\n", maxinlen);
        char * orgdata = new char[maxinlen];
        int orglen = Base64::decode(outdata, outlen, orgdata);
        printf("orglen=%d\n", orglen);
        printf("orgdata(hex)=");
        dump(orgdata, orglen);

        delete[] outdata;
        delete[] outstr;
        delete[] orgdata;
    }

    // base16 testing.
    {
        printf("\n=======================base16=====================\n");
        // input data.
        const char * indata = "bsmith is a good guy.";
        int inlen = (int)strlen(indata);
        printf("inlen=%d\n", inlen);
        printf("indata(hex)=");
        dump(indata, inlen);
        
        // encoding ...
        int maxoutlen = Base16::getCipherLen(inlen);
        printf("maxoutlen=%d\n", maxoutlen);
        char * outdata = new char[maxoutlen];
        int outlen = Base16::encode(indata, inlen, outdata);
        printf("outlen=%d\n", outlen);
        printf("outdata(hex)=");
        dump(outdata, outlen);
        // encoded base16 string.
        char * outstr = new char[outlen+1];
        memcpy(outstr, outdata, outlen);
        outstr[outlen] = 0x00;
        printf("outstr=%s\n", outstr);

        // decoding ...
        int maxinlen = Base16::getPlainLen(outlen);
        printf("maxinlen=%d\n", maxinlen);
        char * orgdata = new char[maxinlen];
        int orglen = Base16::decode(outdata, outlen, orgdata);
        printf("orglen=%d\n", orglen);
        printf("orgdata(hex)=");
        dump(orgdata, orglen);

        delete[] outdata;
        delete[] outstr;
        delete[] orgdata;
    }

    // RSA testing.
    {
        printf("\n=======================RSA PKCS #1=====================\n");
        // key
        // N factor in RSA, aslo called modulus.
        const char * N = "90755611487566208138950675092879865387596685014726501531250157258482495478524769456222913843665634824684037468817980814231054856125127115894189385717148934026931120932481402379431731629550862846041784305274651476086892165805223719552575599962253392248079811268061946102234935422772131475340988882825043233323";
        // e factor in RSA, aslo called public exponent.
        const char * e = "65537";
        // d factor in RSA, aslo called private exponent
        const char * d = "17790520481266507102264359414044396762660094486842415203197747383916331528947124726552875080482359744765793816651732601742929364124685415229452844016482477236658413327331659722342187036963943428678684677279032263501011143882814728160215380051287503219732737197808611144507720521201393129692996926599975297921";

        // input data.
        const char * indata = "bsmith is a good guy.";
        int inlen = (int)strlen(indata);
        printf("inlen=%d\n", inlen);
        printf("indata(hex)=");
        dump(indata, inlen);

        // init RSA public key encryptor.
        RSA enc;
        enc.initPublicKey(N, e);

        // encrypt.
        int maxoutlen = enc.getCipherLen(inlen);
        printf("maxoutlen=%d\n", maxoutlen);
        char * outdata = new char[maxoutlen];
        int outlen = enc.encrypt(indata, inlen, outdata);
        printf("outlen=%d\n", outlen);
        printf("outdata(hex)=");
        dump(outdata, outlen);

        // init private for RSA decryptor.
        RSA dec;
        dec.initPrivateKey(N, e, d);

        // decrypt.
        int maxinlen = dec.getPlainLen(outlen);
        printf("maxinlen=%d\n", maxinlen);
        char * orgdata = new char[maxinlen];
        int orglen = dec.decrypt(outdata, outlen, orgdata);
        printf("orglen=%d\n", orglen);
        printf("orgdata(hex)=");
        dump(orgdata, orglen);

        delete[] outdata;
        delete[] orgdata;
    }

    // AES/CBC/PKCS5Padding testing.
    {
        printf("\n=======================AES/CBC/PKCS5Padding=====================\n");
        // key
        const char * key = "0123456789abcdef";
        // iv
        const char * iv = "fedcba9876543210";

        // init AES.
        AES aes;
        aes.init(key, 16, iv);

        // input data.
        // const char * indata = "bsmith is a good guy.";
        // const char * indata = "bsmith.";
        const char * indata = "I am a good guy.";
        int inlen = (int)strlen(indata);
        printf("inlen=%d\n", inlen);
        printf("indata(hex)=");
        dump(indata, inlen);

        // encrypt.
        int maxoutlen = aes.getCipherLen(inlen);
        printf("maxoutlen=%d\n", maxoutlen);
        char * outdata = new char[maxoutlen];
        int outlen = 0;
        {
            outlen = aes.encrypt(indata, inlen, outdata);
            printf("outlen=%d\n", outlen);
            printf("outdata(hex)=");
            dump(outdata, outlen);
        }
        {
            outlen = aes.encrypt(indata, inlen, outdata);
            printf("outlen=%d\n", outlen);
            printf("outdata(hex)=");
            dump(outdata, outlen);
        }

        // decrypt.
        int maxinlen = aes.getPlainLen(outlen);
        printf("maxinlen=%d\n", maxinlen);
        char * orgdata = new char[maxinlen];
        {
            
            int orglen = aes.decrypt(outdata, outlen, orgdata);
            printf("orglen=%d\n", orglen);
            printf("orgdata(hex)=");
            dump(orgdata, orglen);
        }
        {
            int orglen = aes.decrypt(outdata, outlen, orgdata);
            printf("orglen=%d\n", orglen);
            printf("orgdata(hex)=");
            dump(orgdata, orglen);
        }

        delete[] outdata;
        delete[] orgdata;

    }

    // SHA1 testing.
    {
        printf("\n=======================SHA1=====================\n");

        SHA1 sha1;

        // input data.
        const char * indata = "bsmith is a good guy.";
        int inlen = (int)strlen(indata);
        printf("inlen=%d\n", inlen);
        printf("indata(hex)=");
        dump(indata, inlen);
        
        // one time digest.
        {
            char * outdata = new char[sha1.getCipherLen(inlen)];
            int outlen = sha1.digest(indata, inlen, outdata);
            printf("outlen=%d\n", outlen);
            printf("outdata(hex)=");
            dump(outdata, outlen);

            delete[] outdata;
        }

        // serval times
        {
            char * outdata = new char[sha1.getCipherLen(inlen)];
            sha1.update(indata, 5);
            sha1.update(indata+5, inlen-5);
            int outlen = sha1.final(outdata);
            printf("outlen=%d\n", outlen);
            printf("outdata(hex)=");
            dump(outdata, outlen);

            delete[] outdata;
        }

        // one time digest.
        {
            char * outdata = new char[sha1.getCipherLen(inlen)];
            int outlen = sha1.digest(indata, inlen, outdata);
            printf("outlen=%d\n", outlen);
            printf("outdata(hex)=");
            dump(outdata, outlen);

            delete[] outdata;
        }

        // serval times
        {
            char * outdata = new char[sha1.getCipherLen(inlen)];
            sha1.update(indata, 5);
            sha1.update(indata+5, inlen-5);
            int outlen = sha1.final(outdata);
            printf("outlen=%d\n", outlen);
            printf("outdata(hex)=");
            dump(outdata, outlen);

            delete[] outdata;
        }
    }

    // RSA-SHA1 Sign testing.
    {
        printf("\n=======================RSA-SHA1 Sign=====================\n");

        // key
        // N factor in RSA, aslo called modulus.
        const char * N = "90755611487566208138950675092879865387596685014726501531250157258482495478524769456222913843665634824684037468817980814231054856125127115894189385717148934026931120932481402379431731629550862846041784305274651476086892165805223719552575599962253392248079811268061946102234935422772131475340988882825043233323";
        // e factor in RSA, aslo called public exponent.
        const char * e = "65537";
        // d factor in RSA, aslo called private exponent
        const char * d = "17790520481266507102264359414044396762660094486842415203197747383916331528947124726552875080482359744765793816651732601742929364124685415229452844016482477236658413327331659722342187036963943428678684677279032263501011143882814728160215380051287503219732737197808611144507720521201393129692996926599975297921";

        // input data.
        const char * indata = "bsmith is a good guy.";
        int inlen = (int)strlen(indata);
        printf("inlen=%d\n", inlen);
        printf("indata(hex)=");
        dump(indata, inlen);

        Sign sign;
        // private key for signer.
        sign.initPrivateKey(N, e, d);

        // sign.
        int maxoutlen = sign.getCipherLen(inlen);
        printf("maxoutlen=%d\n", maxoutlen);
        char * outdata = new char[maxoutlen];
        int outlen = sign.sign(indata, inlen, outdata);
        printf("outlen=%d\n", outlen);
        printf("outdata(hex)=");
        dump(outdata, outlen);

        // public key for verifier.
        sign.initPublicKey(N, e);

        // verify.
        {
            bool res = sign.verify(indata, inlen, outdata, outlen);
            printf("result <?> true : %s\n", res?"true":"false");
        }
        
        // another data.
        const char * indata1 = "bsmith is not a good guy.";
        int inlen1 = (int)strlen(indata1);
        {
            bool res = sign.verify(indata1, inlen1, outdata, outlen);
            printf("result <?> false : %s\n", res?"true":"false");
        }

        delete[] outdata;
    }

    //printf("press any key to exit!");
    //getchar();
    
    {
        //my test
        unsigned long decryptSize = 0;
        char* decryptBuffer = (char*)getFileData(decryptPath, &decryptSize);
        
        unsigned long encryptSize = 0;
        char* encryptBuffer = (char*)getFileData(encryptPath, &encryptSize);
        
        printf("decryptBuffer(hex)=");
        dump(decryptBuffer, decryptSize);
        
        // key
        const char * key = "0123456789abcdef";
        // iv
        const char * iv = "fedcba9876543210";
        
        // init AES.
        AES aes;
        aes.init(key, 16, iv);
        {
            // decrypt.
            int maxinlen = aes.getPlainLen(encryptSize);
            char * orgdata = new char[maxinlen];
            {
                int orglen = aes.decrypt(encryptBuffer, encryptSize, orgdata);
                printf("decryptWithCrypto++(hex)=");
                dump(orgdata, orglen);
            }
            delete [] orgdata;
        }
        
        printf("encryptBuffer(hex)=");
        dump(encryptBuffer, encryptSize);
        
        {
            // encrypt.
            int maxoutlen = aes.getCipherLen(decryptSize);
            char * outdata = new char[maxoutlen];
            int outlen = 0;
            {
                outlen = aes.encrypt(decryptBuffer, decryptSize, outdata);
                printf("encryptWithCrypto++(hex)=");
                dump(outdata, outlen);
            }
            delete [] outdata;
        }
        
        delete [] decryptBuffer;
        delete [] encryptBuffer;
        
        
    }

    return 0;
}
Example #8
0
int main(int argc, char*argv[]){    
  
	/*
	TASK 1: Perform encryption and Decryption each using the RSA routines provided here. They don’t necessarily have to be part of the same code
	*/
	int count = 10;
	int i=0;
	std::string result;
	BigInt randMessage[10];
	
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	std::cout<<"TASK 1a: Create 10 instances of the RSA class without giving arguments, generate random message or assign messages, and perform encryption through each of the 10 classes. "<<std::endl;
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	//initializing at once as everytime RSA is called srand is called internally and reseting the random function
	for(i=0;i<count;i++)
	{
	   randMessage[i] = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32);
	}
	for(i=0;i<count;i++)
	{
	  RSA myRSA;
	  BigInt message, cipher, deciphered;
	  message = randMessage[i];
	
	  //encrypting
	  cipher = myRSA.encrypt(message);
	  //decrypting
	  deciphered = myRSA.decrypt(cipher);
	  
	  //Checking if the decrypted value is equal to original value 
	  if(message == deciphered)
	  {
	    result = "success";
	  }
	  else
	  {
	    result = "failed";
	  }
	  std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl;
	}
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	std::cout<<"TASK 1b: Create 5 instances of the RSA class by passing a large prime number [p](> 30,000), and perform encryption decryption. "<<std::endl;
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	
	count =  5;
	//Initializing some primes and non-primes to be used by following tasks.
	unsigned long int setOfLargePrimes[] = {103919, 103951, 103963, 103967,103969, 104677 ,104681 ,104683 ,104693 ,104701 ,104707 ,104711 ,104717 ,104723 ,104729};
	unsigned long int setOfNonPrimes[] = {40000, 31000, 42000, 45000, 50000, 48000,60000,80000,70000,90000,99000};
	
	
	//iterating 5 time for question 1b
	for(i=0;i<count;i++)
	{
	  //giving one primenumber as input to the RSA
	  RSA rsaInstance(setOfLargePrimes[i]);
	  BigInt message, cipher, deciphered;
	  message = randMessage[i];
	 
	  //decrypting and encrypting 
	  cipher = rsaInstance.encrypt(message);
	  deciphered = rsaInstance.decrypt(cipher);
	  
	  //Checking if the decrypted value is equal to original value 
	  if(message == deciphered)
	  {
	    result = "success";
	  }
	  else
	  {
	    result = "failed";
	  }
	  
	  std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl;
	
	}
	
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	std::cout<<"TASK 1c: Create 5 instances of the RSA class by passing 2 large prime numbers [p,q] (> 30,000) and perform encryption decryption "<<std::endl;
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	
	for(i=0;i<count;i++)
	{
	  //Intializing RSA with two primenumbers
	  RSA rsaInstance(setOfLargePrimes[2*i], setOfLargePrimes[2*i+1]);
	  BigInt message, cipher, deciphered;
	  
	  message = randMessage[i];
	  
	  //encrypting and decrypting
	  cipher = rsaInstance.encrypt(message);
	  deciphered = rsaInstance.decrypt(cipher);
	 
	  //Checking if the decrypted value is equal to original value 
	  if(message == deciphered)
	  {
	    result = "success";
	  }
	  else
	  {
	    result = "failed";
	  }
	  
	  std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl;
	
	}
	
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	std::cout<<"TASK 1d: Create 10 instances of the RSA class by passing 2 large non prime numbers (> 30,000) and perform encryption decryption. In most of the cases the message should not get decrypted correctly.  "<<std::endl;
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	
	for(i=0;i<10;i++)
	{
	  //Creating RSA with non primes. 
	  RSA rsaInstance(setOfNonPrimes[i], setOfNonPrimes[i+1]);
	  BigInt message, cipher, deciphered;
	  
	  message = randMessage[i];
	   //encrypting and decrypting
	  cipher = rsaInstance.encrypt(message);
	  deciphered = rsaInstance.decrypt(cipher);
	 
	  //Checking if the decrypted value is equal to original value 
	  if(message == deciphered)
	  {
	    result = "success";
	  }
	  else
	  {
	    result = "failed";
	  }
	  
	  std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl;
	
	}
	
	/*
	TASK 2: Challenge Response: Scheme 0
	*/
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	std::cout<<" TASK 2: Challenge Response Scheme 0 "<<std::endl;
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	
	{
	  RSA RSA1, RSA2;
	  
	  //Assigning RSA1 public key and Modulus to RSA2 
	  BigInt publicKey = RSA1.getPublicKey();
	  BigInt modulus = RSA1.getModulus();
	  RSA2.setPublicKey(publicKey);
	  RSA2.setN(modulus);
	  
	  //generating random message
	  BigInt message, cipher, deciphered;
	  message = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32);
	  
	  
	  //encrypting with RSA2 i.e RSA1's public key
	  cipher = RSA2.encrypt(message);
	  //decrypting with RSA1 i.e RSA1's private key
	  deciphered = RSA1.decrypt(cipher);
	  
	 //Checking if the decrypted value is equal to original value 
	  if(message == deciphered)
	  {
	    result = "success";
	  }
	  else
	  {
	    result = "failed";
	  }
	  
	  std::cout<<"Instance:"<<i<<"\tmessage: "<<message.toHexString()<<"\tcipher: "<<cipher.toHexString()<<"\tdeciphered: "<<deciphered.toHexString()<<"\tverification-result:"<<result<<std::endl;
	
	}
	
	/*
	TASK 3: Blind Signature
	*/
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	std::cout<<" TASK 3: Blind Signature  "<<std::endl;
	std::cout<<"********************************************************************************************************************************************************************"<<std::endl;
	
	{
	  //Random message requester want to send with signers signature
	  BigInt message = int(((double)std::rand()/RAND_MAX)*RAND_LIMIT32);
	  
	  //Passing signers public key to the requester so that he can generate the blind factor
	  BigInt signedMessage =  Requester(message, signersRSA.getPublicKey(), signersRSA.getModulus());
	  BigInt deciphered = signersRSA.encrypt(signedMessage);
	  
	  std::cout<<"Signed Message without blindfactor:" << signedMessage.toHexString() <<std::endl;
	  
	  std::cout<<" Message Decrypted:" << deciphered.toHexString() <<std::endl;
	  
	  //Checking if the decrypted value is equal to original value 
	  if(message == deciphered)
	  {
	    std::cout<<"Blind Signature successfully implemented"<<std::endl;
	  }
	  else
	  {
	    std::cout<<"Blind Signature implementation failed"<<std::endl;
	  }
	  
	 
	}
	

}
Example #9
0
int main() {


    int pprimes[18] = { 30839, 30841, 30851, 30853, 30859, 30869, 30871, 30881, 30893, 30911, 30931, 30937, 30941
            , 30949, 30971, 30977, 30983, 31013 };


    string str = "1";


    //Create 10 instances of the RSA class without giving arguments, generate random message or assign messages,
    // and perform encryption through each of the 10 classes.
    for (int i=0; i < 10; i++) {

        RSA* rsa = new RSA();
        str = str + "0";
        bitset<96> mssg (str);

        BigInt* msg = new BigInt(mssg);

        BigInt encrypted = rsa->encrypt(*msg);

        BigInt decrypted = rsa->decrypt(encrypted);

        string decmsg = decrypted.toString();

        cout << "part a message was:" << decmsg << endl;

        delete rsa;
    }

    //Create 5 instances of the RSA class by passing a large prime number [p](> 30,000), and perform encryption decryption
    for (int j=0; j < 5; j++) {

        RSA* rsa = new RSA(pprimes[j]);
        str = str + "1";

        bitset<96> mssg (str);

        BigInt* msg = new BigInt(mssg);

        BigInt encrypted = rsa->encrypt(*msg);

        BigInt decrypted = rsa->decrypt(encrypted);

        string decmsg = decrypted.toString();

        cout << "part b (1 prime) message was:" << decmsg << endl;

        delete rsa;
    }

    //Create 5 instances of the RSA class by passing 2 large prime numbers [p,q] (> 30,000) and perform encryption decryption
    for (int j=0; j < 5; j++) {

        RSA* rsa = new RSA(pprimes[j], pprimes[j+1]);
        str = str + "0";

        bitset<96> mssg (str);

        BigInt* msg = new BigInt(mssg);

        BigInt encrypted = rsa->encrypt(*msg);

        BigInt decrypted = rsa->decrypt(encrypted);

        string decmsg = decrypted.toString();

        cout << "part c (2 primes) message was:" << decmsg << endl;

        delete rsa;
    }

    //Create 10 instances of the RSA class by passing 2 large non prime numbers (> 30,000) and perform encryption decryption.
    // In most of the cases the message should not get decrypted correctly.
    for (int j=0; j < 10; j++) {
        //add one to the prime number to make it non-prime
        RSA* rsa = new RSA(pprimes[j] + 1, pprimes[j+1] + 1);
        str = str + "1";

        bitset<96> mssg (str);

        BigInt* msg = new BigInt(mssg);

        BigInt encrypted = rsa->encrypt(*msg);

        BigInt decrypted = rsa->decrypt(encrypted);

        string decmsg = decrypted.toString();

        cout << "part d (nonprimes) message was:" << decmsg << endl;

        delete rsa;
    }


    RSA* RSA1 = new RSA();
    RSA* RSA2 = new RSA();
    BigInt pub1 = RSA1->getPublicKey();
    BigInt n = RSA1->getModulus();
    BigInt* randmsg = new BigInt(rand());

    cout << "\n PART 2 " << endl;

    cout << "\nrandom message before encryption: " << randmsg->toString() << endl;

    RSA2->setPublicKey(pub1);
    RSA2->setN(n);

    BigInt encr = RSA2->encrypt(*randmsg);

    BigInt decr = RSA1->decrypt(encr);

    cout << "random message after encryption: " << decr.toString() << endl;

    //PART 3

    cout << "\n PART 3" << endl;

    //a. Alice obtains the public key and Modulus N of the person (Bob) who is to sign the message
    RSA* bob = new RSA();
    RSA* alice = new RSA();

    BigInt bpubkey = bob->getPublicKey();
    BigInt bmod = bob->getModulus();

    alice->setN(bmod);
    alice->setPublicKey(bpubkey);


    //b. Obtain a random number and its inverse with respect to the Modulus [Not phi] of Bob
    BigInt* randnum = new BigInt(rand());
    BigInt inverse = randnum->operator%(bob->getModulus());

    //c. Alice obtains/generates a message to be signed.
    BigInt* rmssg = new BigInt(rand());
    cout << "\nrandom message before encryption: " << rmssg->toString() << endl;

    //d. Alice encrypts the random number with the public key.
    BigInt emessg = alice->encrypt(*rmssg);

    //e. Alice multiplies this value by the message
    BigInt newval = emessg.operator*(*rmssg);

    //f. Alice then takes a modulus over N
    BigInt newmod = newval.operator%(alice->getModulus());

    //g. Alice sends it to Bob
    //h. Bob simply decrypts the received value with the private key

    BigInt bobdecrypt = bob->decrypt(newmod);

    //i. Bob sends it back to Alice
    //j. Alice then multiplied the received value with the inverse and takes a modulus over N.

    BigInt alicemult = bobdecrypt.operator*(inverse);
    BigInt alicemod = alicemult.operator%(alice->getModulus());

    //k. The value obtained above is the signed message. To obtain the original message from it, again encrypt it with Bob’s Public Key.
    BigInt original = alice->encrypt(alicemod);
    cout << "\n Alice decrypted message:  " << original.toString() << endl;

    return 0;
}