Total members 11895 |It is currently Sun Dec 22, 2024 7:48 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.




Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

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









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com