I need to encrypt a string using DESede pkcs5 padding. However C# only provides PKCS7 padding. So how can I achieve this?
Asked
Active
Viewed 1.9k times
2 Answers
13
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.

Myles McDonnell
- 12,943
- 17
- 66
- 116
-
1I 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. – Maarten Bodewes Dec 23 '11 at 11:31
-
@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. – rossum Dec 23 '11 at 12:45
-
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 – Jonny Dec 27 '11 at 16:00
3
Try using a separate library, such as BouncyCastle.

ChrisAnnODell
- 1,341
- 15
- 24
-
Yes, check the class **Org.BouncyCastle.Crypto.Paddings.Pkcs7Padding** and its method _AddPadding_. – Peter Kalef ' DidiSoft Apr 03 '13 at 09:25