“Padding is invalid and cannot be removed” during decryption












0















First, I realize there are dozens of other posts that have answers to this question and I have read and tried them all. I still can't seem to get past this issue so am looking for a little help from somebody that knows more about crypto than I do.



Second, the code I am going to share is legacy and because I am not a crypto expert it is still not 100% clear on what everything means. It may be that some or all of this code is total rubbish and should be scrapped; however, there are a lot of other systems already using it and have stored encrypted values that have gone through this code. Changing things like the crypto algorithm is not exactly an option at this point. With that said, the private methods are the legacy code as well as the testing values (i.e. the encryption key) are all things that can't change. The two public static methods are what is new and likely causing problems, but I can't seem to figure it out.



On with the code......



class Program
{
public static string Encrypt(string key, string toEncrypt)
{
var keyArray = Convert.FromBase64String(key);
var info = Encoding.ASCII.GetBytes(toEncrypt);

var encrypted = Encrypt(keyArray, info);

return Encoding.ASCII.GetString(encrypted);
}

public static string Decrypt(string key, string cipherString)
{
var keyArray = Convert.FromBase64String(key);
var cipherText = Encoding.ASCII.GetBytes(cipherString);

var decrypted = Decrypt(keyArray, cipherText);

return Encoding.ASCII.GetString(decrypted);
}

private static byte Encrypt(byte key, byte info)
{
using (var cipher = Aes.Create())
{
cipher.Key = key;
cipher.Mode = CipherMode.CBC;
cipher.Padding = PaddingMode.ISO10126;

using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, cipher.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(info, 0, info.Length);
}

var ciphertext = ms.ToArray();

var message = new byte[cipher.IV.Length + ciphertext.Length];
cipher.IV.CopyTo(message, 0);
ciphertext.CopyTo(message, cipher.IV.Length);
return message;
}
}
}

private static byte Decrypt(byte key, byte ciphertext)
{
using (var cipher = Aes.Create())
{
cipher.Key = key;
cipher.Mode = CipherMode.CBC;
cipher.Padding = PaddingMode.ISO10126;

var ivSize = cipher.IV.Length;
var iv = new byte[ivSize];
Array.Copy(ciphertext, iv, ivSize);
cipher.IV = iv;

var data = new byte[ciphertext.Length - ivSize];
Array.Copy(ciphertext, ivSize, data, 0, data.Length);

using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, cipher.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
}

return ms.ToArray();
}
}
}

static void Main(string args)
{
var newEncryptionKey = Guid.NewGuid().ToString().Replace("-", string.Empty);

var encryptedValue = Encrypt(newEncryptionKey, "test");

Console.WriteLine($"New encrypted value: {encryptedValue}");

var decryptedValue = Decrypt(newEncryptionKey, encryptedValue);

Console.WriteLine($"New decrypted value: {decryptedValue}");
}
}


So there it is. Basically, I am trying to use a test string of "test" and encrypt it using a GUID as a key. Again, I didn't choose this key and there are encrypted values already using a GUID as a key so I can't change that if at all possible. The encryption works fine, but when I go to do the decryption, I get the exception noted in the title of this question.



Any help would be GREATLY appreciated.










