Note that there are some explanatory texts on larger screens.

plurals
  1. POCryptographicException: Error occurred while decoding OAEP padding
    primarykey
    data
    text
    <p>In continuing of <a href="https://stackoverflow.com/questions/954416/error-occurred-while-decoding-oaep-padding">Error occurred while decoding OAEP padding</a></p> <p>Although I've read all previous asks/posts here I couldn't find a solution.</p> <p>I'm saving the cryptographed key in a file. Later I'm decrypting it by a Windows Service. Both processes are running under the same machine and calling the same assembly (core) to encrypt and decrypt</p> <p>I'm stucked since I can't understand why it's throwing an exception.</p> <p>my code</p> <pre><code>class CryptographyKeyProvider : ICryptographyKeyProvider, ICryptographySaver { private readonly ICryptographyFilesProvider _provider; public CryptographyKeyProvider(ICryptographyFilesProvider provider) { _provider = provider; } #region Util private static readonly CryptoKeyAccessRule Rule = new CryptoKeyAccessRule(WindowsIdentity.GetCurrent().Name.ToLower().Split('\\').Last(), CryptoKeyRights.FullControl, AccessControlType.Allow); private static CspParameters CspProvider(string keyContainerName, CspProviderFlags flags = CspProviderFlags.NoFlags) { var cp = new CspParameters { KeyContainerName = keyContainerName, Flags = CspProviderFlags.NoPrompt | CspProviderFlags.UseMachineKeyStore | flags, CryptoKeySecurity = new CryptoKeySecurity() }; cp.CryptoKeySecurity.SetAccessRule(Rule); return cp; } private static void SaveFile(string keyFile, string encryptedPw) { try { File.WriteAllText(keyFile, encryptedPw); } catch (Exception ex) { throw new Exception("Error saving file. " + ex.Message); } } private static string Encrypt(string text, CspParameters cp) { string encryptedPw; try { using (var rsa = new RSACryptoServiceProvider(cp)) encryptedPw = Convert.ToBase64String(rsa.Encrypt(Encoding.Unicode.GetBytes(text), true)); } catch (Exception ex) { throw new Exception("Errror encrypting data. " + ex.Message); } return encryptedPw; } private static void ClearContainer(CspParameters cp) { try { using (var rsa = new RSACryptoServiceProvider(cp) { PersistKeyInCsp = false }) rsa.Clear(); } catch (CryptographicException ex) { } catch (Exception ex) { throw new Exception("Error cleaning encrypt key. " + ex.Message); } } private static byte[] Decrypt(string containerName, string encriptedData) { RSACryptoServiceProvider rsa; try { var cp = CspProvider(containerName, CspProviderFlags.UseExistingKey); rsa = new RSACryptoServiceProvider(cp); } catch (Exception ex) { throw new Exception("Error accessing container. " + ex.Message); } return rsa.Decrypt(Convert.FromBase64String(encriptedData), true); } #endregion public byte[] Key1 { get { var encriptedData = File.ReadAllText(_provider.Key1File); return Decrypt(_provider.Key1ContainerName, encriptedData); } } public void SaveKeyOwner(string text) { if (File.Exists(_provider.Key1File)) { throw new Exception("File already exists"); } var cp = CspProvider(_provider.Key1ContainerName); ClearContainer(cp); SaveFile(_provider.Key1File, Encrypt(text, cp)); } } </code></pre> <p>Exception</p> <pre><code>System.Security.Cryptography.CryptographicException: Error occurred while decoding OAEP padding. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload