2010-06-01 44 views
1

下面的代碼隨機打印634,635,636,每次運行它。爲什麼它不是恆定的?爲什麼生成的密鑰大小不是恆定的?

public static void main(String[] args) throws Exception { 
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC"); 
    keyPairGen.initialize(1024); 
    RsaKeyPair keyPair = new RsaKeyPair(keyPairGen.generateKeyPair()); 
    System.out.println(keyPair.getPrivate().getEncoded().length); 
} 

回答

4

正如我在其他線程所解釋的,getEncoded()返回DER編碼的ASN.1對象。在BER和DER編碼中,整數用可變數量的八位字節編碼。 DER和BER之間的主要區別在於DER需要最短的編碼。

RSAPrivateKey對象中有幾個素數,它們可能不佔用完整的1024位,並且會導致DER編碼中的大小差異。

相關問題