PKCS5Padding in C#

16,631

Solution 1

Im no authority on the matter but a quick google turned this up: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/13a20d89-7d84-4f7d-8f5c-5ae108a7f5cf/

Seems the 7 & 5 padding algs. are the same.

Solution 2

Try using a separate library, such as BouncyCastle.

Share:
16,631

Related videos on Youtube

Jonny
Author by

Jonny

Updated on June 04, 2022

Comments

  • Jonny
    Jonny almost 2 years

    I need to encrypt a string using DESede pkcs5 padding. However C# only provides PKCS7 padding. So how can I achieve this?

  • Maarten Bodewes
    Maarten Bodewes over 12 years
    I think one of the two has only been defined officially for block sizes to 8 bytes, but in general they should be the same. Most libraries will simply pad 090909090909090909 if the plain text is 9 bytes short of the block size, up to 10101010101010101010101010101010 for AES of course, because for CBC you need at least 1 padding byte.
  • rossum
    rossum over 12 years
    @owlstead: You are right. Officially PKCS#5 is for 64 bit blocks and PKCS#7 for 128 bit blocks. In practice the usual solution is to use whatever is available and see if it works. 95% of the time they two will turn out to be the same.
  • Jonny
    Jonny over 12 years
    Yes, I found that post as well. However I'm using an external API to affect some payment and it specifically requires PKCS#5 padding and I'm not sure how the API will behave with PKCS#7 padding so I asked to make sure there's no simple way to encrypt a string using PKCS#5 in C# ensuring the piece of mind that I'm passing the API the right data
  • Peter Kalef ' DidiSoft
    Peter Kalef ' DidiSoft about 11 years
    Yes, check the class Org.BouncyCastle.Crypto.Paddings.Pkcs7Padding and its method AddPadding.