尝试了解Java RSA密钥大小 [英] Trying to understand Java RSA key size

查看:113
本文介绍了尝试了解Java RSA密钥大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

密钥生成器的大小为1024,因此为什么印刷尺寸为635和162?

The key generator was initilized with a size of 1024, so why the printed sizes are 635 and 162?

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class TEST {

    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGenerator.initialize(1024);
    return keyPairGenerator.generateKeyPair();
    }

    public static void main(String[] args) throws Exception {

    KeyPair keyPair = generateKeyPair();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

    System.out.println("Size = " + privateKey.getEncoded().length);
    System.out.println("Size = " + publicKey.getEncoded().length);

    }

}


推荐答案

RSA键由模数和指数组成。密钥大小是指模数中的比特数。所以即使没有任何编码开销,您将需要超过128个字节来存储1024位密钥。

RSA keys are made of Modulus and Exponent. The key size refers to the bits in modulus. So even without any encoding overhead, you will need more than 128 bytes to store 1024-bit keys.

getEncoded()返回ASN.1 DER编码对象。私钥甚至包含CRT参数,因此它非常大。

getEncoded() returns ASN.1 DER encoded objects. The private key even contains CRT parameters so it's very large.

要获得密钥大小,请执行此操作

To get key size, do something like this,

   System.out.println("Key size = " + publicKey.getModulus().bitLength());

以下是相关的ASN.1对象,

Here are the relevant ASN.1 objects,

  RSAPrivateKey ::= SEQUENCE {
      version           Version,
      modulus           INTEGER,  -- n
      publicExponent    INTEGER,  -- e
      privateExponent   INTEGER,  -- d
      prime1            INTEGER,  -- p
      prime2            INTEGER,  -- q
      exponent1         INTEGER,  -- d mod (p-1)
      exponent2         INTEGER,  -- d mod (q-1)
      coefficient       INTEGER,  -- (inverse of q) mod p
      otherPrimeInfos   OtherPrimeInfos OPTIONAL
  }


  RSAPublicKey ::= SEQUENCE {
      modulus           INTEGER,  -- n
      publicExponent    INTEGER   -- e
  }

这篇关于尝试了解Java RSA密钥大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