-
-
Save pawlos/0526837ee936ebbf66a5b878ee71383a to your computer and use it in GitHub Desktop.
Final code to decrypt encrypted file
This file contains hidden or 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
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileOutputStream; | |
| import java.math.BigInteger; | |
| import java.security.AlgorithmParameters; | |
| import java.security.Key; | |
| import java.security.MessageDigest; | |
| import java.security.NoSuchAlgorithmException; | |
| import java.security.SecureRandom; | |
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| import javax.crypto.Cipher; | |
| import javax.crypto.SecretKey; | |
| import javax.crypto.SecretKeyFactory; | |
| import javax.crypto.spec.IvParameterSpec; | |
| import javax.crypto.spec.PBEKeySpec; | |
| import javax.crypto.spec.SecretKeySpec; | |
| public class own { | |
| public static String md5(String paramString) | |
| { | |
| Object localObject = null; | |
| try | |
| { | |
| MessageDigest localMessageDigest = MessageDigest.getInstance("MD5"); | |
| localObject = localMessageDigest; | |
| } | |
| catch (NoSuchAlgorithmException localNoSuchAlgorithmException) | |
| { | |
| for (;;) | |
| { | |
| localNoSuchAlgorithmException.printStackTrace(); | |
| } | |
| } | |
| ((MessageDigest)localObject).update(paramString.getBytes(), 0, paramString.length()); | |
| return new BigInteger(1, ((MessageDigest)localObject).digest()).toString(16); | |
| } | |
| private void encrypt(String key, String fileName) | |
| throws Exception | |
| { | |
| FileInputStream localFileInputStream = new FileInputStream(fileName); | |
| FileOutputStream localFileOutputStream = new FileOutputStream(fileName + ".xxx"); | |
| Object localObject1 = new byte[8]; | |
| new SecureRandom().nextBytes((byte[])localObject1); | |
| Object localObject2 = new FileOutputStream(fileName + ".salt"); | |
| ((FileOutputStream)localObject2).write((byte[])localObject1); | |
| ((FileOutputStream)localObject2).close(); | |
| localObject1 = new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(new PBEKeySpec(key.toCharArray(), (byte[])localObject1, 65536, 256)).getEncoded(), "AES"); | |
| Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
| cipher.init(1, (Key)localObject1); | |
| localObject2 = cipher.getParameters(); | |
| localObject1 = new FileOutputStream(fileName + ".iv"); | |
| ((FileOutputStream)localObject1).write(((IvParameterSpec)((AlgorithmParameters)localObject2).getParameterSpec(IvParameterSpec.class)).getIV()); | |
| ((FileOutputStream)localObject1).close(); | |
| localObject1 = new byte[64]; | |
| for (;;) | |
| { | |
| int i = localFileInputStream.read((byte[])localObject1); | |
| if (i == -1) { | |
| break; | |
| } | |
| localObject2 = cipher.update((byte[])localObject1, 0, i); | |
| if (localObject2 != null) { | |
| localFileOutputStream.write((byte[])localObject2); | |
| } | |
| } | |
| byte[] paramString1 = cipher.doFinal(); | |
| if (paramString1 != null) { | |
| localFileOutputStream.write(paramString1); | |
| } | |
| localFileInputStream.close(); | |
| localFileOutputStream.flush(); | |
| localFileOutputStream.close(); | |
| //DeleteFile(paramString2); | |
| } | |
| private static void decrypt(String key, String fileName) | |
| throws Exception | |
| { | |
| System.out.println("decrypt"); | |
| //salt | |
| Object localObject1 = new FileInputStream(fileName.substring(0, fileName.length() - 4) + ".salt"); | |
| Object localObject2 = new byte[8]; | |
| ((FileInputStream)localObject1).read((byte[])localObject2); | |
| ((FileInputStream)localObject1).close(); | |
| //iv | |
| Object localObject3 = new FileInputStream(fileName.substring(0, fileName.length() - 4) + ".iv"); | |
| localObject1 = new byte[16]; | |
| ((FileInputStream)localObject3).read((byte[])localObject1); | |
| ((FileInputStream)localObject3).close(); | |
| //file | |
| localObject2 = new SecretKeySpec(SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(new PBEKeySpec(key.toCharArray(), (byte[])localObject2, 65536, 256)).getEncoded(), "AES"); | |
| Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
| cipher.init(2, (Key)localObject2, new IvParameterSpec((byte[])localObject1)); | |
| localObject1 = new FileInputStream(fileName); | |
| FileOutputStream paramString2 = new FileOutputStream(fileName + ".decrypted"); | |
| localObject3 = new byte[64]; | |
| for (;;) | |
| { | |
| int i = ((FileInputStream)localObject1).read((byte[])localObject3); | |
| if (i == -1) { | |
| break; | |
| } | |
| localObject2 = cipher.update((byte[])localObject3, 0, i); | |
| if (localObject2 != null) { | |
| paramString2.write((byte[])localObject2); | |
| } | |
| } | |
| byte[] paramString1 = cipher.doFinal(); | |
| if (paramString1 != null) { | |
| paramString2.write(paramString1); | |
| } | |
| ((FileInputStream)localObject1).close(); | |
| paramString2.flush(); | |
| paramString2.close(); | |
| } | |
| public static String getKey() | |
| { | |
| String result = new String(); | |
| for (result = own.md5("867562039629283");; result = own.md5("e21833235b6eef10")) { | |
| return result; | |
| } | |
| } | |
| public static void main(String[] args) throws Exception | |
| { | |
| System.out.println("main"); | |
| own.decrypt(own.getKey(), "IMG_20180101_071505.jpg.xxx"); | |
| System.out.println("done"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment