List of usage examples for java.security InvalidKeyException getLocalizedMessage
public String getLocalizedMessage()
From source file:jfs.sync.encryption.JFSEncryptedFile.java
private Cipher getCipher(int cipherMode) { try {/*from w w w. j a va 2 s . c o m*/ String cipherSpec = ((AbstractFileProducer) getFileProducer()).storageAccess.getCipherSpec(); byte[] credentials = ((AbstractFileProducer) getFileProducer()).storageAccess .getFileCredentials(getRelativePath()); return SecurityUtils.getCipher(cipherSpec, cipherMode, credentials); } catch (NoSuchAlgorithmException nsae) { log.error("getCipher() No Such Algorhithm"); } catch (NoSuchPaddingException nspe) { log.error("getCipher() No Such Padding"); } catch (InvalidKeyException ike) { log.error("getCipher() Invalid Key " + ike.getLocalizedMessage()); } // try/catch return null; }
From source file:jfs.sync.encryption.AbstractEncryptedStorageAccess.java
protected String getEncryptedFileName(String relativePath, String pathElement) { if (encryptionCache.containsKey(relativePath+getSeparator()+pathElement)) { return encryptionCache.get(relativePath+getSeparator()+pathElement); } // if/*w w w .j a v a 2 s. co m*/ try { Cipher encrypter = SecurityUtils.getCipher(getCipherSpec(), Cipher.ENCRYPT_MODE, getCredentials(relativePath)); byte[] bytes = encrypter.doFinal(getEncodedFileName(relativePath, pathElement)); StringBuilder result = new StringBuilder(); int index = 0; int bc = 0; // System.out.print("E: "); for (int i = 0; i<bytes.length; i++) { for (int mask = 128; mask>0; mask = mask>>1) { int bit = ((bytes[i]&mask)==0) ? 0 : 1; // System.out.print(bit); index = (index<<1)+bit; bc++; if (bc==bits) { char code = CODES[index]; result.append(code); bc = 0; index = 0; } // if } // for // System.out.print(" "+bytes[i]+" "); } // for index = index<<(bits-bc); char code = CODES[index]; result.append(code); String resultString = result.toString(); // System.out.println(""); encryptionCache.put(relativePath+getSeparator()+pathElement, resultString); pathElement = resultString; } catch (NoSuchAlgorithmException nsae) { LOG.error("getEncryptedFileName() No Such Algorhithm "+nsae.getLocalizedMessage()); } catch (NoSuchPaddingException nspe) { LOG.error("getEncryptedFileName() No Such Padding "+nspe.getLocalizedMessage()); } catch (InvalidKeyException ike) { LOG.error("getEncryptedFileName() Invalid Key "+ike.getLocalizedMessage()); } catch (ArrayIndexOutOfBoundsException e) { LOG.error("getEncryptedFileName("+pathElement+") ArrayIndexOutOfBoundsException", e); } catch (IllegalBlockSizeException e) { LOG.error("getEncryptedFileName("+pathElement+") IllegalBlockSizeException", e); } catch (BadPaddingException e) { LOG.error("getEncryptedFileName("+pathElement+") BadPaddingException", e); } // try/catch return pathElement; }
From source file:jfs.sync.encryption.AbstractEncryptedStorageAccess.java
protected String getDecryptedFileName(String relativePath, String name) { if (decryptionCache.containsKey(relativePath+getSeparator()+name)) { return decryptionCache.get(relativePath+getSeparator()+name); } // if/* w w w . j a v a 2s.c o m*/ try { List<Byte> resultList = new ArrayList<Byte>(); // System.out.print("D: "); int bc = 0; byte value = 0; for (int i = 0; i<name.length(); i++) { char code = name.charAt(i); byte index = reverseCodes[code]; for (int mask = decoding_mask; mask>0; mask = mask>>1) { int bit = (index&mask)==0 ? 0 : 1; // System.out.print(bit); value = (byte) ((value<<1)+bit); bc++; if (bc==8) { // System.out.print(" "+value+" "); resultList.add(value); bc = 0; value = 0; } // if } // for } // for byte[] decodedBytes = new byte[resultList.size()]; int i = 0; for (byte b : resultList) { decodedBytes[i++] = b; } // for // System.out.println(""); Cipher decrypter = SecurityUtils.getCipher(getCipherSpec(), Cipher.DECRYPT_MODE, getCredentials(relativePath)); byte[] decryptedBytes = decrypter.doFinal(decodedBytes); // name = new String(decryptedBytes, "UTF-8"); String decryptedName = getDecodedFileName(relativePath, decryptedBytes); decryptionCache.put(relativePath+getSeparator()+name, decryptedName); name = decryptedName; } catch (NoSuchAlgorithmException nsae) { LOG.error("getDecryptedFileName() No Such Algorhithm "+nsae.getLocalizedMessage()); } catch (NoSuchPaddingException nspe) { LOG.error("getDecryptedFileName() No Such Padding "+nspe.getLocalizedMessage()); } catch (InvalidKeyException ike) { LOG.error("getDecryptedFileName() Invalid Key "+ike.getLocalizedMessage()); } catch (ArrayIndexOutOfBoundsException e) { LOG.error("getDecryptedFileName() ArrayIndexOutOfBoundsException", e); } catch (IllegalBlockSizeException e) { LOG.error("getDecryptedFileName() IllegalBlockSizeException", e); } catch (BadPaddingException e) { LOG.error("getDecryptedFileName() BadPaddingException", e); } // try/catch return name; }
From source file:com.otisbean.keyring.Ring.java
/** * Encrypt the given data with our key, prepending saltLength random * characters./* w w w.ja va 2s . co m*/ * @return Base64 encoded representation of the encrypted data. */ String encrypt(String data, int saltLength) throws GeneralSecurityException { log("encrypt()"); try { cipher.init(Cipher.ENCRYPT_MODE, key, iv); } catch (InvalidKeyException ike) { throw new GeneralSecurityException("InvalidKeyException: " + ike.getLocalizedMessage() + "\nYou (probably) need to " + "install the \"Java Cryptography Extension (JCE) " + "Unlimited Strength Jurisdiction Policy\" files. Go to " + "http://java.sun.com/javase/downloads/index.jsp, download them, " + "and follow the instructions."); } String salted = saltString(saltLength, data); byte[] crypted; byte[] saltedBytes; try { saltedBytes = salted.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new GeneralSecurityException(e.getLocalizedMessage()); } crypted = cipher.doFinal(saltedBytes); return Base64.encodeBytes(crypted); }
From source file:com.otisbean.keyring.Ring.java
String decrypt(String cryptext) throws GeneralSecurityException { log("decrypt()"); try {/*from w w w. java 2 s. c o m*/ cipher.init(Cipher.DECRYPT_MODE, key, iv); } catch (InvalidKeyException ike) { throw new GeneralSecurityException("InvalidKeyException: " + ike.getLocalizedMessage() + "\nYou (probably) need to " + "install the \"Java Cryptography Extension (JCE) " + "Unlimited Strength Jurisdiction Policy\" files. Go to " + "http://java.sun.com/javase/downloads/index.jsp, download them, " + "and follow the instructions."); } byte[] crypted; try { crypted = Base64.decode(cryptext); } catch (IOException e) { throw new GeneralSecurityException(e.getLocalizedMessage()); } byte[] decrypted = cipher.doFinal(crypted); String salted; try { salted = new String(decrypted, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new GeneralSecurityException(e.getLocalizedMessage()); } // Remove any leading non-JSON salt characters return salted.replaceAll("^[^\\{]*\\{", "{"); }
From source file:com.qut.middleware.crypto.impl.CryptoProcessorImpl.java
private X509Certificate generateV3Certificate(KeyPair pair, String certSubjectDN, Calendar before, Calendar expiry) throws CryptoException { X509V3CertificateGenerator cert = new X509V3CertificateGenerator(); /* Set the certificate serial number to a random number */ Random rand = new Random(); rand.setSeed(System.currentTimeMillis()); /* Generates a number between 0 and 2^32 as the serial */ BigInteger serial = BigInteger.valueOf(rand.nextInt(Integer.MAX_VALUE)); logger.info("Setting X509 Cert Serial to: " + serial); cert.setSerialNumber(serial);//from w ww . j a v a 2s. c o m /* Set the certificate issuer */ cert.setIssuerDN(new X500Principal(this.certIssuerDN)); /* Set the start of valid period. */ cert.setNotBefore(before.getTime()); /* Set the certificate expiry date. */ cert.setNotAfter(expiry.getTime()); /* Set the subject */ cert.setSubjectDN(new X500Principal(certSubjectDN)); cert.setPublicKey(pair.getPublic()); /* Signature algorithm, this may need to be changed if not all hosts have SHA256 and RSA implementations */ cert.setSignatureAlgorithm("SHA512withRSA"); cert.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(false)); /* Only for signing */ cert.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyCertSign)); cert.addExtension(X509Extensions.ExtendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth)); /* Set a contact email address for the issuer */ cert.addExtension(X509Extensions.SubjectAlternativeName, false, new GeneralNames(new GeneralName(GeneralName.rfc822Name, this.certIssuerEmail))); logger.debug("Generating X509Certificate for key pair: " + pair); try { /* Use the BouncyCastle provider to actually generate the X509Certificate now */ return cert.generateX509Certificate(pair.getPrivate(), "BC"); } catch (InvalidKeyException e) { this.logger.error("InvalidKeyException thrown, " + e.getLocalizedMessage()); this.logger.debug(e.toString()); throw new CryptoException(e.getLocalizedMessage(), e); } catch (NoSuchProviderException e) { this.logger.error("NoSuchProviderException thrown, " + e.getLocalizedMessage()); this.logger.debug(e.toString()); throw new CryptoException(e.getLocalizedMessage(), e); } catch (SecurityException e) { this.logger.error("SecurityException thrown, " + e.getLocalizedMessage()); this.logger.debug(e.toString()); throw new CryptoException(e.getLocalizedMessage(), e); } catch (SignatureException e) { this.logger.error("SignatureException thrown, " + e.getLocalizedMessage()); this.logger.debug(e.toString()); throw new CryptoException(e.getLocalizedMessage(), e); } }