TripleDES encrypting and decrypting gives strange results












1















I have a working implementation of TripleDESCng (tested against some test vectors), but the following happens:



When I encrypt plain text This is a sample message (24 bytes, thus for this it would be 3 blocks) (hex for it is 5468697320697320612073616D706C65206D657373616765) with an example key, I get E81F113DD7C5D965E082F3D42EC1E2CA39BCDBCCBC0A2BD9. However, when I decrypt this with the same example key, I get 5468697320697320612073616D706C650000000000000000, which, when converted back to ASCII, is:



This is a sample.



Any reason other than my code why this would behave this way? To encrypt and decrypt, I use 24 byte keys (ECB mode).



EDIT:



using (var tripleDES = new TripleDESCryptoServiceProvider())
{
byte data = ASCIIEncoding.ASCII.GetBytes("This is a sample message");
Console.WriteLine(BitConverter.ToString(data));
tripleDES.IV = new byte[tripleDES.BlockSize / 8];
var encryptor = tripleDES.CreateEncryptor();
byte result = new byte[data.Length];
encryptor.TransformBlock(data, 0, data.Length, result, 0);
var decryptor = tripleDES.CreateDecryptor();
byte result2 = new byte[result.Length];
decryptor.TransformBlock(result, 0, result.Length, result2, 0);
Console.WriteLine(BitConverter.ToString(result2));
}
Console.ReadLine();









share|improve this question

























  • The default mode is CBC, not EBC. Since you don't explicitly set the Mode, why do you think this is EBC? Plus it's 2018. Outside of historic interest, why are you writing new code that uses outdated ciphers and outdated modes?

    – Damien_The_Unbeliever
    Jul 31 '18 at 12:24













  • @Damien_The_Unbeliever the mode is not a problem, IV is set to 0s either way

    – karolyzz
    Jul 31 '18 at 12:28
















1















I have a working implementation of TripleDESCng (tested against some test vectors), but the following happens:



When I encrypt plain text This is a sample message (24 bytes, thus for this it would be 3 blocks) (hex for it is 5468697320697320612073616D706C65206D657373616765) with an example key, I get E81F113DD7C5D965E082F3D42EC1E2CA39BCDBCCBC0A2BD9. However, when I decrypt this with the same example key, I get 5468697320697320612073616D706C650000000000000000, which, when converted back to ASCII, is:



This is a sample.



Any reason other than my code why this would behave this way? To encrypt and decrypt, I use 24 byte keys (ECB mode).



EDIT:



using (var tripleDES = new TripleDESCryptoServiceProvider())
{
byte data = ASCIIEncoding.ASCII.GetBytes("This is a sample message");
Console.WriteLine(BitConverter.ToString(data));
tripleDES.IV = new byte[tripleDES.BlockSize / 8];
var encryptor = tripleDES.CreateEncryptor();
byte result = new byte[data.Length];
encryptor.TransformBlock(data, 0, data.Length, result, 0);
var decryptor = tripleDES.CreateDecryptor();
byte result2 = new byte[result.Length];
decryptor.TransformBlock(result, 0, result.Length, result2, 0);
Console.WriteLine(BitConverter.ToString(result2));
}
Console.ReadLine();









share|improve this question

























  • The default mode is CBC, not EBC. Since you don't explicitly set the Mode, why do you think this is EBC? Plus it's 2018. Outside of historic interest, why are you writing new code that uses outdated ciphers and outdated modes?

    – Damien_The_Unbeliever
    Jul 31 '18 at 12:24













  • @Damien_The_Unbeliever the mode is not a problem, IV is set to 0s either way

    – karolyzz
    Jul 31 '18 at 12:28














1












1








1








I have a working implementation of TripleDESCng (tested against some test vectors), but the following happens:



When I encrypt plain text This is a sample message (24 bytes, thus for this it would be 3 blocks) (hex for it is 5468697320697320612073616D706C65206D657373616765) with an example key, I get E81F113DD7C5D965E082F3D42EC1E2CA39BCDBCCBC0A2BD9. However, when I decrypt this with the same example key, I get 5468697320697320612073616D706C650000000000000000, which, when converted back to ASCII, is:



