@@ -23,7 +23,7 @@ public class ASAPCryptoAlgorithms {
2323 * @throws ASAPSecurityException
2424 */
2525 public static void writeEncryptedMessagePackage (byte [] unencryptedBytes , CharSequence recipient ,
26- BasicKeyStore basicKeyStore , OutputStream os ) throws ASAPSecurityException {
26+ BasicKeyStore basicKeyStore , OutputStream os ) throws ASAPSecurityException {
2727
2828 PublicKey publicKey = basicKeyStore .getPublicKey (recipient );
2929 // there should be an exception - but better safe than sorry
@@ -40,7 +40,7 @@ public static void writeEncryptedMessagePackage(byte[] unencryptedBytes, CharSeq
4040 byte [] encodedSymmetricKey = encryptionKey .getEncoded ();
4141
4242 // encrypt symmetric key
43- Cipher cipher = Cipher .getInstance (basicKeyStore .getRSAEncryptionAlgorithm ());
43+ Cipher cipher = Cipher .getInstance (basicKeyStore .getAsymmetricEncryptionAlgorithm ());
4444 cipher .init (Cipher .ENCRYPT_MODE , publicKey );
4545 byte [] encryptedSymmetricKeyBytes = cipher .doFinal (encodedSymmetricKey );
4646
@@ -94,6 +94,7 @@ public static SecretKey generateSymmetricKey(String keyType, int size) throws AS
9494 SecretKey secretKey = gen .generateKey ();
9595 return secretKey ;
9696 } catch (NoSuchAlgorithmException e ) {
97+ e .printStackTrace ();
9798 throw new ASAPSecurityException (ASAPCryptoAlgorithms .class .getSimpleName (), e );
9899 }
99100 }
@@ -173,7 +174,9 @@ public static byte[] encryptSymmetric(byte[] unencryptedBytes, SecretKey encrypt
173174 return symmetricCipher .doFinal (unencryptedBytes );
174175 } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
175176 | 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 );
177180 }
178181 }
179182
@@ -186,32 +189,37 @@ public static byte[] decryptSymmetric(byte[] encryptedContent, SecretKey symmetr
186189 return symmetricCipher .doFinal (encryptedContent );
187190 } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
188191 | 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 );
190195 }
191196 }
192197
193198 public static byte [] decryptAsymmetric (byte [] encryptedBytes , BasicKeyStore basicKeyStore )
194199 throws ASAPSecurityException {
195200 try {
196- Cipher cipher = Cipher .getInstance (basicKeyStore .getRSAEncryptionAlgorithm ());
201+ Cipher cipher = Cipher .getInstance (basicKeyStore .getAsymmetricEncryptionAlgorithm ());
197202 cipher .init (Cipher .DECRYPT_MODE , basicKeyStore .getPrivateKey ());
198203 return cipher .doFinal (encryptedBytes );
199204 } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException
200205 | 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 );
202209 }
203210 }
204211
205212 public static byte [] sign (byte [] bytes2Sign , BasicKeyStore basicKeyStore )
206213 throws ASAPSecurityException {
207214
208215 try {
209- Signature signature = Signature .getInstance (basicKeyStore .getRSASigningAlgorithm ());
216+ Signature signature = Signature .getInstance (basicKeyStore .getAsymmetricSigningAlgorithm ());
210217 signature .initSign (basicKeyStore .getPrivateKey ());
211218 signature .update (bytes2Sign );
212219 return signature .sign ();
213220 } 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 );
215223 }
216224 }
217225
@@ -222,12 +230,13 @@ public static boolean verify(byte[] signedData, byte[] signatureBytes, String se
222230 if (publicKey == null ) return false ;
223231
224232 try {
225- Signature signature = Signature .getInstance (basicKeyStore .getRSASigningAlgorithm ());
233+ Signature signature = Signature .getInstance (basicKeyStore .getAsymmetricSigningAlgorithm ());
226234 signature .initVerify (publicKey ); // init with private key
227235 signature .update (signedData ); // feed with signed data
228236 return signature .verify (signatureBytes ); // check against signature
229237 } 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 );
231240 }
232241 }
233242}
0 commit comments