Many people are asking how to encrypt using private key and decrypt by
public key, whether using RSA or ECC.

The main concern is, we do not want to distribute private key in the
software from which hackers can derive  a public key. (Public key encryption
generate a serial number)

I have also asked if there is anyway to remove some components from RSA
private key to prevent public key generation from it. I personally think
this is possible and could make private key and public key equivalent, you
can not generate one from the other.

Here I have an idea by using Elliptic curve.

Using an ECP field for illustration,

Shared:
ECP contains parameters P,A,B. In the Prime field, y^2=x^3+ax+b (from
Wikipedia).
A base point G(x,y) that satisfied above equation.

Private:
A number pv, such that 1<pv<P.

Public:
A point pb=pv*G.

Encryption needs:
ECP(P,A,B), point pb.

Decryption needs:
ECP(P,A,B), number pv.

If we hide G in private key, we can prevent public key generation.

I have a demonstration program using the algorithm from Wikipedia.
http://en.wikipedia.org/wiki/Integrated_Encryption_Scheme

For those who want to implement ECIES themselves, hope this could help.

Please correct me if I am wrong.

-- 
Gung Shi Jie
Department of Computer Science and Information Engineering,
National  Chung Cheng University
Republic of Taiwan

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
-~----------~----~----~----~------~----~------~--~---

// ECC.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <strstream> 
using namespace std;

#include <ecp.h>
#include <modarith.h>
#include <integer.h.>
#include <osrng.h>      // Random Number Generator
#include <sha.h>
#include <filters.h>
#include <hex.h>
using namespace CryptoPP;

CryptoPP::AutoSeededRandomPool rng;

string ItoS(Integer i){
	std::stringstream sb;
	sb<<i;
	string s;
	sb>>s;
	return s.substr(0,s.length()-1);
}

Integer Rand(Integer n){
	Integer r;
	r.Randomize(rng,4096);
	r%=n;
	return r;
}

void sha512(Integer n,byte * out){
	out=new byte[512/8];
	string s;
	s=ItoS(n);
	//512 bits hash
	CryptoPP::SHA512().CalculateDigest(out,(const byte*)(s.c_str()),s.length());
	
	CryptoPP::HexEncoder encoder;
	std::string output;
	encoder.Attach( new CryptoPP::StringSink( output ) );
	encoder.Put( out, sizeof(byte)*512/8 );
	encoder.MessageEnd();
	std::cout <<"SHA512 of key in hex "<<output << std::endl;
}

//Generate symmetric key using sha512
void keygen(Integer n,byte * pbOutputBuffer){
	cout<<"Generate random key size "<<n<<endl;
	Integer r=Rand(n);
	cout<<"Key="<<r<<endl;
	pbOutputBuffer=new byte[512/8];
	string s;
	s=ItoS(r);
	//512 bits hash
	CryptoPP::SHA512().CalculateDigest(pbOutputBuffer,(const byte*)(s.c_str()),s.length());
	
	CryptoPP::HexEncoder encoder;
	std::string output;
	encoder.Attach( new CryptoPP::StringSink( output ) );
	encoder.Put( pbOutputBuffer, sizeof(byte)*512/8 );
	encoder.MessageEnd();
	std::cout <<"SHA512 of key in hex "<<output << std::endl;

	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
	//Curve
	ECP * ecp = new ECP(CryptoPP::Integer("15580959959"),ECP::FieldElement("-1611856127"),ECP::FieldElement("-6381517944"));
	Integer p = ecp->FieldSize();
	//Base point G
	CryptoPP::ECP::Point G=CryptoPP::ECP::Point(CryptoPP::Integer("4397682358"),CryptoPP::Integer("14422084944"));
	//Private key of Alice
	Integer ipvA=Rand(p);		//From private to public needs, ECP,G. Save ECP,ipvA only.
	//Public key of Alice (Point)
	ECP::Point ppbA=ecp->Multiply(ipvA,G);	//Public save ECP,ppbA only
	
	//Alice Encrypt
	cout<<"Alice encrypt using Alice's public key"<<endl;		
	Integer r=Rand(p);	//r=Alice
	ECP::Point R=ecp->Multiply(r,G);	//R,G=Public

	byte * aeskey=0;
	sha512(ecp->Multiply(r,ppbA).x,aeskey);		//Using Alice's public key!
	cout<<"Symmetric key is "<<ecp->Multiply(r,ppbA).x<<endl;	//Encrypt needs ECP,public


	//Bob decrypt
	cout<<"Bob got symmetric key "<<ecp->Multiply(ipvA,R).x<<endl;	//Decrypt needs ECP,private
	//No base point G, thus unable  to make keygen?!

	return 0;
}

Reply via email to