Created
November 4, 2013 09:51
-
-
Save mbarrben/7300410 to your computer and use it in GitHub Desktop.
java encrypt and decrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MessageDigest digest = MessageDigest.getInstance("SHA"); | |
digest.update(password.getBytes()); | |
SecretKeySpec key = new SecretKeySpec(digest.digest(), 0, 16, "AES"); | |
Cipher c = Cipher.getInstance("AES"); | |
c.init(Cipher.DECRYPT_MODE, key); | |
cis = new CipherInputStream(stream, c); | |
reader = new BufferedReader(new InputStreamReader(cis)); | |
String data = reader.readLine(); | |
array = new JSONArray(data); | |
reader.close(); | |
cis.close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String passphrase = args[0]; | |
MessageDigest digest = MessageDigest.getInstance("SHA"); | |
digest.update(passphrase.getBytes()); | |
SecretKeySpec key = new SecretKeySpec(digest.digest(), 0, 16, "AES"); | |
Cipher c = Cipher.getInstance("AES"); | |
c.init(Cipher.ENCRYPT_MODE, key); | |
CipherOutputStream cos = new CipherOutputStream(new FileOutputStream(args[1]), c); | |
System.out.println("Ciphering... " + args[2]); | |
cos.write(args[2].getBytes()); | |
cos.flush(); | |
cos.close(); | |
c = Cipher.getInstance("AES"); | |
c.init(Cipher.DECRYPT_MODE, key); | |
CipherInputStream cis = new CipherInputStream(new FileInputStream(args[1]), c); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(cis)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment