@@ -23,7 +23,7 @@ public class ASAPCryptoAlgorithms {
23
23
* @throws ASAPSecurityException
24
24
*/
25
25
public static void writeEncryptedMessagePackage (byte [] unencryptedBytes , CharSequence recipient ,
26
- BasicKeyStore basicKeyStore , OutputStream os ) throws ASAPSecurityException {
26
+ BasicKeyStore basicKeyStore , OutputStream os ) throws ASAPSecurityException {
27
27
28
28
PublicKey publicKey = basicKeyStore .getPublicKey (recipient );
29
29
// there should be an exception - but better safe than sorry
@@ -40,7 +40,7 @@ public static void writeEncryptedMessagePackage(byte[] unencryptedBytes, CharSeq
40
40
byte [] encodedSymmetricKey = encryptionKey .getEncoded ();
41
41
42
42
// encrypt symmetric key
43
- Cipher cipher = Cipher .getInstance (basicKeyStore .getRSAEncryptionAlgorithm ());
43
+ Cipher cipher = Cipher .getInstance (basicKeyStore .getAsymmetricEncryptionAlgorithm ());
44
44
cipher .init (Cipher .ENCRYPT_MODE , publicKey );
45
45
byte [] encryptedSymmetricKeyBytes = cipher .doFinal (encodedSymmetricKey );
46
46
@@ -94,6 +94,7 @@ public static SecretKey generateSymmetricKey(String keyType, int size) throws AS
94
94
SecretKey secretKey = gen .generateKey ();
95
95
return secretKey ;
96
96
} catch (NoSuchAlgorithmException e ) {
97
+ e .printStackTrace ();
97
98
throw new ASAPSecurityException (ASAPCryptoAlgorithms .class .getSimpleName (), e );
98
99
}
99
100
}
@@ -173,7 +174,9 @@ public static byte[] encryptSymmetric(byte[] unencryptedBytes, SecretKey encrypt
173
174
return symmetricCipher .doFinal (unencryptedBytes );
174
175
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
175
176
| IllegalBlockSizeException | BadPaddingException e ) {
176
- throw new ASAPSecurityException ("problems when encrypting with symmetric key" , e );
177
+ e .printStackTrace ();
178
+ throw new ASAPSecurityException ("symmetric encryption failed: "
179
+ + basicKeyStore .getSymmetricEncryptionAlgorithm (), e );
177
180
}
178
181
}
179
182
@@ -186,32 +189,37 @@ public static byte[] decryptSymmetric(byte[] encryptedContent, SecretKey symmetr
186
189
return symmetricCipher .doFinal (encryptedContent );
187
190
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
188
191
| IllegalBlockSizeException | BadPaddingException e ) {
189
- throw new ASAPSecurityException ("problems when decrypting with symmetric key" , e );
192
+ e .printStackTrace ();
193
+ throw new ASAPSecurityException ("symmetric decryption failed: "
194
+ + basicKeyStore .getSymmetricEncryptionAlgorithm (), e );
190
195
}
191
196
}
192
197
193
198
public static byte [] decryptAsymmetric (byte [] encryptedBytes , BasicKeyStore basicKeyStore )
194
199
throws ASAPSecurityException {
195
200
try {
196
- Cipher cipher = Cipher .getInstance (basicKeyStore .getRSAEncryptionAlgorithm ());
201
+ Cipher cipher = Cipher .getInstance (basicKeyStore .getAsymmetricEncryptionAlgorithm ());
197
202
cipher .init (Cipher .DECRYPT_MODE , basicKeyStore .getPrivateKey ());
198
203
return cipher .doFinal (encryptedBytes );
199
204
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
200
205
| IllegalBlockSizeException | BadPaddingException e ) {
201
- throw new ASAPSecurityException ("problems when encrypting with symmetric key" , e );
206
+ e .printStackTrace ();
207
+ throw new ASAPSecurityException ("asymmetric decryption failed: "
208
+ + basicKeyStore .getAsymmetricEncryptionAlgorithm (), e );
202
209
}
203
210
}
204
211
205
212
public static byte [] sign (byte [] bytes2Sign , BasicKeyStore basicKeyStore )
206
213
throws ASAPSecurityException {
207
214
208
215
try {
209
- Signature signature = Signature .getInstance (basicKeyStore .getRSASigningAlgorithm ());
216
+ Signature signature = Signature .getInstance (basicKeyStore .getAsymmetricSigningAlgorithm ());
210
217
signature .initSign (basicKeyStore .getPrivateKey ());
211
218
signature .update (bytes2Sign );
212
219
return signature .sign ();
213
220
} catch (InvalidKeyException | SignatureException | NoSuchAlgorithmException e ) {
214
- throw new ASAPSecurityException ("problem when signing" , e );
221
+ e .printStackTrace ();
222
+ throw new ASAPSecurityException ("signing failed: " + basicKeyStore .getAsymmetricSigningAlgorithm (), e );
215
223
}
216
224
}
217
225
@@ -222,12 +230,13 @@ public static boolean verify(byte[] signedData, byte[] signatureBytes, String se
222
230
if (publicKey == null ) return false ;
223
231
224
232
try {
225
- Signature signature = Signature .getInstance (basicKeyStore .getRSASigningAlgorithm ());
233
+ Signature signature = Signature .getInstance (basicKeyStore .getAsymmetricSigningAlgorithm ());
226
234
signature .initVerify (publicKey ); // init with private key
227
235
signature .update (signedData ); // feed with signed data
228
236
return signature .verify (signatureBytes ); // check against signature
229
237
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e ) {
230
- throw new ASAPSecurityException ("problems when verifying" , e );
238
+ e .printStackTrace ();
239
+ throw new ASAPSecurityException ("verifying failed: " + basicKeyStore .getAsymmetricSigningAlgorithm (), e );
231
240
}
232
241
}
233
242
}
0 commit comments