Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Data missing when implementing 3DES using DES

Sun Mar 28, 2010 3:16 pm

Here is the code:
Code:

import java
.io.*;
import java.security.*;
import javax.crypto.*;

public class 
test {

    public static 
void main(String[] argsthrows Exception
    
{
        
KeyGenerator keyg KeyGenerator.getInstance("DES");
    
SecureRandom random SecureRandom.getInstance("SHA1PRNG");
    
keyg.init(56random);

    
SecretKey sk1 keyg.generateKey();
        
SecretKey sk2 keyg.generateKey();

        
Cipher cp1 Cipher.getInstance("DES");
    
cp1.init(Cipher.ENCRYPT_MODEsk1);

        
Cipher cp2 Cipher.getInstance("DES");
        
cp2.init(Cipher.DECRYPT_MODEsk2);

        
Cipher cp3 Cipher.getInstance("DES");
        
cp3.init(Cipher.ENCRYPT_MODEsk1);

        
FileInputStream fis = new FileInputStream("input.txt");
        
FileOutputStream fos = new FileOutputStream("encrypted.txt");

        
CipherOutputStream cos1 = new CipherOutputStream(foscp1);
        
CipherOutputStream cos2 = new CipherOutputStream(cos1cp2);
        
CipherOutputStream cos3 = new CipherOutputStream(cos2cp3);

        
byte b[] = new byte[8];
        
int i fis.read(b);
        while(
!= -1){
            
cos3.write(b0i);
            
fis.read(b);
        }

        
fis.close();
        
cos3.close();

        
//Decrypt contents from file
        
FileInputStream fiss = new FileInputStream("encrypted.txt");
        
FileOutputStream foss = new FileOutputStream("original.txt");
        
cp1.init(Cipher.DECRYPT_MODEsk1);
        
cp2.init(Cipher.ENCRYPT_MODEsk2);
        
cp3.init(Cipher.DECRYPT_MODEsk1);

        
CipherInputStream cis1 = new CipherInputStream(fisscp1);
        
CipherInputStream cis2 = new CipherInputStream(cis1cp2);
        
CipherInputStream cis3 = new CipherInputStream(cis2cp3);

        
cis3.read(b);
        while(
!= -1){
            
foss.write(b0i);
            
cis3.read(b);
        }

        
cis3.close();
        
foss.close();
    }
}
 

And this is the input.txt file:
This is the content of the message. Let's see what happen at the end

When I run this code, the original file is:
This is the content of the message. Let's see what happen at the

-> So the "end" word is missing. I don't know what has happened.

But when the input.txt file is:
This is the content of the message. Let's see what happen at the

Then the original.txt file is:
This is the content of the message. Let's see what happen at the

This time it works.

Can anyone show me why this happened ?? Thanks a lot.



Post a reply
  Related Posts  to : Data missing when implementing 3DES using DES
 Missing file in Bmiles     -  
 When to choose C++ for implementing your project and Why     -  
 JButtons implementing an Action     -  
 Implementing synchronization in C++ with threads     -  
 Implementing fibonacci sequence problem using iterations     -  
 Data set for ID3 algorithm     -  
 What is Data integration?     -  
 c++/data structure     -  
 Delete data from database by ID in php     -