XORInputStream.java |
package fr.umlv.ji.io; import java.io.*; /** * Filtre de déchiffrement d'un flot en lecture. */ public class XORInputStream extends FilterInputStream { /** Clef de chiffrement. */ private int key; /** Constructeur de flot. L'octet servant au chiffrement est représenté sous forme d'un int dont seuls les huit bits de poids faible sont utilisés. */ public XORInputStream(InputStream in, int key) { super(in); this.key = key; } /** Déchiffre un octet. */ public int read() throws IOException { int b = in.read(); if (b!=-1) b = (b ^ key) & 0xFF; return b; } /** Déchiffre un tableau d'octets. */ public int read(byte b[], int off, int len) throws IOException { int nb = in.read(b, off, len); for (int i=0; i<nb; i++) { b[off+i] = (byte)((b[off+i] & 0xFF) ^ key); } return nb; } }