
Throw new ArgumentNullException(nameof(plainText)) String decrypt = ControlHelperscs.DecryptStringFromBytes_Aes(file, key, iv) Īnd here are the slightly reworked heavy-lifting methods to be a bit more compact: public static byte EncryptStringToBytes_Aes(string plainText, byte key, byte iv) String json = JsonConvert.SerializeObject(credentials) īyte encrypted = ControlHelperscs.EncryptStringToBytes_Aes(json, key, iv) So, here is the part to make it work: byte key The problem is that you're generating a new and different Key/IV pair to decrypt from the one you used to encrypt. I'm sorry I deleted my comment (it was wrong in that context), but I reworked your example to have a little less boilerplate and be able to encrypt and decrypt properly.

Read the decrypted bytes from the decrypting stream Using (StreamReader srDecrypt = new StreamReader(csDecrypt)) Using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) Using (MemoryStream msDecrypt = new MemoryStream(cipherText)) Create the streams used for decryption. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV) Create a decryptor to perform the stream transform. Throw new ArgumentNullException("cipherText") If (cipherText = null || cipherText.Length <= 0) Public static string DecryptStringFromBytes_Aes(byte cipherText, byte Key, byte IV) Return the encrypted bytes from the memory stream. Using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) Using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) Using (MemoryStream msEncrypt = new MemoryStream()) Create the streams used for encryption. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV) Create an encryptor to perform the stream transform. Using (AesManaged aesAlg = new AesManaged()) Throw new ArgumentNullException("plainText") If (plainText = null || plainText.Length <= 0) Here are Encrypt and Decrypt methods: public static byte EncryptStringToBytes_Aes(string plainText, byte Key, byte IV) String decrypt = ControlHelperscs.DecryptStringFromBytes_Aes(file, myAes.Key, myAes.IV) Ĭredentials = JsonConvert.DeserializeObject(decrypt)
ENCRYPTSTICK LITE INVALID AND CANNOT BE OPENED CODE
Here is my code to Retrieve and Decrypt file: using (AesManaged myAes = new AesManaged())īyte file = File.ReadAllBytes(subPath) Using (AesManaged myAes = new AesManaged())īyte encrypted = ControlHelperscs.EncryptStringToBytes_Aes(json, myAes.Key, myAes.IV) Here is MY code to encrypt and save file: string json = JsonConvert.SerializeObject(credentials) When I try to Decrypt it I receive the error "Padding is Invalid and Cannot Be Removed." I am using example code directly from Microsoft for encryption and decryption. I am trying to save encrypted data to a Text file and then open and decrypt it.
