Skip to content

Commit f43518a

Browse files
committed
Added log messages - there is progress, see SN2
1 parent 5905219 commit f43518a

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

src/net/sharksystem/crypto/ASAPCryptoAlgorithms.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/net/sharksystem/crypto/BasicCryptoKeyStorage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public BasicCryptoKeyStorage(CharSequence ownerID, KeyPair ownerKeyPair) {
3434

3535
public SecretKey generateSymmetricKey() throws ASAPSecurityException {
3636
return ASAPCryptoAlgorithms.generateSymmetricKey(
37-
DEFAULT_SYMMETRIC_KEY_TYPE, DEFAULT_AES_KEY_SIZE);
37+
DEFAULT_SYMMETRIC_KEY_TYPE, DEFAULT_SYMMETRIC_KEY_SIZE);
3838
}
3939

4040
public int getSymmetricKeyLen() {
41-
return BasicKeyStore.DEFAULT_AES_KEY_SIZE;
41+
return BasicKeyStore.DEFAULT_SYMMETRIC_KEY_SIZE;
4242
}
4343

4444
private String getLogStart() {
@@ -123,8 +123,8 @@ public PrivateKey getPrivateKey(CharSequence subjectID) throws ASAPSecurityExcep
123123
}
124124

125125
@Override
126-
public String getRSAEncryptionAlgorithm() {
127-
return DEFAULT_RSA_ENCRYPTION_ALGORITHM;
126+
public String getAsymmetricEncryptionAlgorithm() {
127+
return DEFAULT_ASYMMETRIC_ENCRYPTION_ALGORITHM;
128128
}
129129

130130
@Override
@@ -148,8 +148,8 @@ public CharSequence getOwner() {
148148
}
149149

150150
@Override
151-
public String getRSASigningAlgorithm() {
152-
return DEFAULT_SIGNATURE_ALGORITHM;
151+
public String getAsymmetricSigningAlgorithm() {
152+
return DEFAULT_ASYMMETRIC_SIGNATURE_ALGORITHM;
153153
}
154154

155155
public void addKeyPair(String peerID, KeyPair keyPair) {

src/net/sharksystem/crypto/BasicCryptoSettings.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import java.security.PublicKey;
88

99
public interface BasicCryptoSettings {
10-
String DEFAULT_RSA_ENCRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";
10+
String DEFAULT_ASYMMETRIC_ENCRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";
1111
String DEFAULT_SYMMETRIC_KEY_TYPE = "AES";
1212
String DEFAULT_SYMMETRIC_ENCRYPTION_ALGORITHM = "AES/ECB/PKCS5Padding";
1313
// public static int DEFAULT_AES_KEY_SIZE = 256;
14-
int DEFAULT_AES_KEY_SIZE = 128; // TODO we can do better
15-
String DEFAULT_SIGNATURE_ALGORITHM = "SHA256withRSA";
14+
int DEFAULT_SYMMETRIC_KEY_SIZE = 128; // TODO we can do better
15+
String DEFAULT_ASYMMETRIC_SIGNATURE_ALGORITHM = "SHA256withRSA";
1616

1717
/**
1818
*
@@ -30,9 +30,9 @@ public interface BasicCryptoSettings {
3030
*/
3131
PublicKey getPublicKey() throws ASAPSecurityException;
3232

33-
String getRSAEncryptionAlgorithm();
33+
String getAsymmetricEncryptionAlgorithm();
3434

35-
String getRSASigningAlgorithm();
35+
String getAsymmetricSigningAlgorithm();
3636

3737
SecretKey generateSymmetricKey() throws ASAPSecurityException;
3838

0 commit comments

Comments
 (0)