// not needed std stuff //#include <iostream> #include <Web/Web.h> //not needed, too //using namespace std; using namespace UPP; //write this, otherwise upp functions won't get recognised int main() { char *text = "Hola a todos"; String salida; //in this case seems better to use String - NOTE: this is Ultimate++ String, not std::string //text = "Hola a todos"; // you can write a thing like this only at initialization time salida = Base64Encode(MD5Digest(text)); // if you write *text you pass MD5Digest a _single_ char, not a vector of chars //Upp way to std::cout is writing Cout() Cout() << "Salida en base64 para la digestion de " << text << " = " << salida; return 0; //Why return 1? If we got so far return 0 (no error) seems better }
struct Rsa : public Moveable<Rsa> { Rsa() { rsa = NULL; } ~Rsa() { if(rsa) RSA_free(rsa); } void GenerateKeyPair(int bits = 1024, int exponent = 17); String PrivateKeyToPem(); String PublicKeyToPem(); void PrivateKeyFromPem(const String &pem); void PrivateKeyFromPem(uint8 *d, int l); void PublicKeyFromPem(const String &pem); void PublicKeyFromPem(uint8 *d, int l); String SignSHA(const String &msg); String Decrypt(const String &msg, int padding = RSA_PKCS1_OAEP_PADDING); bool VerifySHA(const String &msg, const String &sig); String Encrypt(const String &msg, int padding = RSA_PKCS1_OAEP_PADDING); int MaxMsgCount(int padding = RSA_PKCS1_OAEP_PADDING); void Serialize(Stream &s); protected: RSA *rsa; };
#ifdef _DEBUG TEST(Rsa) { Rsa rsa; rsa.GenerateKeyPair(512); String pri = rsa.PrivateKeyToPem(); String pub = rsa.PublicKeyToPem(); Rsa rsa2; rsa2.PrivateKeyFromPem(pri); String pri2 = rsa2.PrivateKeyToPem(); String pub2 = rsa.PublicKeyToPem(); CHECK(pri == pri2); CHECK(pub == pub2); CHECK(rsa.VerifySHA("Kleiner Test", rsa2.SignSHA("Kleiner Test"))); CHECK(rsa.Decrypt(rsa2.Encrypt("Kleiner Test")) == "Kleiner Test"); CHECK(!rsa.VerifySHA("Kleiner Test", rsa2.SignSHA("@Kleiner Test"))); } #endif
void firmafactSAT_GUI::CargarSello() //Carga archvio del sello { B64.Clear(); //Clean result editfield Cadena=CadenaOriginal.GetData(); //Get the string to sign ArchivoFirma.Type("Sello Digital", "*.key"); //file type to the key file if (ArchivoFirma.ExecuteOpen("Seleccione el archivo de sello digital")) { RutaSello.SetText(ArchivoFirma.Get()); //Path to key file if (StreamArchivoFirma.Open(ArchivoFirma.Get())) //Open the file stream PromptOK("Archivo abierto correctamente!!!"); //only to test B64=Base64Encode(rsa.SignMD5(Cadena)); //sign the string and convert to base64 //FirmaDigital=RSA_new(); //FirmaDigital=StreamArchivoFirma.; } }
class firmafactSAT_GUI : public WithfirmafactSAT_GUILayout<TopWindow> { String Cadena; // Para guardar la cadena original del campo de edicion String Digestion; // Para guardar la digestion de la cadena String Hexadecimal; // Para guardar la digestion expresada en Hexadecimal String B64; // Para guardar la digestion expresada en base64 Rsa rsa; // <-- here is //RSA *FirmaDigital; FileSel ArchivoFirma; //Selector para cargar el archivo de la firma FileIn StreamArchivoFirma; //Stream del archvio de la firma ....... etc.
struct Rsa : public Moveable<Rsa> { Rsa() { rsa = NULL; } ~Rsa() { if(rsa) RSA_free(rsa); } void GenerateKeyPair(int bits = 1024, int exponent = 17); String PrivateKeyToPem(); String PublicKeyToPem(); void PrivateKeyFromPem(const String &pem); void PrivateKeyFromPem(uint8 *d, int l); void PublicKeyFromPem(const String &pem); void PublicKeyFromPem(uint8 *d, int l); String SignSHA(const String &msg); String SignMD5(const String &msg); // Added by me String Decrypt(const String &msg, int padding = RSA_PKCS1_OAEP_PADDING); bool VerifySHA(const String &msg, const String &sig); bool VerifyMD5(const String &msg, const String &sig); // Added by me String Encrypt(const String &msg, int padding = RSA_PKCS1_OAEP_PADDING); int MaxMsgCount(int padding = RSA_PKCS1_OAEP_PADDING); void Serialize(Stream &s); protected: RSA *rsa; };
String Rsa::SignMD5(const String &msg) { ASSERT(rsa); String ret(0, RSA_size(rsa)); unsigned int len; uint8 h[16]; MD5((uint8 *)~msg, msg.GetCount(), h); RSA_sign(NID_md5, h, 16, (uint8 *)~ret, &len, rsa); ret.Trim(len); return ret; } bool Rsa::VerifyMD5(const String &msg, const String &sig) { ASSERT(rsa); uint8 h[16]; MD5((uint8 *)~msg, msg.GetCount(), h); return RSA_verify(NID_md5, h, 16, (uint8 *)~sig, sig.GetCount(), rsa); }
#ifdef _DEBUG TEST(Rsa) { Rsa rsa; rsa.GenerateKeyPair(512); String pri = rsa.PrivateKeyToPem(); String pub = rsa.PublicKeyToPem(); Rsa rsa2; rsa2.PrivateKeyFromPem(pri); String pri2 = rsa2.PrivateKeyToPem(); String pub2 = rsa.PublicKeyToPem(); CHECK(pri == pri2); CHECK(pub == pub2); CHECK(rsa.VerifySHA("Kleiner Test", rsa2.SignSHA("Kleiner Test"))); CHECK(rsa.Decrypt(rsa2.Encrypt("Kleiner Test")) == "Kleiner Test"); CHECK(!rsa.VerifySHA("Kleiner Test", rsa2.SignSHA("@Kleiner Test"))); } #endif
#include <UnitTest/UnitTest.h>
Quote: |
But when I compile the project, the compiler show me this error: C:\ElectroFactUPP\firmafactSAT_GUI\main.cpp: In member function `void firmafactSAT_GUI::CargarSe llo()': C:\ElectroFactUPP\firmafactSAT_GUI\main.cpp:53: error: `rsa' undeclared (first use this function ) I need to put a include to a some file? Thanks for the help... |
#include <openssl/md5.h>
#ifndef _CryptOpenSsl_CryptOpenSsl_h_ #define _CryptOpenSsl_CryptOpenSsl_h_ // --------------------------------------------------------------------------------------------- #include <Core/Core.h> #include <UnitTest/UnitTest.h> #define OPENSSL_THREAD_DEFINES #include <openssl/opensslconf.h> #if !defined(OPENSSL_THREADS) @@@ #endif #include <openssl/rsa.h> #include <openssl/md5.h> #include <openssl/engine.h> #include <openssl/pem.h> using namespace Upp; // --------------------------------------------------------------------------------------------- String ToString(BIGNUM *b); BIGNUM * ToBigNum(String& str); String ToString(BIO *bp); BIO * ToBIO(String &str); // --------------------------------------------------------------------------------------------- struct Rsa : public Moveable<Rsa> { Rsa() { rsa = NULL; } ~Rsa() { if(rsa) RSA_free(rsa); } void GenerateKeyPair(int bits = 1024, int exponent = 17); String PrivateKeyToPem(); String PublicKeyToPem(); void PrivateKeyFromPem(const String &pem); void PrivateKeyFromPem(uint8 *d, int l); void PublicKeyFromPem(const String &pem); void PublicKeyFromPem(uint8 *d, int l); String SignSHA(const String &msg); String SignMD5(const String &msg); // Added by me String Decrypt(const String &msg, int padding = RSA_PKCS1_OAEP_PADDING); bool VerifySHA(const String &msg, const String &sig); bool VerifyMD5(const String &msg, const String &sig); // Added by me String Encrypt(const String &msg, int padding = RSA_PKCS1_OAEP_PADDING); int MaxMsgCount(int padding = RSA_PKCS1_OAEP_PADDING); void Serialize(Stream &s); protected: RSA *rsa; }; // --------------------------------------------------------------------------------------------- #endif