java - Encryption and decryption of a audio/video file using AES(128) -


i writing program sends audio/video file server data encrypted , sent client on socket. in client part, data extracted , decrypted , stored file.the data getting encrypted , decrypted well,but decrypted file not playing properly.

can help? code 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 " +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 send :" +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 cipher decryption         keygenerator kgen = keygenerator.getinstance("aes");         secretkey skey = kgen.generatekey();//initiate key         decipher.init(cipher.decrypt_mode, skey);//decrypt 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(); }  } 

you're using kgen.generatekey() in client, generates random key - need use same key server , client or else you'll gibberish.

the accepted answer question has encryption code can use - use keyspec spec = new pbekeyspec(password, salt, 64, 128); reduce iteration count , use 128-bit encryption instead of 256-bit encryption. code uses cipher block chaining instead of electronic codebook, more secure (see wikipedia article explanation of cipher modes). both server , client need use same key generation code keys match; in addition using same key, encrypter , decrypter need use same initialization vector (iv), isn't secret (can transmitted in plaintext).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -