如何在java加密中将位插入块? [英] How insert bits into block in java cryptography?

查看:125
本文介绍了如何在java加密中将位插入块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用java程序进行简单的程序加密。我的问题当读取块32个字节从文件clearmsg.txt之后转换此块到intrger数后使用此数字进行加密处理结果大小的密文不静态有时30个字节有时26个字节独立的添加过程的结果喜欢它显示我想想如何成为密码块32字节。如何添加位到这个块,因为当我尝试解密此块我需要读取32字节解密块作为以下我的尝试

  private void ENC_add()
{
文件clearmsg =新文件(F:/java_projects/clearmsg.txt);
文件ciphermsg =新文件(F:/java_projects/ciphermsg.txt);
byte [] block = new byte [32];
try {
FileInputStream fis = new FileInputStream(clearmsg);
FileOutputStream fcs = new FileOutputStream(ciphermsg);
int i;
while((i = fis.read(block))!= -1){
//这个过程是真的
//这里M2(纯文本)应该是32字节
M2 = new BigInteger(block);
//这里通过添加k1加密M2其中k1小于P
的任何数字CM2 = M2.add(K1).mod(P);
//这里我的问题一些时间密码CM2长度31,一些时间CM2长度32,一些时间CM2长度30
System.out.println(THE CM2 =+ CM2.toByteArray()。Length );
fcs.write(CM2.toByteArray(),0,i);
}
fcs.close();

}

catch(IOException e){
e.printStackTrace();
}}
//解密的问题
private void DEC_ADD()
{
// DECREPT METHOD
文件ciphermsg =新文件:/java_projects/ciphermsg.txt);
文件clearmsg =新文件(F:/java_projects/rciphermsg.txt);
byte [] block = new byte [32];
try {
FileInputStream fis = new FileInputStream(ciphermsg);
FileOutputStream fos = new FileOutputStream(clearmsg);
int i;
while((i = fis.read(block))!= -1){
// CM2非静态位数字通过ENCRYPT过程结果附加和PRIME数字P引起的独立性
CM2 = new BigInteger(block);
//这里RM2是解密密码(CM2)注意当在M2 WAS 32字节以上加密并且Cipher CM2是30字节加密时,当我从文件32字节读取时,这是我的问题
RM2 = CM2.subtract (K1).mod(P);
}
fos.write(RM2.toByteArray(),0,i);
}
fos.close();
System.out.println(THE RM2 =+ CM2.bitLength());
}
catch(IOException e){
e.printStackTrace();
}

}

小于32字节

解决方案

对于加密需要一个函数,通常称为整数到八位字节串或I2OS。对于解密,您需要一个OS2I函数来转换回整数。



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



I2OS函数通常编码如下:

  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(八位字节字符串的大小应该至少为1,但是大小+
}

final byte [] signed = i.toByteArray();
if(signed.length == size){
return signed;
}

final byte [] os = new byte [size];
if(signed.length //复制到os变量的最右边部分(os初始化为0x00字节)
System.arraycopy(signed,0,os,size - signed.length,signed.length);
return os;
}

if(signed.length == size + 1&&signed [0] == 0x00){
//复制到os变量,跳过初始符号byte
System.arraycopy(signed,1,os,0,size);
return os;
}

throw new IllegalArgumentException(Integer does not fit into a array of size+ size);幸运的是,反向函数不复杂:











$ b

  public static BigInteger os2i(final byte [] data){
return new BigInteger(1,data);
}


I try make simple program cryptography using java program. my problem when read block 32 bytes from file clearmsg.txt after convert this block to intrger number after that use this number for encrypt process the result size of cipher text not static sometimes 30 bytes sometimes 26 bytes independet on result of add process like it showing I thinking about how become cipher block 32 bytes. How add bits to this block because when I try decrypt this block I need read 32 bytes to decrypt block as the following my try

 private void ENC_add() 
   {    
           File clearmsg = new File("F:/java_projects/clearmsg.txt");
           File ciphermsg = new File("F:/java_projects/ciphermsg.txt");
     byte[] block = new byte[32];
        try {
              FileInputStream fis = new FileInputStream(clearmsg);
              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 (IOException e) {
         e.printStackTrace();
        }  }
//Here problem for decrypt 
private void DEC_ADD()
{
//DECREPT METHOD
 File ciphermsg = new File("F:/java_projects/ciphermsg.txt");
 File  clearmsg = new File("F:/java_projects/rciphermsg.txt");
        byte[] block = new byte[32];
        try {
              FileInputStream fis = new FileInputStream(ciphermsg);
              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 (IOException e) {
            e.printStackTrace();
        }

 }

How can insert bits into each block less than 32 bytes???

解决方案

For the encrypt a function is required that is normally called Integer to Octet String or I2OS. For decryption you need an OS2I function to convert back to integers.

These functions are used to encode/decode to an octet string (byte array) of a given size. This size is normally directly related to the size of the modulus (P in your case) of RSA encryption.

The I2OS function is normally coded like this:

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);
}

Fortunately, the reverse function is not as complicated:

public static BigInteger os2i(final byte[] data) {
    return new BigInteger(1, data); 
}

这篇关于如何在java加密中将位插入块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