8

I've been looking over the docs for the crypto module in Node, and I'm trying to figure out how to set the padding when doing symmetric encryption. I'm trying to use AES-128-ECB, with PKCS5 padding.

I can't see anywhere that it allows you to specify padding. I would certainly hope this is possible to do using this library. How can I specify padding for symmetric encryption in the crypto module?

gevorg
  • 4,835
  • 4
  • 35
  • 52
dsw88
  • 4,400
  • 8
  • 37
  • 50

1 Answers1

7

http://nodejs.org/api/crypto.html#crypto_cipher_setautopadding_auto_padding_true

When you disable auto-padding, you can write any padding you wish by simply writing to cipher (which is a stream).

Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54
  • 4
    Or in other words, if you leave auto-padding enabled, the plaintext will be PKCS#5 padded automatically. – ntoskrnl Sep 24 '13 at 14:00
  • Ok, that's helpful, thanks! How would I write the correct padding to the string? I'm not super good with cryptography, so writing that correctly is difficult to know how to do. – dsw88 Sep 26 '13 at 15:04
  • @ntoskrnl since in iOS the default padding is PKCS#7 and can not be changed, so how to set padding to PKCS#7 in node.js? – Mil0R3 Nov 20 '13 at 03:08
  • @Veelian Yeah I did, but that was to use PKCS5 padding in Node, which is what I needed. I'm not sure how to set it to PKCS7. – dsw88 Nov 20 '13 at 16:55
  • 5
    @dsw88 PKCS#5 padding is the same as PKCS#7 padding. – ntoskrnl Nov 20 '13 at 17:55
  • 1
    Ah I didn't know that, thanks! I'm not super familiar with padding schemes, or other encryption details very much for that matter. – dsw88 Nov 20 '13 at 17:57
  • for block-size 128-bit, there are no difference between PKCS#5 and PKCS#7. – Aborn Jiang Jun 08 '16 at 10:37