share|improve this question



























    0















    First, I realize there are dozens of other posts that have answers to this question and I have read and tried them all. I still can't seem to get past this issue so am looking for a little help from somebody that knows more about crypto than I do.



    Second, the code I am going to share is legacy and because I am not a crypto expert it is still not 100% clear on what everything means. It may be that some or all of this code is total rubbish and should be scrapped; however, there are a lot of other systems already using it and have stored encrypted values that have gone through this code. Changing things like the crypto algorithm is not exactly an option at this point. With that said, the private methods are the legacy code as well as the testing values (i.e. the encryption key) are all things that can't change. The two public static methods are what is new and likely causing problems, but I can't seem to figure it out.



    On with the code......



    class Program
    {
    public static string Encrypt(string key, string toEncrypt)
    {
    var keyArray = Convert.FromBase64String(key);
    var info = Encoding.ASCII.GetBytes(toEncrypt);

    var encrypted = Encrypt(keyArray, info);

    return Encoding.ASCII.GetString(encrypted);
    }

    public static string Decrypt(string key, string cipherString)
    {
    var keyArray = Convert.FromBase64String(key);
    var cipherText = Encoding.ASCII.GetBytes(cipherString);

    var decrypted = Decrypt(keyArray, cipherText);

    return Encoding.ASCII.GetString(decrypted);
    }

    private static byte Encrypt(byte key, byte info)
    {
    using (var cipher = Aes.Create())
    {
    cipher.Key = key;
    cipher.Mode = CipherMode.CBC;
    cipher.Padding = PaddingMode.ISO10126;

    using (var ms = new MemoryStream())
    {
    using (var cs = new CryptoStream(ms, cipher.CreateEncryptor(), CryptoStreamMode.Write))
    {
    cs.Write(info, 0, info.Length);
    }

    var ciphertext = ms.ToArray();

    var message = new byte[cipher.IV.Length + ciphertext.Length];
    cipher.IV.CopyTo(message, 0);
    ciphertext.CopyTo(message, cipher.IV.Length);
    return message;
    }
    }
    }

    private static byte Decrypt(byte key, byte ciphertext)
    {
    using (var cipher = Aes.Create())
    {
    cipher.Key = key;
    cipher.Mode = CipherMode.CBC;
    cipher.Padding = PaddingMode.ISO10126;

    var ivSize = cipher.IV.Length;
    var iv = new byte[ivSize];
    Array.Copy(ciphertext, iv, ivSize);
    cipher.IV = iv;

    var data = new byte[ciphertext.Length - ivSize];
    Array.Copy(ciphertext, ivSize, data, 0, data.Length);

    using (var ms = new MemoryStream())
    {
    using (var cs = new CryptoStream(ms, cipher.CreateDecryptor(), CryptoStreamMode.Write))
    {
    cs.Write(data, 0, data.Length);
    }

    return ms.ToArray();
    }
    }
    }

    static void Main(string args)
    {
    var newEncryptionKey = Guid.NewGuid().ToString().Replace("-", string.Empty);

    var encryptedValue = Encrypt(newEncryptionKey, "test");

    Console.WriteLine($"New encrypted value: {encryptedValue}");

    var decryptedValue = Decrypt(newEncryptionKey, encryptedValue);

    Console.WriteLine($"New decrypted value: {decryptedValue}");
    }
    }


    So there it is. Basically, I am trying to use a test string of "test" and encrypt it using a GUID as a key. Again, I didn't choose this key and there are encrypted values already using a GUID as a key so I can't change that if at all possible. The encryption works fine, but when I go to do the decryption, I get the exception noted in the title of this question.



    Any help would be GREATLY appreciated.










    share|improve this question

























      0












      0








      0








      First, I realize there are dozens of other posts that have answers to this question and I have read and tried them all. I still can't seem to get past this issue so am looking for a little help from somebody that knows more about crypto than I do.



      Second, the code I am going to share is legacy and because I am not a crypto expert it is still not 100% clear on what everything means. It may be that some or all of this code is total rubbish and should be scrapped; however, there are a lot of other systems already using it and have stored encrypted values that have gone through this code. Changing things like the crypto algorithm is not exactly an option at this point. With that said, the private methods are the legacy code as well as the testing values (i.e. the encryption key) are all things that can't change. The two public static methods are what is new and likely causing problems, but I can't seem to figure it out.



      On with the code......



      class Program
      {
      public static string Encrypt(string key, string toEncrypt)
      {
      var keyArray = Convert.FromBase64String(key);
      var info = Encoding.ASCII.GetBytes(toEncrypt);

      var encrypted = Encrypt(keyArray, info);

      return Encoding.ASCII.GetString(encrypted);
      }

      public static string Decrypt(string key, string cipherString)
      {
      var keyArray = Convert.FromBase64String(key);
      var cipherText = Encoding.ASCII.GetBytes(cipherString);

      var decrypted = Decrypt(keyArray, cipherText);

      return Encoding.ASCII.GetString(decrypted);
      }

      private static byte Encrypt(byte key, byte info)
      {
      using (var cipher = Aes.Create())
      {
      cipher.Key = key;
      cipher.Mode = CipherMode.CBC;
      cipher.Padding = PaddingMode.ISO10126;

      using (var ms = new MemoryStream())
      {
      using (var cs = new CryptoStream(ms, cipher.CreateEncryptor(), CryptoStreamMode.Write))
      {
      cs.Write(info, 0, info.Length);
      }

      var ciphertext = ms.ToArray();

      var message = new byte[cipher.IV.Length + ciphertext.Length];
      cipher.IV.CopyTo(message, 0);
      ciphertext.CopyTo(message, cipher.IV.Length);
      return message;
      }
      }
      }

      private static byte Decrypt(byte key, byte ciphertext)
      {
      using (var cipher = Aes.Create())
      {
      cipher.Key = key;
      cipher.Mode = CipherMode.CBC;
      cipher.Padding = PaddingMode.ISO10126;

      var ivSize = cipher.IV.Length;
      var iv = new byte[ivSize];
      Array.Copy(ciphertext, iv, ivSize);
      cipher.IV = iv;

      var data = new byte[ciphertext.Length - ivSize];
      Array.Copy(ciphertext, ivSize, data, 0, data.Length);

      using (var ms = new MemoryStream())
      {
      using (var cs = new CryptoStream(ms, cipher.CreateDecryptor(), CryptoStreamMode.Write))
      {
      cs.Write(data, 0, data.Length);
      }

      return ms.ToArray();
      }
      }
      }

      static void Main(string args)
      {
      var newEncryptionKey = Guid.NewGuid().ToString().Replace("-", string.Empty);

      var encryptedValue = Encrypt(newEncryptionKey, "test");

      Console.WriteLine($"New encrypted value: {encryptedValue}");

      var decryptedValue = Decrypt(newEncryptionKey, encryptedValue);

      Console.WriteLine($"New decrypted value: {decryptedValue}");
      }
      }


      So there it is. Basically, I am trying to use a test string of "test" and encrypt it using a GUID as a key. Again, I didn't choose this key and there are encrypted values already using a GUID as a key so I can't change that if at all possible. The encryption works fine, but when I go to do the decryption, I get the exception noted in the title of this question.



      Any help would be GREATLY appreciated.










      share|improve this question














      First, I realize there are dozens of other posts that have answers to this question and I have read and tried them all. I still can't seem to get past this issue so am looking for a little help from somebody that knows more about crypto than I do.



      Second, the code I am going to share is legacy and because I am not a crypto expert it is still not 100% clear on what everything means. It may be that some or all of this code is total rubbish and should be scrapped; however, there are a lot of other systems already using it and have stored encrypted values that have gone through this code. Changing things like the crypto algorithm is not exactly an option at this point. With that said, the private methods are the legacy code as well as the testing values (i.e. the encryption key) are all things that can't change. The two public static methods are what is new and likely causing problems, but I can't seem to figure it out.



      On with the code......



      class Program
      {
      public static string Encrypt(string key, string toEncrypt)
      {
      var keyArray = Convert.FromBase64String(key);
      var info = Encoding.ASCII.GetBytes(toEncrypt);

      var encrypted = Encrypt(keyArray, info);

      return Encoding.ASCII.GetString(encrypted);
      }

      public static string Decrypt(string key, string cipherString)
      {
      var keyArray = Convert.FromBase64String(key);
      var cipherText = Encoding.ASCII.GetBytes(cipherString);

      var decrypted = Decrypt(keyArray, cipherText);

      return Encoding.ASCII.GetString(decrypted);
      }

      private static byte Encrypt(byte key, byte info)
      {
      using (var cipher = Aes.Create())
      {
      cipher.Key = key;
      cipher.Mode = CipherMode.CBC;
      cipher.Padding = PaddingMode.ISO10126;

      using (var ms = new MemoryStream())
      {
      using (var cs = new CryptoStream(ms, cipher.CreateEncryptor(), CryptoStreamMode.Write))
      {
      cs.Write(info, 0, info.Length);
      }

      var ciphertext = ms.ToArray();

      var message = new byte[cipher.IV.Length + ciphertext.Length];
      cipher.IV.CopyTo(message, 0);
      ciphertext.CopyTo(message, cipher.IV.Length);
      return message;
      }
      }
      }

      private static byte Decrypt(byte key, byte ciphertext)
      {
      using (var cipher = Aes.Create())
      {
      cipher.Key = key;
      cipher.Mode = CipherMode.CBC;
      cipher.Padding = PaddingMode.ISO10126;

      var ivSize = cipher.IV.Length;
      var iv = new byte[ivSize];
      Array.Copy(ciphertext, iv, ivSize);
      cipher.IV = iv;

      var data = new byte[ciphertext.Length - ivSize];
      Array.Copy(ciphertext, ivSize, data, 0, data.Length);

      using (var ms = new MemoryStream())
      {
      using (var cs = new CryptoStream(ms, cipher.CreateDecryptor(), CryptoStreamMode.Write))
      {
      cs.Write(data, 0, data.Length);
      }

      return ms.ToArray();
      }
      }
      }

      static void Main(string args)
      {
      var newEncryptionKey = Guid.NewGuid().ToString().Replace("-", string.Empty);

      var encryptedValue = Encrypt(newEncryptionKey, "test");

      Console.WriteLine($"New encrypted value: {encryptedValue}");

      var decryptedValue = Decrypt(newEncryptionKey, encryptedValue);

      Console.WriteLine($"New decrypted value: {decryptedValue}");
      }
      }


      So there it is. Basically, I am trying to use a test string of "test" and encrypt it using a GUID as a key. Again, I didn't choose this key and there are encrypted values already using a GUID as a key so I can't change that if at all possible. The encryption works fine, but when I go to do the decryption, I get the exception noted in the title of this question.



      Any help would be GREATLY appreciated.







      c# encryption






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 3 '16 at 21:54









      meyousikmannmeyousikmann

      4371421




      4371421
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can't just convert a byte of ciphertext to ASCII. It doesn't work like that. Character encodings are scary beasts and should not be muddled with if you don't understand them. I don't think there is a real person alive that does ;)



          What you should do instead is return your result as base64, which is still a collection of ASCII characters but they are safe to be moved around as a string, and don't result in the loss of any characters.



          See the modified code below:



          public static string Encrypt(string key, string toEncrypt)
          {
          var keyArray = Convert.FromBase64String(key);
          var info = Encoding.ASCII.GetBytes(toEncrypt);

          var encrypted = Encrypt(keyArray, info);

          return Convert.ToBase64String(encrypted);
          }

          public static string Decrypt(string key, string cipherString)
          {
          var keyArray = Convert.FromBase64String(key);
          var cipherText = Convert.FromBase64String(cipherString);

          var decrypted = Decrypt(keyArray, cipherText);

          return Encoding.ASCII.GetString(decrypted);
          }





          share|improve this answer
























          • Yup. I fully admit I am a complete novice at this. I inherited this code and the person that originally wrote it is no longer around. Thanks so much for your explanation and help. It works perfectly now.

            – meyousikmann
            Nov 4 '16 at 1:24











          • @meyousikmann Gotta learn from somewhere. Happy to help.

            – Luke Joshua Park
            Nov 4 '16 at 6:00












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40411878%2fpadding-is-invalid-and-cannot-be-removed-during-decryption%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can't just convert a byte of ciphertext to ASCII. It doesn't work like that. Character encodings are scary beasts and should not be muddled with if you don't understand them. I don't think there is a real person alive that does ;)



          What you should do instead is return your result as base64, which is still a collection of ASCII characters but they are safe to be moved around as a string, and don't result in the loss of any characters.



          See the modified code below:



          public static string Encrypt(string key, string toEncrypt)
          {
          var keyArray = Convert.FromBase64String(key);
          var info = Encoding.ASCII.GetBytes(toEncrypt);

          var encrypted = Encrypt(keyArray, info);

          return Convert.ToBase64String(encrypted);
          }

          public static string Decrypt(string key, string cipherString)
          {
          var keyArray = Convert.FromBase64String(key);
          var cipherText = Convert.FromBase64String(cipherString);

          var decrypted = Decrypt(keyArray, cipherText);

          return Encoding.ASCII.GetString(decrypted);
          }





          share|improve this answer
























          • Yup. I fully admit I am a complete novice at this. I inherited this code and the person that originally wrote it is no longer around. Thanks so much for your explanation and help. It works perfectly now.

            – meyousikmann
            Nov 4 '16 at 1:24











          • @meyousikmann Gotta learn from somewhere. Happy to help.

            – Luke Joshua Park
            Nov 4 '16 at 6:00
















          1














          You can't just convert a byte of ciphertext to ASCII. It doesn't work like that. Character encodings are scary beasts and should not be muddled with if you don't understand them. I don't think there is a real person alive that does ;)



          What you should do instead is return your result as base64, which is still a collection of ASCII characters but they are safe to be moved around as a string, and don't result in the loss of any characters.



          See the modified code below:



          public static string Encrypt(string key, string toEncrypt)
          {
          var keyArray = Convert.FromBase64String(key);
          var info = Encoding.ASCII.GetBytes(toEncrypt);

          var encrypted = Encrypt(keyArray, info);

          return Convert.ToBase64String(encrypted);
          }

          public static string Decrypt(string key, string cipherString)
          {
          var keyArray = Convert.FromBase64String(key);
          var cipherText = Convert.FromBase64String(cipherString);

          var decrypted = Decrypt(keyArray, cipherText);

          return Encoding.ASCII.GetString(decrypted);
          }





          share|improve this answer
























          • Yup. I fully admit I am a complete novice at this. I inherited this code and the person that originally wrote it is no longer around. Thanks so much for your explanation and help. It works perfectly now.

            – meyousikmann
            Nov 4 '16 at 1:24











          • @meyousikmann Gotta learn from somewhere. Happy to help.

            – Luke Joshua Park
            Nov 4 '16 at 6:00














          1












          1








          1







          You can't just convert a byte of ciphertext to ASCII. It doesn't work like that. Character encodings are scary beasts and should not be muddled with if you don't understand them. I don't think there is a real person alive that does ;)



          What you should do instead is return your result as base64, which is still a collection of ASCII characters but they are safe to be moved around as a string, and don't result in the loss of any characters.



          See the modified code below:



          public static string Encrypt(string key, string toEncrypt)
          {
          var keyArray = Convert.FromBase64String(key);
          var info = Encoding.ASCII.GetBytes(toEncrypt);

          var encrypted = Encrypt(keyArray, info);

          return Convert.ToBase64String(encrypted);
          }

          public static string Decrypt(string key, string cipherString)
          {
          var keyArray = Convert.FromBase64String(key);
          var cipherText = Convert.FromBase64String(cipherString);

          var decrypted = Decrypt(keyArray, cipherText);

          return Encoding.ASCII.GetString(decrypted);
          }





          share|improve this answer













          You can't just convert a byte of ciphertext to ASCII. It doesn't work like that. Character encodings are scary beasts and should not be muddled with if you don't understand them. I don't think there is a real person alive that does ;)



          What you should do instead is return your result as base64, which is still a collection of ASCII characters but they are safe to be moved around as a string, and don't result in the loss of any characters.



          See the modified code below:



          public static string Encrypt(string key, string toEncrypt)
          {
          var keyArray = Convert.FromBase64String(key);
          var info = Encoding.ASCII.GetBytes(toEncrypt);

          var encrypted = Encrypt(keyArray, info);

          return Convert.ToBase64String(encrypted);
          }

          public static string Decrypt(string key, string cipherString)
          {
          var keyArray = Convert.FromBase64String(key);
          var cipherText = Convert.FromBase64String(cipherString);

          var decrypted = Decrypt(keyArray, cipherText);

          return Encoding.ASCII.GetString(decrypted);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 3 '16 at 22:57









          Luke Joshua ParkLuke Joshua Park

          5,46951632




          5,46951632













          • Yup. I fully admit I am a complete novice at this. I inherited this code and the person that originally wrote it is no longer around. Thanks so much for your explanation and help. It works perfectly now.

            – meyousikmann
            Nov 4 '16 at 1:24











          • @meyousikmann Gotta learn from somewhere. Happy to help.

            – Luke Joshua Park
            Nov 4 '16 at 6:00



















          • Yup. I fully admit I am a complete novice at this. I inherited this code and the person that originally wrote it is no longer around. Thanks so much for your explanation and help. It works perfectly now.

            – meyousikmann
            Nov 4 '16 at 1:24











          • @meyousikmann Gotta learn from somewhere. Happy to help.

            – Luke Joshua Park
            Nov 4 '16 at 6:00

















          Yup. I fully admit I am a complete novice at this. I inherited this code and the person that originally wrote it is no longer around. Thanks so much for your explanation and help. It works perfectly now.

          – meyousikmann
          Nov 4 '16 at 1:24





          Yup. I fully admit I am a complete novice at this. I inherited this code and the person that originally wrote it is no longer around. Thanks so much for your explanation and help. It works perfectly now.

          – meyousikmann
          Nov 4 '16 at 1:24













          @meyousikmann Gotta learn from somewhere. Happy to help.

          – Luke Joshua Park
          Nov 4 '16 at 6:00





          @meyousikmann Gotta learn from somewhere. Happy to help.

          – Luke Joshua Park
          Nov 4 '16 at 6:00




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40411878%2fpadding-is-invalid-and-cannot-be-removed-during-decryption%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]