2014-02-20 58 views
0

以下代碼將test.cer文件(即X509證書)加載到內存中。當它現在在內存中時,可以修改它的字段嗎?很容易輸出任何字段,例如load.getPublicKey(),但我想更改公鑰的第一個字節,並在更改後再次輸出。我會怎麼做?如何加載和修改.cer文件到內存中

File f= new File("Users/Desktop/JavaFolder/test.cer");  
CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); 
while (in.available() > 0) { 
    Certificate load = cf.generateCertificate(in); 
} 
in.close(); 
+3

鑑於證書是什麼,這似乎是一個奇怪的要求,你能解釋你的用例嗎? – Henry

+0

主要用例是知道如何在將證書內容上傳到存儲器後修改證書內容,爲什麼我想出了公鑰示例。我想知道這一點,因爲我需要顯示類似於簽名驗證的內容,因爲證書中的任何更改使得驗證不可行 – user3334067

+0

可能的重複:http://stackoverflow.com/questions/21917034/modifying-x509-證書 – Henry

回答

0

後你有Certificate

Certificate cert = .... 
PublicKey publicKey = cert.getPublicKey(); 
byte[] originalPublicKey = publicKey.getEncoded(); 
byte[] modifiedPublicKey = java.util.Arrays.copyOf(originalPublicKey , originalPublicKey .length);// make a copy 
modifiedPublicKey[0] = !modifiedPublicKey[0]; // modify something 
print(originalPublicKey); // your "print" method - implement it how you like it e.g., Arrays.toString(originalPublicKey) 
print(modifiedPublicKey); // your "print" method - implement it how you like it e.g., Arrays.toString(modifiedPublicKey) 

見的Javadoc參考