2014-01-10 36 views
1

我想檢查是否一個.jar文件是用C#正確簽名驗證的.jar Java小程序的簽名。我研究了一下,但無法找到一個方法來檢查它(很像jarsigner一樣)。我如何使用C#

我試圖讀取文件的內容,並已成功地從清單和.sf文件中獲得* -digest字符串,但如果我無法驗證它們,這並不能真正讓我到任何地方正確的簽名。

我知道這是一個相當奇怪的問題,但任何幫助,將不勝感激。

在此先感謝!

+1

嘗試調用'jarsigner'並解析它的輸出。或者也許有一個直接的API。 –

回答

1

上面確實似乎是最好的辦法的意見,那就是從C#中調用的jarsigner作爲外部進程。所以讓我給你一些代碼。

using System; 
using System.Diagnostics; 

public class VerifyJar 
{ 
    public static void Main() 
    { 
     Process p = new Process(); 
     p.StartInfo.FileName = "jarsigner"; // put in full path 
     p.StartInfo.Arguments = "-verify liblinear-1.92.jar"; // put in your jar file 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.Start(); 

     string output = p.StandardOutput.ReadToEnd(); 
     p.WaitForExit(); 

     // Handle the output with a string check probably yourself 
     // Here I just display what the result for debugging purposes 
     Console.WriteLine("Output:"); 
     Console.WriteLine(output); 

     // For me, the output is "jar is unsigned. (signature missing or not parsable)" 
     // which is correct for this particular jar file. 
    } 
}