Example usage for java.security InvalidKeyException printStackTrace

List of usage examples for java.security InvalidKeyException printStackTrace

Introduction

In this page you can find the example usage for java.security InvalidKeyException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Crypto.java

/**
 * adding main() for usage demonstration. With member vars, some of the locals would not be needed
 *///from  ww w .  ja v  a 2  s  . c  o  m
public static void main(String[] args) {

    // create the input.txt file in the current directory before continuing
    File input = new File("input.txt");
    File eoutput = new File("encrypted.aes");
    File doutput = new File("decrypted.txt");
    String iv = null;
    String salt = null;
    Crypto en = new Crypto("mypassword");

    /*
     * setup encryption cipher using password. print out iv and salt
     */
    try {
        en.setupEncrypt();
        iv = Hex.encodeHexString(en.getInitVec()).toUpperCase();
        salt = Hex.encodeHexString(en.getSalt()).toUpperCase();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidParameterSpecException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    /*
     * write out encrypted file
     */
    try {
        en.WriteEncryptedFile(input, eoutput);
        System.out.printf("File encrypted to " + eoutput.getName() + "\niv:" + iv + "\nsalt:" + salt + "\n\n");
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    /*
     * decrypt file
     */
    Crypto dc = new Crypto("mypassword");
    try {
        dc.setupDecrypt(iv, salt);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (DecoderException e) {
        e.printStackTrace();
    }

    /*
     * write out decrypted file
     */
    try {
        dc.ReadEncryptedFile(eoutput, doutput);
        System.out.println("decryption finished to " + doutput.getName());
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:test.PBEncryptLink.java

/**
 * @param args//from  w  w w .  j  av a  2  s.  c om
 * @throws InvalidAlgorithmParameterException
 * @throws UnsupportedEncodingException
 *             Shouldn't happen.
 */
public static void main(String[] args) throws InvalidAlgorithmParameterException, UnsupportedEncodingException {
    String keyString = "Password";
    String iv = null;
    if (args.length > 0) {
        keyString = args[0];
    }

    if (args.length > 1) {
        iv = args[1];
    }

    String testEncryptedLink = null;
    String testDecryptedLink = null;
    String urlEncoded = null;
    String urlDecoded = null;
    String encodedIV = null;
    String decodedIV = null;
    byte[] encryptKeyBytes = null;
    byte[] decryptKeyBytes = null;

    try {
        /*
         * Instantiate two PBEncryptLink objects
         */
        PBEncryptLink encryptLink = new PBEncryptLink();
        PBEncryptLink decryptLink = new PBEncryptLink();

        /*
         * Convert password string to byte array; one copy for each action.
         */
        encryptKeyBytes = encryptLink.stringTo_iso8859_1_Bytes(keyString);
        decryptKeyBytes = encryptKeyBytes.clone();

        /*
         * Set object mode.
         */
        encryptLink.setCipherMode(CipherMode.ENCRYPT);
        decryptLink.setCipherMode(CipherMode.DECRYPT);

        /*
         * Set Initialization vector if one was passed.
         */
        if (iv != null) {
            encryptLink.setIV(iv);
        }

        /*
         * Initialize encrypt object and zero password bytes.
         */
        encryptLink.initCipher(encryptKeyBytes);
        zeroBytes(encryptKeyBytes);

        /*
         * Do the same for the decrypt object and also print out the IV.
         */
        decryptLink.setIV(encryptLink.getIV());
        encodedIV = new String(encryptLink.getIV(), "ISO8859_1");
        decodedIV = new String(Base64.encodeBase64(encryptLink.getIV()));
        decryptLink.initCipher(decryptKeyBytes);
        zeroBytes(decryptKeyBytes);

        /*
         * Encrypt the link and decrypt it.
         */
        String testCourse = "GEN101";
        String testUser = "ci_test";
        Logger testLogger = null;
        testEncryptedLink = encryptLink.encryptContextString(testCourse, testUser, testLogger);
        urlEncoded = URLEncoder.encode(testEncryptedLink, "UTF-8");
        urlDecoded = URLDecoder.decode(urlEncoded, "UTF-8");
        testDecryptedLink = decryptLink.decryptString(urlDecoded);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    /*
     * Display results.
     */
    System.out.println("Results:");
    System.out.println("Unencoded IV: " + decodedIV);
    System.out.println("Encoded IV: " + encodedIV);
    System.out.println("Encrypted data: " + testEncryptedLink);
    System.out.println("Encrypted data url encoded: " + urlEncoded);
    System.out.println("encrypted data decoded: " + urlDecoded);
    System.out.println("Decrypted data: " + testDecryptedLink);

    System.exit(0);

}

From source file:edu.rice.batchsig.bench.BenchSigner.java

public static void main(String args[]) throws FileNotFoundException, ParseException {
    Security.addProvider(new BouncyCastleProvider());
    try {// w w w.ja v  a 2s .  c  o  m
        BenchSigner bench = new BenchSigner();
        bench.parsecmd(args);
        System.exit(0);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] encipherAes256(byte[] clearText, String keyString) throws NullPointerException {
    if (keyString == null || keyString.length() == 0) {
        throw new NullPointerException("Please give Password");
    }//from   w  w w.  j a  va2s  . c  o  m

    if (clearText == null || clearText.length <= 0) {
        throw new NullPointerException("Please give clearText");
    }

    try {
        SecretKeySpec skeySpec = getKey(keyString);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // Cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);

        return cipher.doFinal(clearText);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] decipherAes256(byte[] encrypedPwdBytes, String password) throws NullPointerException {

    if (password == null || password.length() == 0) {
        throw new NullPointerException("Please give Password");
    }//from  w  w  w.  j a  v  a 2  s . c  o  m

    if (encrypedPwdBytes == null || encrypedPwdBytes.length <= 0) {
        throw new NullPointerException("Please give encrypedPwdBytes");
    }

    try {
        SecretKey key = getKey(password);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
        byte[] decryptedValueBytes = (cipher.doFinal(encrypedPwdBytes));

        return decryptedValueBytes;

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.google.resting.serviceaccessor.impl.ServiceAccessor.java

public static void signRequest(String keyString, ServiceContext serviceContext) {
    boolean isSecureInvocation = serviceContext.isSecureInvocation();
    String targetDomain = serviceContext.getTargetDomain();
    String path = serviceContext.getPath();
    Verb verb = serviceContext.getVerb();
    String contextPathElement = serviceContext.getContextPathElement();
    String encoding = serviceContext.getCharset().getName();
    List<NameValuePair> inputParams = serviceContext.getInputParams();
    try {/*from  w w  w. ja v  a 2  s .c  om*/
        path = path + String.format(AMPERSAND_SEPARATED_STRING, SIGNATURE, getSignature(keyString, targetDomain,
                verb, isSecureInvocation, contextPathElement, inputParams, encoding));
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:yoyo.framework.standard.shared.SecurityUtils.java

/**
 * ?/?/*from  ww  w .j ava2  s . c o m*/
 * @param encryptedBytes ?(/?)
 * @param base64Key ?
 * @param cipherMode ?/?
 * @return ?(?/)
 */
private static byte[] exec(final byte[] encryptedBytes, final String base64Key, final int cipherMode) {
    try {
        final Cipher cipher = Cipher.getInstance(ALGO_NAME);
        cipher.init(cipherMode, new SecretKeySpec(Base64.decodeBase64(base64Key), ALGO_NAME));
        return cipher.doFinal(encryptedBytes);
    } catch (final InvalidKeyException e) {
        e.printStackTrace();
        throw new StandardRuntimeException(e);
    } catch (final NoSuchAlgorithmException e) {
        throw new StandardRuntimeException(e);
    } catch (final NoSuchPaddingException e) {
        throw new StandardRuntimeException(e);
    } catch (final IllegalBlockSizeException e) {
        throw new StandardRuntimeException(e);
    } catch (final BadPaddingException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:org.suren.autotest.web.framework.util.EncryptorUtil.java

/**
 * //from   w ww.  j av a  2  s.  c om
 * @param plainText
 * @return
 */
public static String encrypt(String plainText) {
    try {
        return Encryptor.getInstance(Encryptor.ALG_DES, getSecretKey()).encryptStr(plainText);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }

    return plainText;
}

From source file:org.suren.autotest.web.framework.util.EncryptorUtil.java

/**
 * //from w  w w  . j  a v  a  2 s  .  co  m
 * @param encryptText
 * @return
 */
public static String decrypt(String encryptText) {
    try {
        return Encryptor.getInstance(Encryptor.ALG_DES, getSecretKey()).decryptStr(encryptText);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }

    return encryptText;
}

From source file:org.suren.autotest.web.framework.util.EncryptorUtil.java

/**
 * ??Base64?/* w w  w. j  a va  2  s  .c  om*/
 * @param plainText
 * @return
 */
public static String encryptWithBase64(String plainText) {
    try {
        return Base64.encodeBase64String(
                Encryptor.getInstance(Encryptor.ALG_DES, getSecretKey()).encrypt(plainText));
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }

    return null;
}