Beispiel #1
0
int send_rsa(bool server, std::string data, std::string& recieved, int sock) {
	
	CryptoPP::AutoSeededRandomPool rng;
    //Do RSA Encryption here
   
	std::string plain, encrypted, decrypted, signature, recovered, tdecoded;
    bool result;

	CryptoPP::RSAES_OAEP_SHA_Encryptor other_pub;
	CryptoPP::RSAES_OAEP_SHA_Encryptor my_pub;
    CryptoPP::RSAES_OAEP_SHA_Decryptor my_priv;

	if(server)
	{
	    {
		    CryptoPP::HexDecoder decoder;
		    decoder.Put( (byte*)bank_priv, bank_priv_size );
		    decoder.MessageEnd();

		     my_priv.AccessKey().Load( decoder );
		 }

	    {

	        CryptoPP::HexDecoder decoder;
	        decoder.Put( (byte*)atm_pub, atm_pub_size );
	        decoder.MessageEnd();

	        other_pub.AccessKey().Load( decoder );
	    }
	    {

	        CryptoPP::HexDecoder decoder;
	        decoder.Put( (byte*)bank_pub, bank_pub_size);
	        decoder.MessageEnd();

	        my_pub.AccessKey().Load( decoder );
	    }


	 }
	 else
	 {
        {
	        CryptoPP::HexDecoder decoder;
	        decoder.Put( (byte*)atm_pub, atm_pub_size);
	        decoder.MessageEnd();

	        my_pub.AccessKey().Load( decoder );
	    }
	    {
	        CryptoPP::HexDecoder decoder;
	        decoder.Put( (byte*)bank_pub, bank_pub_size );
	        decoder.MessageEnd();

	        other_pub.AccessKey().Load( decoder );
	    }
	    {
	        CryptoPP::HexDecoder decoder;
	        decoder.Put( (byte*)atm_priv, atm_priv_size );
	        decoder.MessageEnd();

	        my_priv.AccessKey().Load( decoder );
	    }
	
	  }

	CryptoPP::RSASS<CryptoPP::PSSR, CryptoPP::SHA1>::Signer signer( my_priv );

	//Run the data through the RSA encryption
	CryptoPP::StringSource( data, true, new CryptoPP::PK_EncryptorFilter( rng, other_pub, new CryptoPP::StringSink( encrypted )));
	
	//Sign the encrypted data	
    size_t length = signer.MaxSignatureLength(); 
    byte sig_buff[length];

    signer.SignMessage(rng, (byte *)encrypted.c_str(), encrypted.length(), sig_buff);
    signature.assign((char *)sig_buff, length);

	CryptoPP::RSASS<CryptoPP::PSSR, CryptoPP::SHA1>::Verifier verifiermine( my_pub);

    data.assign(std::to_string(signature.length()));
    data.append("|");
    data.append(signature);
    data.append(encrypted);


    int err = send_socket(data, recieved, sock);
    if (err != 0) {
        return -1;
    }

    if (recieved.find("|") == recieved.npos) {
        return -1;
    }
    int sig_length = atoi(recieved.substr(0, recieved.find("|")).c_str());
    if (sig_length == 0) {
        return -1;
    }
    if (recieved.length() < sig_length + recieved.find("|")+1) {
        return -1;
    }
    recieved = recieved.substr(recieved.find("|")+1, recieved.length()-recieved.find("|")-1);
    std::string other_signature = recieved.substr(0, sig_length);
    std::string other_encrypted = recieved.substr(sig_length, recieved.length() - sig_length);
    if (other_signature.length() != (unsigned int)sig_length) {
        return -1;
    }

	CryptoPP::RSASS<CryptoPP::PSSR, CryptoPP::SHA1>::Verifier verifier( other_pub );

    result = verifier.VerifyMessage((byte *)other_encrypted.c_str(), other_encrypted.length(), (byte *)other_signature.c_str(), other_signature.length());
    if (result == false)
	{
        return -1;//Signature failed if it returns -1
    }

	CryptoPP::StringSource( other_encrypted, true, new CryptoPP::PK_DecryptorFilter( rng, my_priv, new CryptoPP::StringSink( decrypted )));

    recieved.assign(decrypted);
    return 0;
}
void decodeHex(const byte * inString1, byte * inString2,const size_t length){
  CryptoPP::HexDecoder hexDecoder;
  hexDecoder.Put(inString1,length);
  hexDecoder.MessageEnd();
  hexDecoder.Get(inString2,length/2);
}