1

I am writing a program that sends an audio/video file to the server where the data is encrypted and sent back to the client on a socket. In the client part, the data is extracted and is decrypted and stored into an another file.The data is getting encrypted and decrypted as well,but the decrypted file is not playing properly.

Can anyone help? My code is as follows

Server:

    public class Ser_enc 
    {
    private static int packet_count;
    private static int packet_size=1024;
    public static void main(String args[]) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException
    {
    System.out.println("Hi iam server");
    ServerSocket ss=new ServerSocket(2001);
    Socket s=ss.accept();

    BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));//sockin

    OutputStream pw= s.getOutputStream();


    String filename=in.readLine();
    System.out.println("The file requested is " +filename);

    String loc="F://files//source_files//"+filename;

    File file=new File(loc);

    if(file.exists())
    System.out.println("File found");

    File to_b_encf =new File("F:/files/source_files//encryped.mp3");

    if(!to_b_encf.exists())
    to_b_encf.createNewFile();

    System.out.println("encrypting");

    Cipher encipher = Cipher.getInstance("AES");

    KeyGenerator kgen = KeyGenerator.getInstance("AES");

    SecretKey skey = kgen.generateKey();//initiate key

    encipher.init(Cipher.ENCRYPT_MODE, skey);

    FileInputStream fsrc=new FileInputStream(loc);

    FileOutputStream encfile=new FileOutputStream(to_b_encf);

    CipherInputStream cis = new CipherInputStream(fsrc, encipher);

    int read;
    while((read=cis.read())!=-1)
    {
      encfile.write(read);
      encfile.flush();
    }

     BufferedInputStream fsrcread=new BufferedInputStream(new   FileInputStream(to_b_encf));

    packet_count = (int) Math.ceil((to_b_encf.length()/packet_size));
    System.out.println("The number of packets to send is :" +packet_count);
    for(int i=0;i<=packet_count;i++)
    {
    byte[] packet=new byte[packet_size];

    fsrcread.read(packet, 0, packet_size);

    int per=(int)((i*100)/(packet_count));

    System.out.println("Transfer " +per +"% done");

    pw.write(packet);
    pw.flush();

    }
    s.close();
   pw.close();
   cis.close();
   encfile.close();
   }
   }

Client:

public class Cli_dec 
{
    private static Socket s;
     private static int read;
    public static void main(String args[]) throws UnknownHostException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException
    {

        s=new Socket("127.0.0.1",2001);

        PrintWriter out=new PrintWriter(s.getOutputStream());

        String fname=JOptionPane.showInputDialog(null);

        out.write(fname+"\n");
        out.flush();

        int count;
        byte[] buf=new byte[100000];
        System.out.println("Receiving packets");
        File f=new File("F:/files/source_files//decryped.mp3");
        FileOutputStream to_b_decf=new FileOutputStream(f);
        BufferedOutputStream bos=new BufferedOutputStream(to_b_decf);
        InputStream in1=s.getInputStream();

        while((count=in1.read(buf))>0)
        {
        bos.write(buf, 0,count);
        bos.flush();
        }

        File destfile =new File("F:/files/source_files//original.mp3");

        if(!destfile.exists())
        destfile.createNewFile();

        Cipher decipher = Cipher.getInstance("AES");//initiate a cipher for decryption
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecretKey skey = kgen.generateKey();//initiate key
        decipher.init(Cipher.DECRYPT_MODE, skey);//decrypt the file 

        FileInputStream decf=new FileInputStream(f);

        System.out.println("decrypting");

        CipherInputStream c_decf=new CipherInputStream(decf,decipher);

        FileOutputStream destf=new FileOutputStream(destfile);

        CipherOutputStream cout=new CipherOutputStream(destf,decipher);


        while((read=c_decf.read())!=-1)
        {
         cout.write(read);
         cout.flush();
         }
        c_decf.close();
        destf.close();
        cout.close();
        decf.close();
        s.close();
}

}
Krish
  • 308
  • 3
  • 5
  • 19
  • I think there is a problem while decrypting the data(client). I am not getting what's wrong.Can anyone suggest me please?? – Krish Apr 20 '13 at 10:25
  • Can you please post your solution for your problem ? @Krish – Martin May 13 '17 at 15:33

2 Answers2

1

You're using kgen.generateKey() in the client, which generates a random key - you need to use the same key for the server and client or else you'll get gibberish.

The accepted answer to this question has some good encryption code that you can use - use KeySpec spec = new PBEKeySpec(password, salt, 64, 128); to reduce the iteration count and to use 128-bit encryption instead of 256-bit encryption. This code also uses Cipher Block Chaining instead of Electronic Codebook, which is much more secure (see this Wikipedia article for an explanation of cipher modes). Both the server and client will need to use the same key generation code so that their keys match; in addition to using the same key, the encrypter and decrypter will need to use the same initialization vector (IV), but this isn't secret (can be transmitted in plaintext).

Community
  • 1
  • 1
Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
  • May i know what do you mean by "Salt" which you specified in your answer?? – Krish Apr 21 '13 at 18:10
  • 1
    This is a non-secret piece of data unique to each user that gets appended to the password prior to hashing - this ensures that if two users inadvertently pick the same password their password hashes will be different. – Zim-Zam O'Pootertoot Apr 21 '13 at 18:16
1

i am in middle of developing application that do like you asked, your code helped me well.

in client module you did double decryption... my code looks like this

CipherInputStream c_decf = new CipherInputStream(decf, decipher);

    FileOutputStream destf = new FileOutputStream(destfile);

    //CipherOutputStream cout = new CipherOutputStream(destf, decipher);


    while ((read = c_decf.read()) != -1) {
        destf.write(read);
        destf.flush();
    }
    c_decf.close();
    destf.close();
   // cout.close();
    decf.close();

just remove it and did a key generation as How do i decrypt a file in Android with AES?. code works.

thank you for posting this question.

Community
  • 1
  • 1
Axay Patel
  • 47
  • 6
  • yes but this is hint... my code looks like.. CipherInputStream c_decf = new CipherInputStream(decf, decipher); FileOutputStream destf = new FileOutputStream(destfile); //CipherOutputStream cout = new CipherOutputStream(destf, decipher); while ((read = c_decf.read()) != -1) { destf.write(read); destf.flush(); } c_decf.close(); destf.close(); // cout.close(); decf.close(); – Axay Patel Dec 26 '14 at 06:33