Code cleanup

This commit is contained in:
2016-02-28 23:03:00 +01:00
parent 57057298a1
commit 3e5e431d6f
6 changed files with 313 additions and 261 deletions

View File

@ -51,19 +51,23 @@ import java.util.Arrays;
*/
public class BouncyCryptography extends AbstractCryptography {
private static final X9ECParameters EC_CURVE_PARAMETERS = CustomNamedCurves.getByName("secp256k1");
private static final String ALGORITHM_ECDSA = "ECDSA";
private static final String PROVIDER = "BC";
static {
java.security.Security.addProvider(new BouncyCastleProvider());
}
public BouncyCryptography() {
super("BC");
super(PROVIDER);
}
@Override
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESEngine()),
new PKCS7Padding()
);
CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);
cipher.init(encrypt, params);
@ -105,9 +109,9 @@ public class BouncyCryptography extends AbstractCryptography {
ECPoint Q = keyToPoint(pubkey.getSigningKey());
KeySpec keySpec = new ECPublicKeySpec(Q, spec);
PublicKey publicKey = KeyFactory.getInstance("ECDSA", "BC").generatePublic(keySpec);
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER).generatePublic(keySpec);
Signature sig = Signature.getInstance("ECDSA", "BC");
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
sig.initVerify(publicKey);
sig.update(data);
return sig.verify(signature);
@ -129,9 +133,10 @@ public class BouncyCryptography extends AbstractCryptography {
BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey());
KeySpec keySpec = new ECPrivateKeySpec(d, spec);
java.security.PrivateKey privKey = KeyFactory.getInstance("ECDSA", "BC").generatePrivate(keySpec);
java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER)
.generatePrivate(keySpec);
Signature sig = Signature.getInstance("ECDSA", "BC");
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
sig.initSign(privKey);
sig.update(data);
return sig.sign();