Sunday, September 15, 2013

How insert bits into block in java cryptography?

How insert bits into block in java cryptography?

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

No comments:

Post a Comment