-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCryptoOutputStream.java
More file actions
42 lines (35 loc) · 933 Bytes
/
Copy pathCryptoOutputStream.java
File metadata and controls
42 lines (35 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//package ecc.io;
import java.io.*;
//import ecc.*;
public class CryptoOutputStream extends OutputStream {
private DataOutputStream out;
private CryptoSystem cs;
private Key key;
private byte[] buffer;
private int top;
public CryptoOutputStream(OutputStream out, CryptoSystem cs, Key key) {
this.out = new DataOutputStream(out);
this.cs = cs;
this.key = key;
buffer = new byte[cs.blockSize()];
}
private void writeOut() throws IOException {
if(top == 0) return;
byte[] cipher = cs.encrypt(buffer, top, key);
out.writeInt(cipher.length);
out.write(cipher);
top = 0;
}
public void write(int b) throws IOException {
buffer[top] = (byte) b;
top++;
if(top == buffer.length) writeOut();
}
public void flush() throws IOException {
writeOut();
out.flush();
}
public void close() throws IOException {
out.close();
}
}