List of usage examples for javax.crypto Cipher DECRYPT_MODE
int DECRYPT_MODE
To view the source code for javax.crypto Cipher DECRYPT_MODE.
Click Source Link
From source file:hudson.model.UsageStatisticsTest.java
/** * Makes sure that the stat data can be decrypted safely. *//* www . j av a 2s . c om*/ public void testRoundtrip() throws Exception { // key pair for testing String privateKey = "30820276020100300d06092a864886f70d0101010500048202603082025c0201000281810084cababdb38040f659c2cb07a36d758f46e84ebc3d6ba39d967aedf1d396b0788ed3ab868d45ce280b1102b434c2a250ddc3254defe1785ab4f94d7038cf69ecca16753d2de3f6ad8976b3f74902d8634111d730982da74e1a6e3fc0bc3523bba53e45b8a8cbfd0321b94efc9f7fefbe66ad85281e3d0323d87f4426ec51204f0203010001028180784deaacdea8bd31f2d44578601954be3f714b93c2d977dbd76efb8f71303e249ad12dbeb2d2a1192a1d7923a6010768d7e06a3597b3df83de1d5688eb0f0e58c76070eddd696682730c93890dc727564c65dc8416bfbde5aad4eb7a97ed923efb55a291daf3c00810c0e43851298472fd539aab355af8cedcf1e9a0cbead661024100c498375102b068806c71dec838dc8dfa5624fb8a524a49cffadc19d10689a8c9c26db514faba6f96e50a605122abd3c9af16e82f2b7565f384528c9f31ea5947024100aceafd31d7f4872a873c7e5fe88f20c2fb086a053c6970026b3ce364768e2033100efb1ad8f2010fe53454a29decedc23a8a0c8df347742b1f13e11bd3a284b9024100931321470cd0f6cd24d4278bf8e61f9d69b6ef2bf3163a944aa340f91c7ffdf33aeea22b18cc43514af6714a21bb148d6cdca14530a8fa65acd7a8f62bfc9b5f024067452059f8438dc61466488336fce3f00ec483ad04db638dce45daf850e5a8cd5635dc39b87f2fab32940247ec5167ddabe06e870858104500967ac687aa73e102407e3b7997503e18d8d0f094d5e0bd5d57cb93cb39a2fc42cec1ea9a1562786438b61139e45813204d72c919f5397e139ad051d98e4d0f8a06d237f42c0d8440fb"; String publicKey = "30819f300d06092a864886f70d010101050003818d003081890281810084cababdb38040f659c2cb07a36d758f46e84ebc3d6ba39d967aedf1d396b0788ed3ab868d45ce280b1102b434c2a250ddc3254defe1785ab4f94d7038cf69ecca16753d2de3f6ad8976b3f74902d8634111d730982da74e1a6e3fc0bc3523bba53e45b8a8cbfd0321b94efc9f7fefbe66ad85281e3d0323d87f4426ec51204f0203010001"; String data = new UsageStatistics(publicKey).getStatData(); System.out.println(data); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey priv = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Util.fromHexString(privateKey))); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, priv); byte[] cipherText = Base64.decode(data.toCharArray()); InputStreamReader r = new InputStreamReader( new GZIPInputStream( new CombinedCipherInputStream(new ByteArrayInputStream(cipherText), cipher, "AES", 1024)), "UTF-8"); JSONObject o = JSONObject.fromObject(IOUtils.toString(r)); System.out.println(o); assertEquals(1, o.getInt("stat")); }
From source file:net.navasoft.madcoin.backend.services.security.Encrypter.java
/** * Decrypt.//from ww w .j ava 2 s. c o m * * @param value * the value * @return the string * @throws BadPaddingException * the bad padding exception * @since 2/09/2014, 03:41:07 AM */ public String decrypt(String value) throws BadPaddingException { try { Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "SunJCE"); dcipher.init(Cipher.DECRYPT_MODE, key, iv); if (value == null) return null; // Decode base64 to get bytes byte[] dec = Base64.decodeBase64(value.getBytes()); // Decrypt byte[] utf8 = dcipher.doFinal(dec); // Decode using utf-8 return new String(utf8, "UTF8"); } catch (Exception e) { if (e instanceof BadPaddingException) { throw (BadPaddingException) e; } else { e.printStackTrace(); } } return null; }
From source file:net.sf.vfsjfilechooser.accessories.bookmarks.BookmarksReader.java
public BookmarksReader(File bookmarksFile) { entries = new ArrayList<TitledURLEntry>(); Reader reader = null;//from w w w . j a va 2 s .c om try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); xmlReader.setContentHandler(new BookmarksHandler()); reader = new BufferedReader(new InputStreamReader(new FileInputStream(bookmarksFile), "UTF-8")); // read 1st 2 bytes to support multiple encryptions char[] code = new char[2]; reader.read(code, 0, 2); LOGGER.debug("code=" + String.valueOf(code) + "="); if ((code[0] == 'b') && (code[1] == '1')) { LOGGER.debug("in encrypted code section"); // read the encrypted file InputStream is = new FileInputStream(bookmarksFile); int the_length = (int) bookmarksFile.length() - 2; LOGGER.debug("raw_length=" + (the_length + 2)); if (the_length <= 0) the_length = 1; LOGGER.debug("fixed_length=" + the_length); byte[] code2 = new byte[2]; byte[] outhex = new byte[the_length]; try { is.read(code2); is.read(outhex); // is.read(outhex,2,the_length); is.close(); } catch (Exception e) { LOGGER.info("exception reading encrypted file" + e); } finally { IOUtils.closeQuietly(is); } byte[] out = Util.hexByteArrayToByteArray(outhex); // do the decryption byte[] raw = new byte[16]; raw[0] = (byte) 1; raw[2] = (byte) 23; raw[3] = (byte) 24; raw[4] = (byte) 2; raw[5] = (byte) 99; raw[6] = (byte) 200; raw[7] = (byte) 202; raw[8] = (byte) 209; raw[9] = (byte) 199; raw[10] = (byte) 181; raw[11] = (byte) 255; raw[12] = (byte) 33; raw[13] = (byte) 210; raw[14] = (byte) 214; raw[15] = (byte) 216; SecretKeySpec skeyspec = new SecretKeySpec(raw, "Blowfish"); Cipher cipher = Cipher.getInstance("Blowfish"); cipher.init(Cipher.DECRYPT_MODE, skeyspec); byte[] decrypted = cipher.doFinal(out); // convert decrypted into a bytestream and parse it ByteArrayInputStream bstream = new ByteArrayInputStream(decrypted); InputSource inputSource = new InputSource(bstream); xmlReader.parse(inputSource); LOGGER.debug("leaving encrypted code section"); } else { LOGGER.debug("in decrypted code section"); reader = new BufferedReader(new InputStreamReader(new FileInputStream(bookmarksFile), "UTF-8")); InputSource inputSource = new InputSource(reader); xmlReader.parse(inputSource); LOGGER.debug("leaving decrypted code section"); } } catch (SAXParseException e) { StringBuilder sb = new StringBuilder(); sb.append("Error parsing xml bookmarks file").append("\n").append(e.getLineNumber()).append(":") .append(e.getColumnNumber()).append("\n").append(e.getMessage()); throw new RuntimeException(sb.toString(), e); } catch (FileNotFoundException e) { throw new RuntimeException("Bookmarks file doesn't exist!", e); } catch (Exception e) { throw new RuntimeException(e); } finally { if (reader != null) { try { reader.close(); } catch (IOException ioe) { LOGGER.error("Unable to close bookmarks stream", ioe); } } } }
From source file:com.alliander.osgp.shared.security.EncryptionService.java
/** * Decrypts the data using the key, Strips off iv bytes when they are there * (first 16 0 bytes).//from w ww. jav a2 s . c om */ public byte[] decrypt(final byte[] inputData) { try { final Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER); cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(IVBYTES)); final byte[] decryptedData = cipher.doFinal(inputData); if (this.checkNullBytesPrepended(decryptedData)) { return Arrays.copyOfRange(decryptedData, IVBYTES.length, decryptedData.length); } else { return decryptedData; } } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchProviderException | InvalidAlgorithmParameterException ex) { LOGGER.error(UNEXPECTED_EXCEPTION_DURING_DECRYPTION, ex); throw new EncrypterException("Unexpected exception during decryption!", ex); } }
From source file:com.google.feedserver.util.EncryptionUtil.java
/** * Decrypts the given encrypted string and returns the original value * //from w ww. j ava 2 s .com * @param encryptedValue The encrypted string * @return The decrypted value * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException * @throws DecoderException * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException */ public String decrypt(String encryptedValue) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, DecoderException, NoSuchAlgorithmException, NoSuchPaddingException { byte[] encryptionBytes = Hex.decodeHex(encryptedValue.toCharArray()); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, key); byte[] recoveredBytes = cipher.doFinal(encryptionBytes); String recovered = new String(recoveredBytes); return recovered; }
From source file:net.alegen.datpass.library.crypto.CryptoManager.java
public String decrypt(EncryptionOutput encryptionOutput, String password) { final String ctext = encryptionOutput.getCypherText(); final String salt = encryptionOutput.getSalt(); final String iv = encryptionOutput.getIV(); try {/*from w w w . jav a 2 s .c o m*/ // set up the decryption key byte[] saltBytes = Base64.decodeBase64(salt.getBytes("UTF-8")); SecretKey key = this.derivateKey(KeyDerivationFunctions.PBKDF2_HMAC_SHA1, password, saltBytes, AES_KEY_LENGTH, DEFAULT_ITERATIONS); key = new SecretKeySpec(key.getEncoded(), "AES"); // decrypt cipher text with AES using generated key byte[] cipherBytes = Base64.decodeBase64(ctext.getBytes("UTF-8")); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iVBytes = Base64.decodeBase64(iv.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iVBytes)); byte[] plaintext = cipher.doFinal(cipherBytes); // return plaintext return new String(plaintext, "UTF-8"); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException | InvalidAlgorithmParameterException e) { log.error("An error occured while decrypting the message."); return null; } }
From source file:pl.psnc.synat.wrdz.common.https.HttpsClientHelper.java
/** * Gets HTTPS client that can authenticate in WRDZ modules. * /*from w w w . ja v a 2 s.co m*/ * @param module * module that wants to be authenticated * @return HTTPS client */ public synchronized HttpClient getHttpsClient(WrdzModule module) { DefaultHttpClient httpClient = httpsClients.get(module); if (httpClient == null) { logger.debug("HTTPS client for module " + module.name() + " is not yet initialized"); try { SSLSocketFactory socketFactory; if (config.getHttpsVerifyHostname()) { socketFactory = new SSLSocketFactory(new TrustAllStrategy()); } else { socketFactory = new SSLSocketFactory(new TrustAllStrategy(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } Scheme scheme = new Scheme("https", 443, socketFactory); PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); connectionManager.getSchemeRegistry().register(scheme); String cipher = config.getModulesPassword(); byte[] key = SECRET.getBytes("utf-8"); Cipher c = Cipher.getInstance("AES"); SecretKeySpec k = new SecretKeySpec(key, "AES"); c.init(Cipher.DECRYPT_MODE, k); byte[] decrypted = c.doFinal(Base64.decodeBase64(cipher)); String password = new String(decrypted, "utf-8"); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(module.name(), password); httpClient = new DefaultHttpClient(connectionManager); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); httpsClients.put(module, httpClient); } catch (Exception e) { throw new WrdzRuntimeException(e.getMessage(), e); } } return httpClient; }
From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java
/** * Description ?/*from ww w.j a v a2s.c o m*/ * * @param data * @param key byte * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws GeneralSecurityException { // ???? SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
From source file:com.lightszentip.module.security.password.PasswordModuleImpl.java
/** * {@inheritDoc}// ww w .ja va 2 s .c o m */ @Override public String decrypt(String value, String key, EncryptionType type) { return crypt(value, key, type, Cipher.DECRYPT_MODE); }
From source file:org.openmrs.module.clinicalsummary.io.UploadSummariesTask.java
/** * Method to initialize the cipher object with the correct encryption algorithm. * * @throws Exception/*from w ww.jav a 2 s . c o m*/ */ protected void initializeCipher() throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance(TaskConstants.SECRET_KEY_FACTORY); KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1024, 128); SecretKey tmp = factory.generateSecret(spec); if (log.isDebugEnabled()) log.debug("Secret Key Length: " + tmp.getEncoded().length); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), TaskConstants.KEY_SPEC); cipher = Cipher.getInstance(TaskConstants.CIPHER_CONFIGURATION); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(initVector)); }