use of java.security.InvalidKeyException in project JamsMusicPlayer by psaravan.
the class Security method verify.
/**
* Verifies that the signature from the server matches the computed
* signature on the data. Returns true if the data is correctly signed.
*
* @param publicKey public key associated with the developer account
* @param signedData signed data from server
* @param signature server signature
* @return true if the data and signature match
*/
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
Signature sig;
try {
sig = Signature.getInstance(SIGNATURE_ALGORITHM);
sig.initVerify(publicKey);
sig.update(signedData.getBytes());
if (!sig.verify(Base64.decode(signature))) {
Log.e(TAG, "Signature verification failed.");
return false;
}
return true;
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "NoSuchAlgorithmException.");
} catch (InvalidKeyException e) {
Log.e(TAG, "Invalid key specification.");
} catch (SignatureException e) {
Log.e(TAG, "Signature exception.");
} catch (Base64DecoderException e) {
Log.e(TAG, "Base64 decoding failed.");
}
return false;
}
use of java.security.InvalidKeyException in project ratpack by ratpack.
the class DefaultCrypto method decrypt.
@Override
public ByteBuf decrypt(ByteBuf message, ByteBufAllocator allocator) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, ShortBufferException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
if (isInitializationVectorRequired) {
int ivByteLength = message.readByte();
byte[] iv = new byte[ivByteLength];
message.readBytes(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
} else {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
}
int messageLength = message.readableBytes();
ByteBuf decMessage = allocator.buffer(cipher.getOutputSize(messageLength));
ByteBuf byteBuf = message.readBytes(messageLength);
try {
int count = cipher.doFinal(byteBuf.nioBuffer(), decMessage.nioBuffer(0, messageLength));
if (!hasPadding) {
for (int i = count - 1; i >= 0; i--) {
if (decMessage.getByte(i) == 0x00) {
count--;
} else {
break;
}
}
}
decMessage.writerIndex(count);
return decMessage;
} catch (Exception e) {
decMessage.release();
throw e;
} finally {
byteBuf.release();
}
}
use of java.security.InvalidKeyException in project robovm by robovm.
the class BaseMac method engineInit.
protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
if (key == null) {
throw new InvalidKeyException("key is null");
}
if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (k.getParam() != null) {
param = k.getParam();
} else if (params instanceof PBEParameterSpec) {
param = PBE.Util.makePBEMacParameters(k, params);
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
} else if (params instanceof IvParameterSpec) {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else {
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
macEngine.init(param);
}
use of java.security.InvalidKeyException in project robovm by robovm.
the class BaseStreamCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
AlgorithmParameterSpec paramSpec = null;
if (params != null) {
for (int i = 0; i != availableSpecs.length; i++) {
try {
paramSpec = params.getParameterSpec(availableSpecs[i]);
break;
} catch (Exception e) {
continue;
}
}
if (paramSpec == null) {
throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
}
}
engineInit(opmode, key, paramSpec, random);
engineParams = params;
}
use of java.security.InvalidKeyException in project robovm by robovm.
the class BaseWrapCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
AlgorithmParameterSpec paramSpec = null;
if (params != null) {
for (int i = 0; i != availableSpecs.length; i++) {
try {
paramSpec = params.getParameterSpec(availableSpecs[i]);
break;
} catch (Exception e) {
// try next spec
}
}
if (paramSpec == null) {
throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
}
}
engineParams = params;
engineInit(opmode, key, paramSpec, random);
}
Aggregations