This is a sample.



Any reason other than my code why this would behave this way? To encrypt and decrypt, I use 24 byte keys (ECB mode).



EDIT:



using (var tripleDES = new TripleDESCryptoServiceProvider())
{
byte data = ASCIIEncoding.ASCII.GetBytes("This is a sample message");
Console.WriteLine(BitConverter.ToString(data));
tripleDES.IV = new byte[tripleDES.BlockSize / 8];
var encryptor = tripleDES.CreateEncryptor();
byte result = new byte[data.Length];
encryptor.TransformBlock(data, 0, data.Length, result, 0);
var decryptor = tripleDES.CreateDecryptor();
byte result2 = new byte[result.Length];
decryptor.TransformBlock(result, 0, result.Length, result2, 0);
Console.WriteLine(BitConverter.ToString(result2));
}
Console.ReadLine();









share|improve this question
















I have a working implementation of TripleDESCng (tested against some test vectors), but the following happens:



When I encrypt plain text This is a sample message (24 bytes, thus for this it would be 3 blocks) (hex for it is 5468697320697320612073616D706C65206D657373616765) with an example key, I get E81F113DD7C5D965E082F3D42EC1E2CA39BCDBCCBC0A2BD9. However, when I decrypt this with the same example key, I get 5468697320697320612073616D706C650000000000000000, which, when converted back to ASCII, is:



This is a sample.



Any reason other than my code why this would behave this way? To encrypt and decrypt, I use 24 byte keys (ECB mode).



EDIT:



using (var tripleDES = new TripleDESCryptoServiceProvider())
{
byte data = ASCIIEncoding.ASCII.GetBytes("This is a sample message");
Console.WriteLine(BitConverter.ToString(data));
tripleDES.IV = new byte[tripleDES.BlockSize / 8];
var encryptor = tripleDES.CreateEncryptor();
byte result = new byte[data.Length];
encryptor.TransformBlock(data, 0, data.Length, result, 0);
var decryptor = tripleDES.CreateDecryptor();
byte result2 = new byte[result.Length];
decryptor.TransformBlock(result, 0, result.Length, result2, 0);
Console.WriteLine(BitConverter.ToString(result2));
}
Console.ReadLine();






c# .net cryptography






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 31 '18 at 12:23







karolyzz

















asked Jul 31 '18 at 12:04









karolyzzkarolyzz

13815




13815













  • The default mode is CBC, not EBC. Since you don't explicitly set the Mode, why do you think this is EBC? Plus it's 2018. Outside of historic interest, why are you writing new code that uses outdated ciphers and outdated modes?

    – Damien_The_Unbeliever
    Jul 31 '18 at 12:24













  • @Damien_The_Unbeliever the mode is not a problem, IV is set to 0s either way

    – karolyzz
    Jul 31 '18 at 12:28



















  • The default mode is CBC, not EBC. Since you don't explicitly set the Mode, why do you think this is EBC? Plus it's 2018. Outside of historic interest, why are you writing new code that uses outdated ciphers and outdated modes?

    – Damien_The_Unbeliever
    Jul 31 '18 at 12:24













  • @Damien_The_Unbeliever the mode is not a problem, IV is set to 0s either way

    – karolyzz
    Jul 31 '18 at 12:28

















The default mode is CBC, not EBC. Since you don't explicitly set the Mode, why do you think this is EBC? Plus it's 2018. Outside of historic interest, why are you writing new code that uses outdated ciphers and outdated modes?

– Damien_The_Unbeliever
Jul 31 '18 at 12:24







The default mode is CBC, not EBC. Since you don't explicitly set the Mode, why do you think this is EBC? Plus it's 2018. Outside of historic interest, why are you writing new code that uses outdated ciphers and outdated modes?

– Damien_The_Unbeliever
Jul 31 '18 at 12:24















@Damien_The_Unbeliever the mode is not a problem, IV is set to 0s either way

– karolyzz
Jul 31 '18 at 12:28





@Damien_The_Unbeliever the mode is not a problem, IV is set to 0s either way

