Search in sources :

Example 11 with InvalidKeyException

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) });
}
Also used : ArrayList(java.util.ArrayList) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 12 with InvalidKeyException

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;
}
Also used : PublicKey(java.security.PublicKey) ArrayList(java.util.ArrayList) CertificateEncodingException(java.security.cert.CertificateEncodingException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) Pair(com.android.apksigner.core.internal.util.Pair)

Example 13 with InvalidKeyException

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());
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 14 with InvalidKeyException

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());
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) JDKDigestSignature(org.bouncycastle.jce.provider.JDKDigestSignature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException)

Example 15 with InvalidKeyException

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 "";
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)478 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)246 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)123 SignatureException (java.security.SignatureException)92 IOException (java.io.IOException)90 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)87 BadPaddingException (javax.crypto.BadPaddingException)83 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)80 Cipher (javax.crypto.Cipher)71 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)61 Signature (java.security.Signature)58 SecretKeySpec (javax.crypto.spec.SecretKeySpec)52 PublicKey (java.security.PublicKey)49 SecretKey (javax.crypto.SecretKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)40 NoSuchProviderException (java.security.NoSuchProviderException)38 Mac (javax.crypto.Mac)38 SecureRandom (java.security.SecureRandom)32