我怎么取消一个罐子?

Jas*_*son 25 java jar jar-signing

有没有办法取消签名java jar文件?我有一些我想在我的开发环境中使用的签名罐子,但我得到安全例外,所以我想取消签名这些罐子,我可以在以后准备部署时签名.

DwB*_*DwB 26

我不知道答案,但这就是我要做的:

  1. 解压缩jar文件或有问题的文件(jar只是拉链)
  2. 在META-INF目录中查找不是MANIFEST-MF的内容.
  3. 删除那些东西.
  4. 打开MANIFEST-MF并删除看起来与签名相关的内容.
  5. rejar.


mar*_*ans 15

要从jar文件中删除签名,请从中删除该META-INF目录.jar文件是一个zip文件,因此在Linux上你可以这样做:

zip -d file.jar 'META-INF/*.SF' 'META-INF/*.RSA'
Run Code Online (Sandbox Code Playgroud)

如果要保存许多jar文件,则以下命令会对当前目录及下面的每个jar文件执行此操作:

find . -name '*.jar' -exec zip -d '{}' 'META-INF/*.SF' 'META-INF/*.RSA' ';'
Run Code Online (Sandbox Code Playgroud)


Hou*_*man 8

我看到答案已被接受,但我认为这可能是有用的:

我已经做了一些事情(部分来自其他帖子)来自动完成任务.
附带没有任何保证,但它适用于我:)
删除签名信息时复制Jar文件.
注意,MANIFEST只留下MAIN部分!

使用javac JarUnsigner.java 创建的.class文件
使用java -cp <class dir> JarUnsigner <inJar> <outJar>

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class JarUnsigner {

  private static final String MANIFEST = "META-INF/MANIFEST.MF";

  public static void main(String[] args){

    if (args.length!=2){
      System.out.println("Arguments: <infile.jar> <outfile.jar>");
      System.exit(1);
    }
    String infile = args[0];
    String outfile = args[1];
    if ((new File(outfile)).exists()){
      System.out.println("Output file already exists:" + outfile);
      System.exit(1);
    }
    try{
      ZipFile zipFile = new ZipFile(infile);
      final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outfile));
      for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
        ZipEntry entryIn = (ZipEntry) e.nextElement();

        if (! exclude_file( entryIn.getName() ) ) {

          /* copy the entry as-is */
          zos.putNextEntry( new ZipEntry( entryIn.getName() ));
          InputStream is = zipFile.getInputStream(entryIn);
          byte[] buf = new byte[1024];
          int len;
          while ((len = (is.read(buf))) > 0) {
            zos.write(buf, 0, len);
          }
          zos.closeEntry();

        } else {

          if (MANIFEST.equals(entryIn.getName())){
            /* if MANIFEST, adjust the entry */
            zos.putNextEntry(new ZipEntry(MANIFEST));

            // manifest entries until first empty line. i.e. the 'MainAttributes' section
            // (this method is used so to keep the formatting exactly the same)
            InputStream mIS = zipFile.getInputStream(entryIn);
            BufferedReader in = new BufferedReader(new InputStreamReader(mIS));
            String line = in.readLine();
            byte[] mNL = "\n".getBytes("UTF-8");
            while( line != null && !line.trim().isEmpty() ) {
              zos.write( line.getBytes("UTF-8"));
              zos.write( mNL );
              line = in.readLine();
            }
            zos.write( mNL );
            zos.closeEntry();

          }else{
            /* else: Leave out the Signature files */
          }

        }

      }
      zos.close();
      System.out.println("Successfully unsigned " + outfile);

    }catch(IOException ex){
      System.err.println("Error for file: " + infile);
      ex.printStackTrace();
      System.exit(1);
    }
  }

  /**
   * Exclude .SF signature file
   * Exclude .RSA and DSA (signed version of .SF file) 
   * Exclude SIG-  files  (unknown sign types for signed .SF file)
   * Exclude Manifest file
   * @param filename
   * @return 
   */
  public static boolean exclude_file(String filename){
    return filename.equals("META-INF/MANIFEST.MF") ||
           filename.startsWith("META-INF/SIG-") || 
           filename.startsWith("META-INF/") && ( filename.endsWith(".SF") || filename.endsWith(".RSA") || filename.endsWith(".DSA") );
  }

}
Run Code Online (Sandbox Code Playgroud)

在ANT中使用以取消一堆罐子,如下所示:

<apply executable="java" dest="${output-dir}"> 
  <arg value="-cp" />
  <arg value="${dev-dir}" />
  <arg value="JarUnsigner" />
  <srcfile/> 
  <targetfile/>
  <fileset dir="${input-dir}" includes="*.jar"/> 
  <mapper type="glob" from="*.jar" to="*.jar"/> <!-- uses "dest"-->
</apply> 
Run Code Online (Sandbox Code Playgroud)