如何生成与com.auth0 java-jwt一起使用的RSA密钥?

问题描述:

https://github.com/auth0/java-jwt如何生成与com.auth0 java-jwt一起使用的RSA密钥?

国家,设立了智威汤逊的算法应该是越简单

//RSA 
RSAPublicKey publicKey = //Get the key instance 
RSAPrivateKey privateKey = //Get the key instance 
Algorithm algorithmRS = Algorithm.RSA256(publicKey, privateKey); 

的问题是我不知道如何不触及文件系统创建一个RSAPublicKey和RSAPrivateKey实例。

  1. 它应该是安全的。
  2. 它不应该在文件系统上创建密钥,因为我打算通过另一种方法来存储密钥。

通常情况下,这是我猜想的事情,直到我得到正确的,但考虑到它的密码学我想做正确的事情。

keygen = KeyPairGenerator.getInstance("RSA"); 
     RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4); //What does F4 mean vs F0? 
      keygen.initialize(spec); 
      KeyPair keypair = keygen.generateKeyPair(); 
      PublicKey pub = keypair.getPublic(); //Wrong type, need RSAPublicKey 
      PrivateKey priv = keypair.getPrivate(); //Wrong type, need RSAPrivateKey 

您可以直接浇铸的公钥和私钥对RSAPublicKeyRSAPrivateKey,因为你使用的是RSA的KeyPairGenerator

RSAPublicKey rsaPublicKey = (RSAPublicKey) keypair.getPublic(); 
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keypair.getPrivate(); 

可以开始使用key.getEncoded();的重点内容(无投需要)你并储存起来作为字节数组您喜欢的任何方式

+0

您知道F0和F4指数的用途吗? –

+0

我认为F0(3)是'RSAKeyPairGenerator'使用的公共指数的最小值,F4(65537)是RFC的推荐值。请参阅https://crypto.stackexchange.com/questions/3110/impacts-of-not-using-rsa-exponent-of-65537 – pedrofb