项目作者: caluml

项目描述 :
Java XOR library
高级语言: Java
项目地址: git://github.com/caluml/libxor.git
创建时间: 2013-11-10T15:46:10Z
项目社区:https://github.com/caluml/libxor

开源协议:GNU General Public License v2.0

下载


libxor

libxor is a Java library to help with XORing functionality.

This can be used to implement a One-time Pad.
A one time pad is only secure if

  • the pad data is perfectly random
  • no-one obtains a copy of the pad
  • you never reuse a pad
  • the pad is securely destroyed after the pad is used.

It is recommended to use the pad truncating versions of the streams.

XoringInputStream and XoringOutputStream.
These take an pad file, and can wrap other Streams (FileInputStream, network socket streams, etc).
When data is written/read from the stream, the content is XORed against the data in the pad file.

To encourage the non-reuse of One Time Pads, there are the PadTruncatingXorInputStream and PadTruncatingXorInputStream.
Using these classes will cause the pad file to be optionally overwritten with random data, and truncated as it is used.
Ideally, this would mean the pad can’t be reused or recovered.
However, if your filesystem uses caching, journalling, or wear-levelling though, (such as most solid state drives these
days) the original pad may still be recoverable.

Enabling the optional overwriting with random data flag makes operations a lot slower.

If you run out of pad data, an InsufficientXorDataRuntimeException is thrown.

Usage

  1. File sourceFile = new File("/etc/passwd");
  2. FileInputStream source = new FileInputStream(sourceFile);
  3.  
  4. Random random = new Random();
  5. byte[] pad = new byte[(int) sourceFile.length()];
  6. random.nextBytes(pad);
  7.  
  8. // Write two copies of pad, as they get deleted after use
  9. FileUtils.writeByteArrayToFile(new File("/tmp/pad1"), pad);
  10. FileUtils.writeByteArrayToFile(new File("/tmp/pad2"), pad);
  11.  
  12. PadTruncatingXorInputStream padTruncatingXorInputStream = new PadTruncatingXorInputStream(source, new File("/tmp/pad1"), 0, true);
  13. FileUtils.copyInputStreamToFile(padTruncatingXorInputStream, new File("/tmp/encrypted"));
  14.  
  15. PadTruncatingXorOutputStream padTruncatingXorOutputStream = new PadTruncatingXorOutputStream(new FileOutputStream("/tmp/decrypted"), new File("/tmp/pad2"), 0, true);
  16. padTruncatingXorOutputStream.write(FileUtils.readFileToByteArray(new File("/tmp/encrypted")));

Example CLI tools for sending files over a network

  1. cd cli
  2. ./PadGenerator.sh /tmp/pad 1024000
  3. ./FileReceiver.sh /tmp/pad 0 /tmp/received 5000
  4. ./FileSender.sh /tmp/pad 0 /file/to/send 127.0.0.1 5000
  5. ./FileXorer.sh /tmp/pad /input/file /output/file