encryption - Error occurred while decoding OAEP Padding C# -


below code , error in title. encrypted using private key decrypt using same private key ran error mentioned in title, not sure underlying cause of error. in regard highly appreciated.

class program {     static void main(string[] args)     {        string thumbprint = "somevalue";        encryptusername("steve", thumbprint, true, true);     }     public static void encryptusername(string texttoencript, string certificatethumbprint, bool searchlocalmachine, bool searchuser)     {         x509certificate2 cert = findcertificate(certificatethumbprint, searchlocalmachine, searchuser);         rsacryptoserviceprovider rsaencryptor = (rsacryptoserviceprovider)cert.publickey.key;         byte[] cipherdata = rsaencryptor.encrypt(encoding.utf8.getbytes(texttoencript), true);         var encryptedstring  = convert.tobase64string(cipherdata);         console.writeline(encryptedstring);     }     public static byte[] decrypt(byte[] encrypteddata, bool foaep, x509certificate2 certificate)     {         if (encrypteddata == null)         {             throw new argumentnullexception("encrypteddata");         }         if (certificate == null)         {             throw new argumentnullexception("certificate");         }         if (certificate.privatekey == null)         {             throw new applicationexception("certificate provided has no private key");         }         console.writeline(certificate.privatekey);         using (rsacryptoserviceprovider provider = (rsacryptoserviceprovider)certificate.privatekey)         {             return provider.decrypt(encrypteddata, foaep);         }     }      public static string certificatedecrypt(string texttodecript, string certificatethumbprint, bool searchlocalmachine, bool searchuser)     {         x509certificate2 certificate = findcertificate(certificatethumbprint, searchlocalmachine, searchuser);         byte[] bytesarray = convert.frombase64string(texttodecript);         //decrypt(bytesarray, true, certificate);         return encoding.utf8.getstring(decrypt(bytesarray, true, certificate));     }     public static x509certificate2 loadcertificate(storename storename, storelocation storelocation, string thumbprint)     {         x509store store = null;         x509certificate2 certificate2;         try         {             store = new x509store(storename, storelocation);             store.open(openflags.readonly);             x509certificate2enumerator enumerator = store.certificates.find(x509findtype.findbythumbprint, thumbprint, false).getenumerator();             x509certificate2 current = null;             while (enumerator.movenext())             {                 current = enumerator.current;             }             certificate2 = current;         }                 {             if (store != null)             {                 store.close();             }         }         return certificate2;     }     private static x509certificate2 findcertificate(string certificatethumbprint, bool searchlocalmachine, bool searchuser)     {         certificatethumbprint = certificatethumbprint.replace(" ", "");         x509certificate2 certificate = null;         if (searchuser)         {             certificate = loadcertificate(storename.my, storelocation.currentuser, certificatethumbprint);         }         if (searchlocalmachine && (certificate == null))         {             certificate = loadcertificate(storename.my, storelocation.localmachine, certificatethumbprint);         }         if (certificate == null)         {             throw new applicationexception($"certificate thumbprint {certificatethumbprint} cannot loaded (not found)");         }         return certificate;     } } 

error occurred while decoding oaep padding happens when either input string decrypt not same output string of encrypt, or because public key used encryption not match private key used decryption.

in case, not passing encrypted string decrypt method absolutely fail, making encryption , decryption key not match.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -