-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstract_Administrator.java
More file actions
178 lines (138 loc) · 7.31 KB
/
Abstract_Administrator.java
File metadata and controls
178 lines (138 loc) · 7.31 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import javax.crypto.NoSuchPaddingException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.InvalidAlgorithmParameterException;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import javax.crypto.Cipher;
import java.security.DigestException;
import java.security.InvalidKeyException;
public abstract class Abstract_Administrator {
protected static final int BLOCK_SIZE_OF_ENCRYPTION = 16;
protected static final int LENGTH_OF_HEADER = BLOCK_SIZE_OF_ENCRYPTION -1;
protected static final int FILE_OFFSET_SIZE_OF_HEADER = 0;
protected static final int FILE_LENGTH_OF_HEADER = 4; //Size of an integer in bytes
protected static final int HASH_OFFSET_OF_HEADER = FILE_LENGTH_OF_HEADER; //4
protected static final int FILE_EXTENSION_LENGTH_OF_HEADER = 3;
protected static final int HASH_LENGTH_OF_HEADER = LENGTH_OF_HEADER - HASH_OFFSET_OF_HEADER - FILE_EXTENSION_LENGTH_OF_HEADER; //8
protected static final int FILE_EXTENSION_OFFSET_HEADER = HASH_OFFSET_OF_HEADER + HASH_LENGTH_OF_HEADER;
protected static final int MIN_NUM_OF_PIXELS_FOR_NONCE = 5;
protected static final int MAX_NUM_OF_PIXELS_FOR_NONCE = 22;
protected static final int PORTION_OF_PIXELS_FOR_NONCE = 1000; //1 in 1,000
protected static final int NON_ALPHA_CHANNELS_PER_PIXEL = 3;
protected static final int BITS_IN_ONE_BYTE = 8;
protected boolean _started;
protected boolean _completed;
protected boolean _completed_successfully;
protected Cipher _cipher_text;
public Abstract_Administrator( ) {
this._started = false;
this._completed = false;
this._completed_successfully = false;
}
// called by abstract administrator to initialize cipher text and give securerandom to
// scatter administrator
protected boolean initialize( char[] _character_password, Class_Abstract _abstract, boolean encrypt ) throws Weird_Exception {
boolean result = false;
//Avoid nullPointerException
if ( ( _abstract != null ) && ( _character_password != null ) ) {
//number of pixels required to generate the nonce
Dimension _dimension = _abstract.fetch_Original_Image_Dimension();
int numPixels = ( _dimension.height * _dimension.width ) / PORTION_OF_PIXELS_FOR_NONCE; //Use 0.1% of the pixels (rounded down)
numPixels = Math.max( MIN_NUM_OF_PIXELS_FOR_NONCE, numPixels ); //At least 5 (15 bytes)
numPixels = Math.min( MAX_NUM_OF_PIXELS_FOR_NONCE, numPixels ); //But no more than 22 (66 bytes)
//Fetch the nonce
byte[] nonce = _abstract.fetch_Nonce( numPixels * NON_ALPHA_CHANNELS_PER_PIXEL ); //That input value is the number of bytes wanted
Key_Struct_Package kps = null;
try {
kps = Get_Key_Class.create_cryptographic_requirements( nonce, _character_password);
result = _abstract.make_random_scat( kps.rand );
if ( result == true ) {
this._cipher_text = Cipher.getInstance("AES/PCBC/PKCS5Padding");
if ( encrypt == true ) {
this._cipher_text.init( Cipher.ENCRYPT_MODE, kps.key, kps.ivParam );
} else {
this._cipher_text.init( Cipher.DECRYPT_MODE, kps.key, kps.ivParam );
}
this._started = true;
}
kps.make_zero();
} catch ( DigestException | NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e ) {
result = false;
try {
if ( kps != null ) {
kps.make_zero();
}
} catch ( Exception e2 ) {
e2.printStackTrace();
}
try {
if ( _character_password != null ) {
Byte_Conversions.make_zero( _character_password );
}
} catch ( Exception e2 ) {
e2.printStackTrace();
}
Weird_Exception se = new Weird_Exception( "There was an error with Java cryptographic services that is outside of our control", e );
se.getStackTrace();
throw se;
} catch ( InvalidKeyException | InvalidAlgorithmParameterException e ) {
result = false;
try {
if ( kps != null ) {
kps.make_zero();
}
} catch ( Exception e2 ) {
e2.printStackTrace();
}
try {
if ( _character_password != null ) {
Byte_Conversions.make_zero( _character_password );
}
} catch ( Exception e2 ) {
e2.printStackTrace();
}
Weird_Exception se = new Weird_Exception( "Invalid parameters were generated for Java cryptographic services", e );
se.getStackTrace();
throw se;
}
}
return result;
}
public static boolean Is_Image_Valid( BufferedImage img ) {
return Class_Abstract.Is_Image_Valid( img );
}
// returns true if all required variables are initialized
public boolean has_started() {
return this._started;
}
public boolean has_completed() {
return this._completed;
}
public boolean has_completed_successfully() {
return this._completed_successfully;
}
public abstract BufferedImage fetch_original_image();
public abstract boolean make_zero();
// determines max image payload size that can be written with
// given dimensions of image
public static int Max_Image_Pay_Load( Dimension _dimension ) {
int answer = Class_Abstract.Max_Image_Pay_Load( _dimension );
answer -= ( BLOCK_SIZE_OF_ENCRYPTION * BITS_IN_ONE_BYTE ); //Minus Header Size
return answer;
}
// same as previous but with given number of pixels in one bit
public static int Max_Image_Pay_Load( Dimension _dimension, int _Pixels_in_one_Bit ) {
if ( _Pixels_in_one_Bit <= 0 ) {
throw new IllegalArgumentException( "The value _Pixels_in_one_Bit provided to Abstract_Administrator.Max_Image_Pay_Load was non-positive: " + _Pixels_in_one_Bit + "." );
}
int answer = Class_Abstract.Max_Image_Pay_Load( _dimension, _Pixels_in_one_Bit );
answer -= ( BLOCK_SIZE_OF_ENCRYPTION * BITS_IN_ONE_BYTE );
return answer;
}
public static int Max_Pixels_in_One_Bit( Dimension _dimension, int byte_size_of_file ) {
int _length_of_int_payload = ( byte_size_of_file - ( byte_size_of_file % BLOCK_SIZE_OF_ENCRYPTION ) + BLOCK_SIZE_OF_ENCRYPTION );
int answer = Class_Abstract.Max_Pixels_in_One_Bit( _dimension, ( _length_of_int_payload + BLOCK_SIZE_OF_ENCRYPTION ) ); //Count the header
return answer;
}
}