Debian Codegraph @

src/main/java/org/freehep/util/io/EEXECEncryption.java

raw
1 // Copyright 2001 freehep
2 package org.freehep.util.io;
3
4 //import java.util.Random;
5 import java.io.IOException;
6 import java.io.OutputStream;
7
8 /**
9 * Encrypts using the EEXEC form (Used by Type 1 fonts).
10 *
11 * @author Simon Fischer
12 * @version $Id: EEXECEncryption.java 8584 2006-08-10 23:06:37Z duns $
13 */
14 public class EEXECEncryption extends OutputStream implements EEXECConstants {
15
16 private int n, c1, c2, r;
17
18 private OutputStream out;
19
20 private boolean first = true;
21
22 /**
23 * Creates an EEXECEncryption from given stream.
24 *
25 * @param out stream to write
26 */
27 public EEXECEncryption(OutputStream out) {
28 this(out, EEXEC_R, N);
29 }
30
31 /**
32 * Creates an EEXECEncryption from given stream.
33 *
34 * @param out stream to write
35 * @param r
36 */
37 public EEXECEncryption(OutputStream out, int r) {
38 this(out, r, N);
39 }
40
41 /**
42 * Creates an EEXECEncryption from given stream.
43 *
44 * @param out stream to write
45 * @param r
46 * @param n
47 */
48 public EEXECEncryption(OutputStream out, int r, int n) {
49 this.out = out;
50 this.c1 = C1;
51 this.c2 = C2;
52 this.r = r;
53 this.n = n;
54
55 }
56
57 private int encrypt(int plainByte) {
58 int cipher = (plainByte ^ (r >>> 8)) % 256;
59 r = ((cipher + r) * c1 + c2) % 65536;
60 return cipher;
61 }
62
63 public void write(int b) throws IOException {
64 if (first) {
65 for (int i = 0; i < n; i++)
66 out.write(encrypt(0));
67 first = false;
68 }
69
70 out.write(encrypt(b));
71 }
72
73 public void flush() throws IOException {
74 super.flush();
75 out.flush();
76 }
77
78 public void close() throws IOException {
79 flush();
80 super.close();
81 out.close();
82 }
83
84 private static class IntOutputStream extends OutputStream {
85 int[] chars;
86
87 int i;
88
89 private IntOutputStream(int size) {
90 chars = new int[size];
91 i = 0;
92 }
93
94 public void write(int b) {
95 chars[i++] = b;
96 } // str += (char)b; }
97
98 // public String getString() { return str; }
99 private int[] getInts() {
100 return chars;
101 }
102 }
103
104 /**
105 * Encrypt array of characters.
106 *
107 * @param chars int array to encrypt
108 * @param r
109 * @param n
110 * @return encrypted array
111 * @throws IOException if write fails (never happens)
112 */
113 public static int[] encryptString(int[] chars, int r, int n)
114 throws IOException {
115 IntOutputStream resultStr = new IntOutputStream(chars.length + 4);
116 EEXECEncryption eout = new EEXECEncryption(resultStr, r, n);
117 for (int i = 0; i < chars.length; i++)
118 eout.write(chars[i]);
119 return resultStr.getInts();
120 }
121 }