質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

2575閲覧

javaでの難読化の復号化

santora

総合スコア11

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/07/10 08:04

javaで暗号化ツールを仲間からもらったのですがどういう仕組みで動いているかがわかりません。
この場合どのようにjar.datを作成、またjar.datの中身を戻してXXX.jarに保存する手法を教えてください

java

1package jcrypt; 2 3import java.io.InputStream; 4import java.lang.reflect.Method; 5import java.security.Key; 6import java.util.jar.JarInputStream; 7import java.util.zip.ZipEntry; 8import java.util.zip.ZipFile; 9import javax.crypto.Cipher; 10import javax.crypto.CipherInputStream; 11import javax.crypto.spec.IvParameterSpec; 12import javax.crypto.spec.SecretKeySpec; 13 14public class Main 15{ 16 public static final String ENCRYPTED_ARCHIVE = "/jar.dat"; 17 18 public static void main(String[] args) 19 throws Exception 20 { 21 ZipFile zip = new ZipFile(Utils.getJarFile()); 22 23 ZipEntry e = zip.getEntry("jar.dat"); 24 byte[] extra = e.getExtra(); 25 26 byte[] key = new byte[16]; 27 System.arraycopy(extra, 0, key, 0, 16); 28 29 byte[] iv = new byte[16]; 30 System.arraycopy(extra, 16, iv, 0, 16); 31 32 boolean cryptAll = extra[32] == 1; 33 34 byte[] bMainClass = new byte[extra.length - 33]; 35 System.arraycopy(extra, 33, bMainClass, 0, extra.length - 33); 36 String mainClass = new String(bMainClass); 37 38 zip.close(); 39 40 InputStream resource = Main.class.getResourceAsStream("/jar.dat"); 41 42 Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING"); 43 44 Key sks = new SecretKeySpec(key, "AES"); 45 cipher.init(2, sks, new IvParameterSpec(iv)); 46 47 JarInputStream jarInputStream = new JarInputStream(new CipherInputStream(resource, cipher)); 48 EncryptedClassLoader classLoader = new EncryptedClassLoader(Main.class.getClassLoader(), jarInputStream, cryptAll); 49 50 Class<?> classToLoad = classLoader.loadClass(mainClass); 51 Method method = classToLoad.getMethod("main", new Class[] { String[].class }); 52 53 method.invoke(classToLoad.newInstance(), new Object[] { args }); 54 55 jarInputStream.close(); 56 } 57}

java

1package jcrypt; 2 3import java.io.File; 4import java.net.URL; 5import java.security.CodeSource; 6import java.security.ProtectionDomain; 7 8public class Utils 9{ 10 public static File getJarFile() 11 { 12 return new File(Utils.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace("file:", "")); 13 } 14 15 public static String getClassName(String fileName) 16 { 17 return fileName.substring(0, fileName.length() - 6).replace('/', '.'); 18 } 19}

java

1package jcrypt; 2 3import java.io.ByteArrayInputStream; 4import java.io.ByteArrayOutputStream; 5import java.io.IOException; 6import java.io.InputStream; 7import java.net.URL; 8import java.util.Enumeration; 9import java.util.HashMap; 10import java.util.jar.JarEntry; 11import java.util.jar.JarInputStream; 12 13public class EncryptedClassLoader 14 extends ClassLoader 15{ 16 private final HashMap<String, byte[]> classes = new HashMap(); 17 private final HashMap<String, byte[]> others = new HashMap(); 18 private final boolean encryptResources; 19 20 public EncryptedClassLoader(ClassLoader parent, JarInputStream stream, boolean encryptResources) 21 { 22 super(parent); 23 loadResources(stream); 24 this.encryptResources = encryptResources; 25 } 26 27 public InputStream getResourceAsStream(String name) 28 { 29 if (this.encryptResources) 30 { 31 byte[] buffer = (byte[])this.others.get(name); 32 if (buffer != null) { 33 return new ByteArrayInputStream(buffer); 34 } 35 } 36 return super.getResourceAsStream(name); 37 } 38 39 public URL getResource(String name) 40 { 41 if (this.encryptResources) { 42 throw null; 43 } 44 return super.getResource(name); 45 } 46 47 protected Enumeration<URL> findResources(String name) 48 throws IOException 49 { 50 if (this.encryptResources) { 51 throw new IOException("Cant get URL from resource in memory"); 52 } 53 return super.findResources(name); 54 } 55 56 public int hashCode() 57 { 58 return getParent().hashCode(); 59 } 60 61 public Class<?> findClass(String name) 62 throws ClassNotFoundException 63 { 64 byte[] data = getClassData(name); 65 if (data != null) { 66 return defineClass(name, data, 0, data.length, Main.class.getProtectionDomain()); 67 } 68 throw new ClassNotFoundException(name); 69 } 70 71 public void loadResources(JarInputStream stream) 72 { 73 byte[] buffer = new byte['?']; 74 try 75 { 76 JarEntry entry = null; 77 while ((entry = stream.getNextJarEntry()) != null) 78 { 79 ByteArrayOutputStream out = new ByteArrayOutputStream(); 80 int count; 81 while ((count = stream.read(buffer)) != -1) 82 { 83 int count; 84 out.write(buffer, 0, count); 85 } 86 out.close(); 87 88 byte[] array = out.toByteArray(); 89 if (entry.getName().toLowerCase().endsWith(".class")) { 90 this.classes.put(Utils.getClassName(entry.getName()), array); 91 } else if (this.encryptResources) { 92 this.others.put(entry.getName(), array); 93 } 94 } 95 } 96 catch (IOException e) 97 { 98 e.printStackTrace(); 99 } 100 } 101 102 public boolean equals(Object o) 103 { 104 if ((o instanceof EncryptedClassLoader)) { 105 return ((EncryptedClassLoader)o).getParent() == getParent(); 106 } 107 return false; 108 } 109 110 public byte[] getClassData(String name) 111 { 112 byte[] b = (byte[])this.classes.get(name); 113 this.classes.remove(name); 114 return b; 115 } 116}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2018/07/10 08:12

そのお仲間に聞けば良いのでは?このコードを全て解読して解説するのは何も知らない人がするには負担が大きすぎます。ご自身で何も調べてないのでしょうか?
santora

2018/07/10 08:19

試行錯誤して試してみたのですがわからなかったので、質問させていただきました。
guest

回答1

0

ベストアンサー

仲間の人が一番知ってると思いますが……
1,package名「jcrypt」でググるとjcryptがHITします。

jCrypt
Includes wizard for building your file (Check releases, jCrypt.jar). Bin.jar has to be in the working directory of jCrypt.jar

Encrypts your input archive and writes it to a copy of Bin.jar, and sets encryption key, initialization vector, encrypt resources and mainclass in the extra field. When ran, it will load the information in the extra field, decrypt and load the entry containing your encrypted JAR and invoke it.

2点重要なことが英文で書かれています。つ Google 翻訳

Includes wizard for building your file (Check releases, jCrypt.jar). Bin.jar has to be in the working directory of jCrypt.jar

2,jcrypt releasesからBin.jarjCrypt.jarDownloadし、jCrypt.jarを実行します。

Encrypts your input archive and writes it to a copy of Bin.jar, and sets encryption key, initialization vector, encrypt resources and mainclass in the extra field. When ran, it will load the information in the extra field, decrypt and load the entry containing your encrypted JAR and invoke it.

3,Javaの逆コンパイラはluytenを使うのがいいでしょう。
これもreleasesからluyten-0.5.3.exeをDownloadして実行します。使い方はググってください。

◇ものすごく重要な点
IV(初期化ベクトル)の生成SecureRandomを使っていないので、その点は気をつけてください。

◇参考情報
初期化ベクトル
javax.crypto.Cipher


まず質問文のプログラムは復号を行うプログラムです。
暗号化部分はこのソースリンク先のソースコードを一行の意味が分かるまで読んでください。

まず暗号化後のjarファイルの中にエントリとしてjar.datがあります。
jar.datExtra領域にKey,IV,encall(暗号化リソース)フラグ,メインクラス名が記述されています。
jarファイルを解凍してjar.datを取り出しあとは質問文のコードを参考にjar化してください。

投稿2018/07/10 08:55

編集2018/07/10 11:29
umyu

総合スコア5846

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

santora

2018/07/10 09:11

jCryptのDecryptはどうすればいいのでしょうか。
umyu

2018/07/10 09:18

>jCryptのDecryptはどうすればいいのでしょうか。 コメントの意味が理解できないのですが…… luytenの使い方はぐぐりましたか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問