Tuesday, 15 July 2014

performance - Fast modular addition of 2 (potentially large) files in java -



performance - Fast modular addition of 2 (potentially large) files in java -

i doing cryptography experiment 1 time pads.

i have 2 files otp , text (1k-10k) bytes in them. otp big (>1gb). want create 3rd file cyphertext (same size text) performing modular add-on of text otp using offset otp. coded hand using java.io, , works, isn't snappy, buffered io (streams or writers).

i looking way add together 1 of underlying byte buffers other 1 using nio not find (built-in) way that, or filter contents of text using info otp except hand. there way without reinventing wheel? thought utilize selector. ideally i'd able handle files larger 2gb in size both otp , text why looking @ nio.

private static void createotp() { ... system.out.print("generating " + filename + " "); long starttime = system.nanotime(); fileoutputstream fos = new fileoutputstream(f); bufferedoutputstream bos = new bufferedoutputstream(fos, mb); for(long currentsize =0; currentsize < otpsize; currentsize += basize){ new securerandom().nextbytes(ba); bos.write(ba); if(currentsize % (mb * 20l * (long)sizeingb)==0){ system.out.print("."); } } long elapsedtime = system.nanotime() - starttime; system.out.println(" otp generation elapsed time " + (elapsedtime / 1000000.0) + " msec"); fos.close(); bos.close(); ... } private static void symetricencryptiondecryption(boolean encrypt) { ... outtext=new file(intext.getparentfile(), direction + ".otp"); bufferedoutputstream bos = new bufferedoutputstream(new fileoutputstream(outtext), mb); byte[] plaindata = new byte[(int)intext.length()]; datainputstream datais = new datainputstream(new fileinputstream(intext)); datais.readfully(plaindata); datais.close(); bytebuffer bb = bytebuffer.wrap(plaindata); datainputstream in = new datainputstream(new bufferedinputstream(new fileinputstream(otpfile))); in.skip(offset); while(bb.hasremaining()){ bos.write( bb.get() + (encrypt? in.readbyte() : -in.readbyte()) ); } bos.close(); in.close(); system.out.println("offset: " + offset); }

so there far slicker way this:

while(bb.hasremaining()){ bos.write( bb.get() + (encrypt? in.readbyte() : -in.readbyte()) ); }

or generate otp matter.

it's not clear trying if memory map otp file, giving random access, , read/process 8 bytes @ time i.e. long values should eb able write encrypted 10k text file under 100 ms time spend starting jvm.

btw: if have access encrypted text , otp file might decode text without offset i.e. work out using brute force.

java performance cryptography nio

No comments:

Post a Comment