2

我需要加密ISO 8583消息...这里的问题是消息比密钥长。我需要有人帮助我如何加密这个字符串。

例如:我的字符串中有 300 个字符;我应该单独加密每个 16 个字符然后连接它们,因为我的主密钥长度是 16 个字节?

我感谢您的帮助...


ISO 8583-1:2003 金融交易卡发起的消息——交换消息规范——第 1 部分:消息、数据元素和代码值。

4

5 回答 5

7

DES is a block cipher, and block ciphers have different modes of operation.

The mode you mentioned is known as ECB (Electronic Codebook), and is not very secure (actually, neither is DES, but more on that later).

I'd suggest you use CBC or some other mode.

You can read about block cipher modes of operation here: Block cipher modes of operation

As for the cipher itself, I'd suggest you avoid using DES if this is at all possible. DES is extremely easy to crack nowadays. Please use AES, or at least 3DES if AES is not available.

EDIT: In response to the updated question, yes, you would need to pad the last block if the plaintext size is not a multiple of the block size.

于 2009-02-08T13:51:56.323 回答
4

There are many different modes of operation for a block cipher. If you just need to applay ECB to your plain text, just split the plain text into equally sized blocks of size 8 bytes (DES block size) and encrypt each separately.
Depending on what you want to achieve, you could also use

  • ECB which encrypts block wise with each block being independent from all other (previous) blocks. Drawback: known plain text attacks can reveal patterns in your cipher text because the same plain text will always be encrypted to the same cipher text
  • CBC here you have an initialization vector and each blocks depends on all previously encrypted blocks. This is why you need an IV for the first block.
  • CFB this is an interesting one because it allows you to turn your block cipher into a stream cipher, which might be useful if you want to encrypt a video stream or whatever (similarly for OFB which basically generates a key stream)
  • CTS cipher text stealing might be of use if you want to encrypt data but want to avoid padding. A usage example might be to encrypt a blob in your database, which you cannot resize after it has been written to the DB.

There are still many more modes, but these are the most commonly used ones (imho).
As others have pointed out visit Wikipedia for all the details.

Update:
As for the padding, you have different possibilities. I'd recommend to use the ANSI X.923 standard which basically requires you to pad the last buffer with zeroes and append a counter in the last byte which gives you the number of valid bytes in the last block. The same idea is used in ISO10126 but this time padding is done with random bytes. Note that you can avoid padding at all when using CTS.

Maybe ask yourself if it's actually easier to use a crypto library to do the job for you.
If you're using C++ go for Crypto++ (not so straightforward, but consistent c++ style), Java and .NET have built in crypto providers. If you want to use plain C i can recommend libTomCrypt (very easy to use).

于 2009-02-08T14:15:37.213 回答
2

The key length does not impose a limit on the message size. The message can be as long as you want, and your 128-bit key (nonstandard for DES?) will still be good. The DES cipher operates on blocks of bytes, one block at a time. Standard DES uses a 56-bit key (plus 8 parity bits) and 64-bit blocks.

should I encrypt each 16 chars alone then concat them, since my master key length is 16 bytes?

Ciphers in general do not require the key and block sizes to be the same; they can define complicated operations taking a given block of cleartext and transforming it with the key to a block of ciphertext (usually of the same size). When multiple blocks need to be encrypted, a mode of operation is specified to describe how one block relates to the next block in the process.

When operating in the electronic codebook (ECB) mode, the message is divided into blocks, and each block of cleartext is encrypted separately with the same key (the resulting blocks of ciphertext are then concatenated). Like other modes of operation for DES (i.e. CBC, CFB, OFB), this approach has its pros and cons. You will need to pick the mode most suitable for your application.

Btw, you should also be aware that DES is now considered insecure.

于 2009-02-08T13:50:10.493 回答
2

You need to look up encryption modes - which have names such as Cipher Block Chaining (CBC) and the 'do not use' mode Electronic Code Book (ECB), and even some exotic names like the Infinite Garble Extension (IGE). That page has a beautiful illustration of why the ECB mode should not be used.

CBC is a standard, solid mode of operation. OFB and CFB are also widely used.

You realize that the US Federal Government no longer uses plain DES because it is not secure enough (because it uses a 56-bit key and can be broken by brute force)? Triple-DES is just about tolerated - it has a 112-bit or 168-bit key, depending on which way you use it. The standard, though, is Advanced Encryption System, AES. Unless you have backwards compatibility reasons, you should use AES and not DES in new production code.

Also, you should know the answers to these questions before trying to write production code. I trust this is in the nature of homework or personal interest.

于 2009-02-08T13:50:55.873 回答
2

您可能出于以下原因需要加密:

  1. 传输安全数据,即 PIN 块(数据元素 52),当您从收单方密钥转换为发行方密钥时,该数据由 HSM 处理。
  2. 或对消息进行 MAC 处理,然后您将选择一些字段进行散列并附加到消息的末尾(通常是数据元素 124 或 128)
  3. 或者您想存储 ISO 8583 消息并希望遵守 PCI DSS 法规,在这种情况下,您将加密以下数据元素(如果存在)
    • DE 2 - 卡号
    • DE 14 - 到期日
    • DE 35 - 第二轨
    • DE 45 - 轨道 I
    • DE 48 - EMV(芯片)数据(MasterCard TLV)
    • DE 52 - PIN 块
    • DE 55 - EMV(芯片)数据(签证)

还有一件事,您的主密钥应该是 128 位以符合 Visa 的要求(三重 DES 要求 LMK 至少是双倍长度的密钥,即 32 位 - 128 位密钥)

于 2009-07-17T16:18:17.880 回答