![]() |
|
[.NET] rsa encryption exception (Key not valid for use in specified state.) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] rsa encryption exception (Key not valid for use in specified state.) (/showthread.php?tid=5360380) |
rsa encryption exception (Key not valid for use in specified state.) - x factor fan - Aug 1, 2016 hi friends.. today i was woking on a app that would encrypt a file to keep it safe from other ppl (i would pick a file to encrypt then i would need to enter passwords to unlock file).. the code works very good but the problem is when i encrypt a file over 255 bytes i get eception :( that say Key not valid for use in specified state ).. what can be problem? the key is fine.. any help would be great. thanks friends)).. my code.. Code static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)i am russian by the way sorry for my english (( * - HFDone - Aug 2, 2016 RSA.ExportParameters(true) * - x factor fan - Aug 2, 2016 (Aug 2, 2016 12:46 AM)HFDone Wrote: RSA.ExportParameters(true) hi.. i tried that and it gave me unspecified error exception (( 👍 * - debug - Aug 2, 2016 The problem is your RSA key format. I had a similar issue. Use this command on unix to format it correctly: openssl rsa -in server.key -out server_new.key * - 6e756c6c - Aug 2, 2016 The problem is RSA can only encrypt data with a length less than or equal to it's key size in bytes. So if you have a 4096 RSA key it can encrypt up to 512 bytes of data. Instead, you should be encrypting with AES or if you are really set on using RSA then you need to: A.) Encrypt with AES, then encrypt the AES key using RSA and append this to the cipher text. B.) Encrypt your data in chunks of 128, 256, 512, etc. bytes. This is going to be VERY slow so I would not recommend it. * - x factor fan - Aug 2, 2016 (Aug 2, 2016 09:23 AM)Fire+Rain Wrote: The problem is RSA can only encrypt data with a length less than or equal to it's key size in bytes. So if you have a 4096 RSA key it can encrypt up to 512 bytes of data. Instead, you should be encrypting with AES or if you are really set on using RSA then you need to: thank u for the info.. i will try later and report back.. 👍 * - x factor fan - Aug 7, 2016 hello friends, i have switched to aes encryption but i am facing 1 big problem (( i was able to create a encryt function very easily (And it work) but i am struggle on how to make decrypt function.. my encrypt function: Code public static void EncryptFile(string filePath, byte[] key, byte[] iv)i am getting my key and iv from this function and storing them in 2 seperate variables.. Code public static byte[] GenerateKey()i am now trying to figure out how to write a decrypt function.. this is how far i got before i get stuck (( :D Code public static void DecryptFile(string filePath, byte[] key, byte[] iv)any help would be appreciated my friends )) 👍 so i guess no one wants to help me (( |