use of java.security.InvalidKeyException in project qksms by moezbhatti.
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 ninja by ninjaframework.
the class CookieEncryption method encrypt.
/**
* Encrypts data with secret key.
*
* @param data text to encrypt
* @return encrypted text in base64 format
*/
public String encrypt(String data) {
Objects.requireNonNull(data, "Data to be encrypted");
if (!secretKeySpec.isPresent()) {
return data;
}
try {
// encrypt data
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec.get());
byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
// convert encrypted bytes to string in base64
return Base64.encodeBase64URLSafeString(encrypted);
} catch (InvalidKeyException ex) {
logger.error(getHelperLogMessage(), ex);
throw new RuntimeException(ex);
} catch (GeneralSecurityException ex) {
logger.error("Failed to encrypt data.", ex);
return "";
}
}
use of java.security.InvalidKeyException in project ninja by ninjaframework.
the class CookieEncryption method decrypt.
/**
* Decrypts data with secret key.
*
* @param data text to decrypt in base64 format
* @return decrypted text
*/
public String decrypt(String data) {
Objects.requireNonNull(data, "Data to be decrypted");
if (!secretKeySpec.isPresent()) {
return data;
}
// convert base64 encoded string to bytes
byte[] decoded = Base64.decodeBase64(data);
try {
// decrypt bytes
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec.get());
byte[] decrypted = cipher.doFinal(decoded);
// convert bytes to string
return new String(decrypted, StandardCharsets.UTF_8);
} catch (InvalidKeyException ex) {
logger.error(getHelperLogMessage(), ex);
throw new RuntimeException(ex);
} catch (GeneralSecurityException ex) {
logger.error("Failed to decrypt data.", ex);
return "";
}
}
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();
}
}
Aggregations