– karolyzz
Jul 31 '18 at 12:28












2 Answers
2






active

oldest

votes


















2














With almost all modes1, you should make sure that the final part of your data is pushed through TransformFinalBlock rather than TransformBlock2, to make sure it knows that no more data is coming and to ensure final blocks are flushed/written.



It's bad form, in general, to assume the output size is going to match the input size.




the mode is not a problem, IV is set to 0s either way




Yes, that'll mean that the first block was not affected by your choice of Mode. But all subsequent blocks will be, because they will use the chaining mode and the previous block, not the IV. So if you want ECB (you shouldn't3) you need to explicitly set that mode.





1Your code is using CBC, not EBC as you claim in your narrative. CBC is the default mode for .NET encryption classes.



2And when using this second method, pay attention to it's return value, as mjwills commented.



3You've picked an outdated crypto algorithm, paired it with an outdated mode of operation, and your words I've quoted above mean that you don't understand modes. Added together, I would suggest that you're not well placed to be writing code that uses crypto currently. The .NET classes can make it seem easy to write crypto code but you still have to understand how to make good choices in using them. Better to spend more time on researching these things before writing code.






share|improve this answer


























  • I was well aware the default mode is CBC. I'm sorry, but your suggestion seems to be based on vague assumptions about my knowledge and the choice of algorithm, which, by the way, I purposefully ignored, since I have my own reasons (don't worry, this code is not supposed to reach production)

    – karolyzz
    Jul 31 '18 at 12:56











  • the actual problem was with the final block indeed, I agree I should've studied it a bit more carefully, thanks

    – karolyzz
    Jul 31 '18 at 12:59



















1














I think that your problem is in the method of the encryptor / decryptor that you are using: the TransformBlock method is conceived to transform a block when you will be encrypting multiple blocks.



That is not the case in your code, where you want to transform a single block, and therefore you should be using the TransformFinalBlock method instead. BTW I took the liberty of making your sample buildable.



using System;
using System.Text;

namespace Tests
{
class Program
{
static void Main(string args)
{
System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
byte data = Encoding.UTF8.GetBytes("This is a sample message");
byte key = Encoding.UTF8.GetBytes("NOSTROMOHASSOMEGODPOWERS");
tripleDES.Key = key;
tripleDES.IV = new byte[tripleDES.BlockSize / 8];
var encryptor = tripleDES.CreateEncryptor();
byte result = new byte[data.Length];
result = encryptor.TransformFinalBlock(data, 0, data.Length);
string res = BitConverter.ToString(result).Replace("-","");
Console.WriteLine(BitConverter.ToString(result).Replace("-",""));

byte data2 = result;
tripleDES.Key = key;
tripleDES.IV = new byte[tripleDES.BlockSize / 8];
var decryptor = tripleDES.CreateDecryptor();
byte result2 = new byte[data2.Length];
result2 = decryptor.TransformFinalBlock(data2, 0, data2.Length);
Console.WriteLine(Encoding.UTF8.GetString(result2));
}
}
}





