CipherInputStream などを使用してバイトを暗号化しているコード サンプルをインターネット上で見つけました。Javaでやってます
2 に答える
This is quite a reasonable question, despite the down-votes.
Many encryption modes (such as CBC) require that the input is a multiple of the block size of the underlying cipher (.e.g. 16 bytes for AES or 8 bytes for DES). To achieve this one uses a padding scheme. Whether you can encrypt a plaintext with an arbitrary bit-length depends on what padding scheme you use.
The most commonly used padding scheme (i.e. PKCS #5 padding) prepends n bytes of value n to the plaintext. This obviously requires that the plaintext is in bytes. But there are lesser known padding schemes that allow arbitrary inputs. For example, the so called "bit padding" appends a single 1 bit to your plaintext and the appends as many 0 bits as necessary until the length of your input is a multiple of the block size of your cipher. For example, the ISO/IEC 9797-1 standard is proposing this.
When deciding, whether you want to support arbitrary bit-lengths in your application you also have to consider the crypto libraries that you want to use. While some crypto algorithms have been standarized to allow inputs of arbitrary sizes in bits, it may not be implemented by your crypto library. E.g. the hash function SHA-1 is well defined for inputs such as the 4-bit string '0101', but you might have some trouble finding a library that can actually compute this hash.
Java には、1 バイト未満のものにアクセスできるプリミティブ型はありません。したがって、Java ではビット単位ではなく、バイト単位で作業する必要があります。
1000 ビット程度のデータを含むファイルがある場合、どうすれば DES を適用できますか?
バイトを使用します。Cipher.doUpdateおよびCipher.doFinalメソッドは、バイトの配列ではなく、バイトを使用します。適切なパディング スキームを使用して、パディングされていないデータを考慮します。