如何在java加密中将位插入块中?

我正在尝试制作一个涉及加密的简单Java程序。

首先,我从文件clearmsg.txt中读取一个32字节的块。 然后我将此块转换为整数,并将其用于加密。 不幸的是,密文的大小不是静态的; 有时它返回30个字节,有时返回26个字节。 这似乎与添加操作的结果无关。

如何确保它成为32字节的密码块? 如何向这个块添加位/字节? 因为当我尝试解密这个块时,我需要读取32个密文字节。

private void ENC_add() { final File clearmsg = new File("F:/java_projects/clearmsg.txt"); final File ciphermsg = new File("F:/java_projects/ciphermsg.txt"); final byte[] block = new byte[32]; try { final FileInputStream fis = new FileInputStream(clearmsg); final FileOutputStream fcs = new FileOutputStream(ciphermsg); int i; while ((i = fis.read(block)) != -1) { // Is this process true // here M2 (Plain text) shuld be 32 byte M2 = new BigInteger(block); // here encrypt M2 by add k1 where k1 any number less than P CM2 = M2.add(K1).mod(P); // here my problem some time Cipher CM2 length 31 , some time CM2 length 32 ,some time CM2 length 30 System.out.println("THE CM2=" + CM2.toByteArray().Length); fcs.write(CM2.toByteArray(), 0, i); } fcs.close(); } catch (final IOException e) { e.printStackTrace(); } } // Here problem for decrypt private void DEC_ADD() { // DECREPT METHOD final File ciphermsg = new File("F:/java_projects/ciphermsg.txt"); final File clearmsg = new File("F:/java_projects/rciphermsg.txt"); final byte[] block = new byte[32]; try { final FileInputStream fis = new FileInputStream(ciphermsg); final FileOutputStream fos = new FileOutputStream(clearmsg); int i; while ((i = fis.read(block)) != -1) { // CM2 NOT STATIC BITS NUMBER BECAUSE INDEPENDET ON RESULT ADDITIONAL AND PRIME NUMBER P through ENCRYPT // Process CM2 = new BigInteger(block); // here RM2 is decrypt cipher (CM2) NOTE When encrypt above M2 WAS 32 bytes and Cipher CM2 was 30 bytes // and When I read from file 32 bytes then this is my problem RM2 = CM2.subtract(K1).mod(P); fos.write(RM2.toByteArray(), 0, i); } fos.close(); System.out.println("THE RM2=" + CM2.bitLength()); } catch (final IOException e) { e.printStackTrace(); } } 

对于加密,需要一个通常称为整数到Octet String Primitive或I2OSP的函数。 对于解密,您需要一个OS2IP函数来转换回整数。 两者都在这里解释。

这些函数用于编码/解码为给定大小的八位字节串(字节数组)。 此大小通常与RSA加密的模数(在您的情况下为P)的大小直接相关。

I2OSPfunction通常编码如下:

 public static byte[] i2os(final BigInteger i, final int size) { if (i == null || i.signum() == -1) { throw new IllegalArgumentException("Integer should be a positive number or 0"); } if (size < 1) { throw new IllegalArgumentException("Size of the octet string should be at least 1 but is " + size); } final byte[] signed = i.toByteArray(); if (signed.length == size) { return signed; } final byte[] os = new byte[size]; if (signed.length < size) { // copy to rightmost part of os variable (os initialized to 0x00 bytes) System.arraycopy(signed, 0, os, size - signed.length, signed.length); return os; } if (signed.length == size + 1 && signed[0] == 0x00) { // copy to os variable, skipping initial sign byte System.arraycopy(signed, 1, os, 0, size); return os; } throw new IllegalArgumentException("Integer does not fit into an array of size " + size); } 

幸运的是,反向function并不复杂:

 public static BigInteger os2i(final byte[] data, final int size) { if (data.length != size) { throw new IllegalArgumentException("Size of the octet string should be precisely " + size); } return new BigInteger(1, data); } 

我保留了大小validation,因此可以使用预期的八位字节大小调用它,即使计算本身不需要它。