XOROutputStream.java

package fr.umlv.ji.io;
import java.io.*;
/**
 * Filtre de chiffrement d'un flot en écriture.
 */
public class XOROutputStream extends FilterOutputStream {
  /** Clef de chiffrement. */
  private int key;
  /** Constructeur de flot. L'octet servant au chiffrement
      est transmis sous forme d'un int dont seuls les huit
      bits de poids faible sont utilisés. */
  public XOROutputStream(OutputStream out, int key) {
    super(out);
    this.key = key;
  }
  /** Chiffre un octet. */
  public void write(int b) throws IOException {
    out.write(b ^ key);
  }
  /** Chiffre un tableau d'octets. */
  public void write(byte b[], int off, int len) throws IOException {
    byte buffer[] = new byte[len-off];
    for (int i=0; i<len; i++)
      buffer[i] = (byte)((b[off+i] & 0xFF) ^ key);
    out.write(buffer,0,buffer.length);
  }
}