System.out.println ("File:"); String s = new Scanner (system.in). nextline (); File File = new file (s); if (! File.isfile ()) {System.out.println ("not file"); return;} System.out.print ("key:"); int KEY = new Scanner (system.in). Nextint (); try {Encrypt (file, key); System.out.println ("Done");} catch (Exception e) {System.out.println ("failed");}} private static void Encrypt (file file, int key) throws Exception{randomaccessfile RAF = New Randomaccessfile (file, "RW ");//single-byte read standard format//int b;//while ((b = Raf.read ())! =-1) {//b ^= key; b = B^key;//raf.seek (Raf.getfilepointer ()-1); Raf.write (b);//}//8k 8192byte[] buff = new Byte[8192];int n;//Save batch number while ((n = raf.read (buff))! =-1) {//Array first n bytes encrypted for (int i=0;i<n;i++) {buff[i] ^= key;} The subscript moves back to n positions Raf.seek (Raf.getfilepointer ()-n);//output array in the first n bytes raf.write (buff,0,n);} Raf.close ();}
Java file encryption