문제

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