use of java.security.InvalidKeyException in project walle by Meituan-Dianping.
the class V2SchemeSigner method generateApkSignatureSchemeV2Block.
private static byte[] generateApkSignatureSchemeV2Block(List<SignerConfig> signerConfigs, Map<ContentDigestAlgorithm, byte[]> contentDigests) throws InvalidKeyException, SignatureException {
// FORMAT:
// * length-prefixed sequence of length-prefixed signer blocks.
List<byte[]> signerBlocks = new ArrayList<>(signerConfigs.size());
int signerNumber = 0;
for (SignerConfig signerConfig : signerConfigs) {
signerNumber++;
byte[] signerBlock;
try {
signerBlock = generateSignerBlock(signerConfig, contentDigests);
} catch (InvalidKeyException e) {
throw new InvalidKeyException("Signer #" + signerNumber + " failed", e);
} catch (SignatureException e) {
throw new SignatureException("Signer #" + signerNumber + " failed", e);
}
signerBlocks.add(signerBlock);
}
return encodeAsSequenceOfLengthPrefixedElements(new byte[][] { encodeAsSequenceOfLengthPrefixedElements(signerBlocks) });
}
use of java.security.InvalidKeyException in project walle by Meituan-Dianping.
the class V1SchemeSigner method signManifest.
/**
* Signs the provided APK using JAR signing (aka v1 signature scheme) and returns the list of
* JAR entries which need to be added to the APK as part of the signature.
*
* @param signerConfigs signer configurations, one for each signer. At least one signer config
* must be provided.
*
* @throws InvalidKeyException if a signing key is not suitable for this signature scheme or
* cannot be used in general
* @throws SignatureException if an error occurs when computing digests of generating
* signatures
*/
public static List<Pair<String, byte[]>> signManifest(List<SignerConfig> signerConfigs, DigestAlgorithm digestAlgorithm, List<Integer> apkSigningSchemeIds, OutputManifestFile manifest) throws InvalidKeyException, CertificateEncodingException, SignatureException {
if (signerConfigs.isEmpty()) {
throw new IllegalArgumentException("At least one signer config must be provided");
}
// For each signer output .SF and .(RSA|DSA|EC) file, then output MANIFEST.MF.
List<Pair<String, byte[]>> signatureJarEntries = new ArrayList<>(2 * signerConfigs.size() + 1);
byte[] sfBytes = generateSignatureFile(apkSigningSchemeIds, digestAlgorithm, manifest);
for (SignerConfig signerConfig : signerConfigs) {
String signerName = signerConfig.name;
byte[] signatureBlock;
try {
signatureBlock = generateSignatureBlock(signerConfig, sfBytes);
} catch (InvalidKeyException e) {
throw new InvalidKeyException("Failed to sign using signer \"" + signerName + "\"", e);
} catch (CertificateEncodingException e) {
throw new CertificateEncodingException("Failed to sign using signer \"" + signerName + "\"", e);
} catch (SignatureException e) {
throw new SignatureException("Failed to sign using signer \"" + signerName + "\"", e);
}
signatureJarEntries.add(Pair.of("META-INF/" + signerName + ".SF", sfBytes));
PublicKey publicKey = signerConfig.certificates.get(0).getPublicKey();
String signatureBlockFileName = "META-INF/" + signerName + "." + publicKey.getAlgorithm().toUpperCase(Locale.US);
signatureJarEntries.add(Pair.of(signatureBlockFileName, signatureBlock));
}
signatureJarEntries.add(Pair.of(MANIFEST_ENTRY_NAME, manifest.contents));
return signatureJarEntries;
}
use of java.security.InvalidKeyException in project OpenAttestation by OpenAttestation.
the class Diagnostic method tryMacWithPassword.
private static void tryMacWithPassword(String algorithmName, String message, String password) {
try {
SecretKeySpec key = new SecretKeySpec(password.getBytes(), algorithmName);
// a string like "HmacSHA256"
Mac mac = Mac.getInstance(algorithmName, "BC");
mac.init(key);
byte[] digest = mac.doFinal(message.getBytes());
System.out.println("Created " + algorithmName + " digest of length " + digest.length);
} catch (NoSuchProviderException e) {
System.err.println("Cannot use provider: BC: " + e.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("Cannot use algorithm: " + algorithmName + ": " + e.toString());
} catch (InvalidKeyException e) {
System.err.println("Cannot use key: " + password + ": " + e.toString());
}
}
use of java.security.InvalidKeyException in project OpenAttestation by OpenAttestation.
the class Diagnostic method trySignature.
private static void trySignature() {
String algorithmName = "SHA1withRSA";
try {
// generate keypair
// NoSuchAlgorithmException, NoSuchProviderException
KeyPair keyPair = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
String plaintext = "This is the message being signed";
// generate signature
// NoSuchAlgorithmException, NoSuchProviderException
Signature instance = Signature.getInstance("SHA1withRSAEncryption", "BC");
// InvalidKeyException
instance.initSign(privateKey);
// SignatureException
instance.update((plaintext).getBytes());
byte[] signature = instance.sign();
System.out.println("Generated SHA1 with RSA signature of length: " + signature.length);
} catch (NoSuchProviderException e) {
System.err.println("Cannot use provider: BC: " + e.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("Cannot use algorithm: " + algorithmName + ": " + e.toString());
} catch (InvalidKeyException e) {
System.err.println("Cannot use key: " + e.toString());
} catch (SignatureException e) {
System.err.println("Cannot generate signature: " + e.toString());
}
}
use of java.security.InvalidKeyException in project remusic by aa112901.
the class AESTools method encrpty.
public static String encrpty(String paramString) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
messageDigest.update(INPUT.getBytes());
byte[] stringBytes = messageDigest.digest();
StringBuilder stringBuilder = new StringBuilder(stringBytes.length * 2);
for (int i = 0; i < stringBytes.length; i++) {
stringBuilder.append(CHARS[((stringBytes[i] & 0xF0) >>> 4)]);
stringBuilder.append(CHARS[(stringBytes[i] & 0xF)]);
}
String str = stringBuilder.toString();
SecretKeySpec localSecretKeySpec = new SecretKeySpec(str.substring(str.length() / 2).getBytes(), "AES");
Cipher localCipher;
try {
localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
localCipher.init(1, localSecretKeySpec, new IvParameterSpec(IV.getBytes()));
return URLEncoder.encode(new String(BytesHandler.getChars(localCipher.doFinal(paramString.getBytes()))), "utf-8");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return "";
}
Aggregations