share|improve this answer























    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%2f51612996%2ftripledes-encrypting-and-decrypting-gives-strange-results%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    With almost all modes1, you should make sure that the final part of your data is pushed through TransformFinalBlock rather than TransformBlock2, to make sure it knows that no more data is coming and to ensure final blocks are flushed/written.



    It's bad form, in general, to assume the output size is going to match the input size.




    the mode is not a problem, IV is set to 0s either way




    Yes, that'll mean that the first block was not affected by your choice of Mode. But all subsequent blocks will be, because they will use the chaining mode and the previous block, not the IV. So if you want ECB (you shouldn't3) you need to explicitly set that mode.





    1Your code is using CBC, not EBC as you claim in your narrative. CBC is the default mode for .NET encryption classes.



    2And when using this second method, pay attention to it's return value, as mjwills commented.



    3You've picked an outdated crypto algorithm, paired it with an outdated mode of operation, and your words I've quoted above mean that you don't understand modes. Added together, I would suggest that you're not well placed to be writing code that uses crypto currently. The .NET classes can make it seem easy to write crypto code but you still have to understand how to make good choices in using them. Better to spend more time on researching these things before writing code.






    share|improve this answer


























    • I was well aware the default mode is CBC. I'm sorry, but your suggestion seems to be based on vague assumptions about my knowledge and the choice of algorithm, which, by the way, I purposefully ignored, since I have my own reasons (don't worry, this code is not supposed to reach production)

      – karolyzz
      Jul 31 '18 at 12:56











    • the actual problem was with the final block indeed, I agree I should've studied it a bit more carefully, thanks

      – karolyzz
      Jul 31 '18 at 12:59
















    2














    With almost all modes1, you should make sure that the final part of your data is pushed through TransformFinalBlock rather than TransformBlock2, to make sure it knows that no more data is coming and to ensure final blocks are flushed/written.



    It's bad form, in general, to assume the output size is going to match the input size.




    the mode is not a problem, IV is set to 0s either way




    Yes, that'll mean that the first block was not affected by your choice of Mode. But all subsequent blocks will be, because they will use the chaining mode and the previous block, not the IV. So if you want ECB (you shouldn't3) you need to explicitly set that mode.





    1Your code is using CBC, not EBC as you claim in your narrative. CBC is the default mode for .NET encryption classes.



    2And when using this second method, pay attention to it's return value, as mjwills commented.



    3You've picked an outdated crypto algorithm, paired it with an outdated mode of operation, and your words I've quoted above mean that you don't understand modes. Added together, I would suggest that you're not well placed to be writing code that uses crypto currently. The .NET classes can make it seem easy to write crypto code but you still have to understand how to make good choices in using them. Better to spend more time on researching these things before writing code.






    share|improve this answer


























    • I was well aware the default mode is CBC. I'm sorry, but your suggestion seems to be based on vague assumptions about my knowledge and the choice of algorithm, which, by the way, I purposefully ignored, since I have my own reasons (don't worry, this code is not supposed to reach production)

      – karolyzz
      Jul 31 '18 at 12:56











    • the actual problem was with the final block indeed, I agree I should've studied it a bit more carefully, thanks

      – karolyzz
      Jul 31 '18 at 12:59














    2












    2








    2







    With almost all modes1, you should make sure that the final part of your data is pushed through TransformFinalBlock rather than TransformBlock2, to make sure it knows that no more data is coming and to ensure final blocks are flushed/written.



    It's bad form, in general, to assume the output size is going to match the input size.




    the mode is not a problem, IV is set to 0s either way




    Yes, that'll mean that the first block was not affected by your choice of Mode. But all subsequent blocks will be, because they will use the chaining mode and the previous block, not the IV. So if you want ECB (you shouldn't3) you need to explicitly set that mode.





    1Your code is using CBC, not EBC as you claim in your narrative. CBC is the default mode for .NET encryption classes.



    2And when using this second method, pay attention to it's return value, as mjwills commented.



    3You've picked an outdated crypto algorithm, paired it with an outdated mode of operation, and your words I've quoted above mean that you don't understand modes. Added together, I would suggest that you're not well placed to be writing code that uses crypto currently. The .NET classes can make it seem easy to write crypto code but you still have to understand how to make good choices in using them. Better to spend more time on researching these things before writing code.






    share|improve this answer















    With almost all modes1, you should make sure that the final part of your data is pushed through TransformFinalBlock rather than TransformBlock2, to make sure it knows that no more data is coming and to ensure final blocks are flushed/written.



    It's bad form, in general, to assume the output size is going to match the input size.




    the mode is not a problem, IV is set to 0s either way




    Yes, that'll mean that the first block was not affected by your choice of Mode. But all subsequent blocks will be, because they will use the chaining mode and the previous block, not the IV. So if you want ECB (you shouldn't3) you need to explicitly set that mode.





    1Your code is using CBC, not EBC as you claim in your narrative. CBC is the default mode for .NET encryption classes.



    2And when using this second method, pay attention to it's return value, as mjwills commented.



    3You've picked an outdated crypto algorithm, paired it with an outdated mode of operation, and your words I've quoted above mean that you don't understand modes. Added together, I would suggest that you're not well placed to be writing code that uses crypto currently. The .NET classes can make it seem easy to write crypto code but you still have to understand how to make good choices in using them. Better to spend more time on researching these things before writing code.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 31 '18 at 12:40

























    answered Jul 31 '18 at 12:30









    Damien_The_UnbelieverDamien_The_Unbeliever

    194k17248335




    194k17248335













    • I was well aware the default mode is CBC. I'm sorry, but your suggestion seems to be based on vague assumptions about my knowledge and the choice of algorithm, which, by the way, I purposefully ignored, since I have my own reasons (don't worry, this code is not supposed to reach production)

      – karolyzz
      Jul 31 '18 at 12:56











    • the actual problem was with the final block indeed, I agree I should've studied it a bit more carefully, thanks

      – karolyzz
      Jul 31 '18 at 12:59



















    • I was well aware the default mode is CBC. I'm sorry, but your suggestion seems to be based on vague assumptions about my knowledge and the choice of algorithm, which, by the way, I purposefully ignored, since I have my own reasons (don't worry, this code is not supposed to reach production)

      – karolyzz
      Jul 31 '18 at 12:56











    • the actual problem was with the final block indeed, I agree I should've studied it a bit more carefully, thanks

      – karolyzz
      Jul 31 '18 at 12:59

















    I was well aware the default mode is CBC. I'm sorry, but your suggestion seems to be based on vague assumptions about my knowledge and the choice of algorithm, which, by the way, I purposefully ignored, since I have my own reasons (don't worry, this code is not supposed to reach production)

    – karolyzz
    Jul 31 '18 at 12:56





    I was well aware the default mode is CBC. I'm sorry, but your suggestion seems to be based on vague assumptions about my knowledge and the choice of algorithm, which, by the way, I purposefully ignored, since I have my own reasons (don't worry, this code is not supposed to reach production)

    – karolyzz
    Jul 31 '18 at 12:56













    the actual problem was with the final block indeed, I agree I should've studied it a bit more carefully, thanks

    – karolyzz
    Jul 31 '18 at 12:59





    the actual problem was with the final block indeed, I agree I should've studied it a bit more carefully, thanks

    – karolyzz
    Jul 31 '18 at 12:59













    1














    I think that your problem is in the method of the encryptor / decryptor that you are using: the TransformBlock method is conceived to transform a block when you will be encrypting multiple blocks.



    That is not the case in your code, where you want to transform a single block, and therefore you should be using the TransformFinalBlock method instead. BTW I took the liberty of making your sample buildable.



    using System;
    using System.Text;

    namespace Tests
    {
    class Program
    {
    static void Main(string args)
    {
    System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
    byte data = Encoding.UTF8.GetBytes("This is a sample message");
    byte key = Encoding.UTF8.GetBytes("NOSTROMOHASSOMEGODPOWERS");
    tripleDES.Key = key;
    tripleDES.IV = new byte[tripleDES.BlockSize / 8];
    var encryptor = tripleDES.CreateEncryptor();
    byte result = new byte[data.Length];
    result = encryptor.TransformFinalBlock(data, 0, data.Length);
    string res = BitConverter.ToString(result).Replace("-","");
    Console.WriteLine(BitConverter.ToString(result).Replace("-",""));

    byte data2 = result;
    tripleDES.Key = key;
    tripleDES.IV = new byte[tripleDES.BlockSize / 8];
    var decryptor = tripleDES.CreateDecryptor();
    byte result2 = new byte[data2.Length];
    result2 = decryptor.TransformFinalBlock(data2, 0, data2.Length);
    Console.WriteLine(Encoding.UTF8.GetString(result2));
    }
    }
    }





    share|improve this answer




























      1














      I think that your problem is in the method of the encryptor / decryptor that you are using: the TransformBlock method is conceived to transform a block when you will be encrypting multiple blocks.



      That is not the case in your code, where you want to transform a single block, and therefore you should be using the TransformFinalBlock method instead. BTW I took the liberty of making your sample buildable.



      using System;
      using System.Text;

      namespace Tests
      {
      class Program
      {
      static void Main(string args)
      {
      System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
      byte data = Encoding.UTF8.GetBytes("This is a sample message");
      byte key = Encoding.UTF8.GetBytes("NOSTROMOHASSOMEGODPOWERS");
      tripleDES.Key = key;
      tripleDES.IV = new byte[tripleDES.BlockSize / 8];
      var encryptor = tripleDES.CreateEncryptor();
      byte result = new byte[data.Length];
      result = encryptor.TransformFinalBlock(data, 0, data.Length);
      string res = BitConverter.ToString(result).Replace("-","");
      Console.WriteLine(BitConverter.ToString(result).Replace("-",""));

      byte data2 = result;
      tripleDES.Key = key;
      tripleDES.IV = new byte[tripleDES.BlockSize / 8];
      var decryptor = tripleDES.CreateDecryptor();
      byte result2 = new byte[data2.Length];
      result2 = decryptor.TransformFinalBlock(data2, 0, data2.Length);
      Console.WriteLine(Encoding.UTF8.GetString(result2));
      }
      }
      }





      share|improve this answer


























        1












        1








        1







        I think that your problem is in the method of the encryptor / decryptor that you are using: the TransformBlock method is conceived to transform a block when you will be encrypting multiple blocks.



        That is not the case in your code, where you want to transform a single block, and therefore you should be using the TransformFinalBlock method instead. BTW I took the liberty of making your sample buildable.



        using System;
        using System.Text;

        namespace Tests
        {
        class Program
        {
        static void Main(string args)
        {
        System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
        byte data = Encoding.UTF8.GetBytes("This is a sample message");
        byte key = Encoding.UTF8.GetBytes("NOSTROMOHASSOMEGODPOWERS");
        tripleDES.Key = key;
        tripleDES.IV = new byte[tripleDES.BlockSize / 8];
        var encryptor = tripleDES.CreateEncryptor();
        byte result = new byte[data.Length];
        result = encryptor.TransformFinalBlock(data, 0, data.Length);
        string res = BitConverter.ToString(result).Replace("-","");
        Console.WriteLine(BitConverter.ToString(result).Replace("-",""));

        byte data2 = result;
        tripleDES.Key = key;
        tripleDES.IV = new byte[tripleDES.BlockSize / 8];
        var decryptor = tripleDES.CreateDecryptor();
        byte result2 = new byte[data2.Length];
        result2 = decryptor.TransformFinalBlock(data2, 0, data2.Length);
        Console.WriteLine(Encoding.UTF8.GetString(result2));
        }
        }
        }





        share|improve this answer













        I think that your problem is in the method of the encryptor / decryptor that you are using: the TransformBlock method is conceived to transform a block when you will be encrypting multiple blocks.



        That is not the case in your code, where you want to transform a single block, and therefore you should be using the TransformFinalBlock method instead. BTW I took the liberty of making your sample buildable.



        using System;
        using System.Text;

        namespace Tests
        {
        class Program
        {
        static void Main(string args)
        {
        System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
        byte data = Encoding.UTF8.GetBytes("This is a sample message");
        byte key = Encoding.UTF8.GetBytes("NOSTROMOHASSOMEGODPOWERS");
        tripleDES.Key = key;
        tripleDES.IV = new byte[tripleDES.BlockSize / 8];
        var encryptor = tripleDES.CreateEncryptor();
        byte result = new byte[data.Length];
        result = encryptor.TransformFinalBlock(data, 0, data.Length);
        string res = BitConverter.ToString(result).Replace("-","");
        Console.WriteLine(BitConverter.ToString(result).Replace("-",""));

        byte data2 = result;
        tripleDES.Key = key;
        tripleDES.IV = new byte[tripleDES.BlockSize / 8];
        var decryptor = tripleDES.CreateDecryptor();
        byte result2 = new byte[data2.Length];
        result2 = decryptor.TransformFinalBlock(data2, 0, data2.Length);
        Console.WriteLine(Encoding.UTF8.GetString(result2));
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 31 '18 at 12:49









        xtoikxtoik

        65738




        65738






























            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%2f51612996%2ftripledes-encrypting-and-decrypting-gives-strange-results%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith