RSACryptoServiceProvider Issue


 

i new technology please bare me. have 2 methods encrypt , decrypt data using rsacryptoserviceprovide in wcf service.

encrypting data

public byte[] rsaencrypt(byte[] datatoencrypt, rsaparameters rsakeyinfo, bool dooaeppadding)    {     try     {      byte[] encrypteddata;        using (var rsacryptoserviceprovider = new rsacryptoserviceprovider())      {       rsacryptoserviceprovider.importparameters(rsakeyinfo);         encrypteddata = rsacryptoserviceprovider.encrypt(datatoencrypt, dooaeppadding);      }        return encrypteddata;     }     catch (cryptographicexception cryptographicexception)     {      eventlog.writeentry(appname, "[rsaencrypt error] cryptographicexception: " + cryptographicexception, eventlogentrytype.error);        return null;     }     catch (exception exception)     {      eventlog.writeentry(appname, "[rsaencrypt error] exception: " + exception, eventlogentrytype.error);        return null;     }    }

 

decrypting data

public byte[] rsadecrypt(byte[] datatodecrypt, rsaparameters rsakeyinfo, bool dooaeppadding)    {     try     {      byte[] decrypteddata;        using (var rsacryptoserviceprovider = new rsacryptoserviceprovider())      {       rsacryptoserviceprovider.importparameters(rsakeyinfo);         decrypteddata = rsacryptoserviceprovider.decrypt(datatodecrypt, dooaeppadding);      }        return decrypteddata;     }     catch (cryptographicexception cryptographicexception)     {      eventlog.writeentry(appname, "[rsadecrypt error] cryptographicexception: " + cryptographicexception, eventlogentrytype.error);        return null;     }     catch (exception exception)     {      eventlog.writeentry(appname, "[rsadecrypt error] exception: " + exception, eventlogentrytype.error);        return null;     }    }

 


exposed interface encrypting data wpf client. wpf client encrypts data using exposed method above andwith this, tried encrypting password during authentication.

 

unicodeencoding byteconverter = new unicodeencoding();    byte[] encryptedpassword;    cspparameters parms = new cspparameters();  parms.flags = cspproviderflags.noflags;  parms.keycontainername = guid.newguid().tostring().toupperinvariant();  parms.providertype = ((environment.osversion.version.major > 5) || ((environment.osversion.version.major == 5) && (environment.osversion.version.minor >= 1))) ? 0x18 : 1;    using (var rsakey = new rsacryptoserviceprovider(parms))  {   encryptedpassword = _mywcfservice.rsaencrypt(byteconverter.getbytes(txtpassword.text),     rsakey.exportparameters(false), false);  }    user user = _mywcfservice.authenticate(txtusername.text, 0, encryptedpassword, parms.keycontainername);  


, in wcf service, decrypt password:

 

public user authenticate(string username, int testid, byte[] password, string keycontainername)  {   driver driver = null;     usersession session = addusersession();     try   {    #region decrypt data      _byteconverter = new unicodeencoding();      byte[] decryptedpassword;      var cspparameters = new cspparameters          {           keycontainername = keycontainername          };      using (var rsakey = new rsacryptoserviceprovider(cspparameters))    {     <strong>decryptedpassword </strong>= rsadecrypt(password, rsakey.exportparameters(true), false);    }      #endregion      // problem: decryptedpassword null..      // connect database , authenticate user...   }   catch(sqlexception sqlexception)   {    eventlog.writeentry(appname, "[authenticate error] sqlexception: " + sqlexception, eventlogentrytype.error);       }   catch (exception exception)   {    eventlog.writeentry(appname, "[authenticate error] exception: " + exception, eventlogentrytype.error);   }     return user;  }  

however, decrypted password null means there wrong decryption process (rsadecrypt). in host server wcf service installed, notice error in event log:

 

[authenticate error] exception: system.argumentnullexception: array cannot null.
parameter name: bytes
   @ system.text.encoding.getstring(byte[] bytes)
   @ samplewcfservice.authenticate(string username, int32 testid, byte[] password, string keycontainername)

also, observed when deploying wcf service on same machine wpf client, seems work fine. however, when wpf client connecting machine error.

 

any thoughts why happening?

 

 

 


"when key container, code snippet below saves keys key container?"

almost. have call method ensure key generated , stored. say exportparameters(false).

"and sharing public key, can code snippet trick?"

yes, or toxmlstring(false). depends what's more convenient you.

"and using private key in server, have use code below private key decryption."

hmm, no need export. initialize rsacryptoprovider cspparameters have same keycontainername use when generated key.



.NET Framework  >  Common Language Runtime Internals and Architecture



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'