Rya*_*ach 5 java cryptography rsa jwt
https://github.com/auth0/java-jwt
指出为 JWT 设置算法应该像下面一样简单
//RSA
RSAPublicKey publicKey = //Get the key instance
RSAPrivateKey privateKey = //Get the key instance
Algorithm algorithmRS = Algorithm.RSA256(publicKey, privateKey);
Run Code Online (Sandbox Code Playgroud)
问题是我无法弄清楚如何在不接触文件系统的情况下创建 RSAPublicKey 和 RSAPrivateKey 实例。
通常这是我会猜测的事情,直到我猜对为止,但考虑到它是密码学,我想做正确的事情。
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
Run Code Online (Sandbox Code Playgroud)
您可以直接将公钥和私钥转换为RSAPublicKey和RSAPrivateKey因为您使用的是 RSA KeyPairGenerator
RSAPublicKey rsaPublicKey = (RSAPublicKey) keypair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keypair.getPrivate();
Run Code Online (Sandbox Code Playgroud)
key.getEncoded();您可以使用(无需强制转换)获取关键内容并将其存储为字节数组,以您喜欢的方式
| 归档时间: |
|
| 查看次数: |
2399 次 |
| 最近记录: |