The following code loads test.cer file (Which is X509 Certificate) into memory. Is that possible to modify its fields when it is now in the memory, right? It is easy to output any field for example load.getPublicKey() but I want to change the first byte from the public key and output it again after changing. How would I do that?

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();
有帮助吗?

解决方案

After you have the 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)

See the javadoc reference

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top