我正在尝试创建一个使用 ECDSA 密钥进行身份验证的应用程序,但我在密钥生成和读取方面遇到了一些问题。当我向身份验证控制器发出请求时,它向我显示了这样的异常:
java.security.InvalidKeyException: IOException : algid parse error, not a sequence
这就是我所做的:
$ openssl ecparam -name secp256k1 -genkey -noout -out private_key.pem
然后我得到了这个pem密钥:
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIMdy2DTdd7ZF/oVjd69ddslzyy1GsBSuehz0uEGVYtk5oAcGBSuBBAAK
oUQDQgAEnZaAiTxwX93hzPMs4+VVJ1tGK1wv6SWN4Ac/59fQx6bBY0MO6VTzofna
gomVhx/xcyu7KQVmNVTgW51w7BSfNg==
-----END EC PRIVATE KEY-----
RSAService.java
public class RSAService {
public static PrivateKey getPrivateKey(String fileName)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
return getPrivateKeyFromString(getKey(fileName));
}
private static String getKey(String fileName) throws IOException {
StringBuilder strKeyPem = new StringBuilder();
String line;
try (BufferedReader bf = new BufferedReader(new FileReader(fileName))) {
while ((line = bf.readLine()) != null) {
strKeyPem.append(line).append("\n");
}
}
return strKeyPem.toString();
}
private static PrivateKey getPrivateKeyFromString(String key)
throws NoSuchAlgorithmException, InvalidKeySpecException {
String privateKeyPem = key;
privateKeyPem = privateKeyPem.replace("-----BEGIN EC PRIVATE KEY-----", "");
privateKeyPem = privateKeyPem.replace("-----END PRIVATE KEY-----", "");
privateKeyPem = privateKeyPem.replace("\n", "");
byte[] encoded = Base64.decodeBase64(privateKeyPem);
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
return kf.generatePrivate(keySpec); //Here's exception is throwing
}
}
JWTService.java
@Service
public class JWTService {
public String extractUsername(String token)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
return extractClaim(token, Claims::getSubject);
}
public <T> T extractClaim(String token, Function<Claims, T> claimResolver)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
final Claims claims = extractAllClaims(token);
return claimResolver.apply(claims);
}
public String generateToken(UserDetails userDetails)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
return generateToken(new HashMap<>(), userDetails);
}
public String generateToken(Map<String, Object> claims, UserDetails userDetails)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
PrivateKey privateKey = RSAService.getPrivateKey(PathConfig.PRIVATE_KEY_CONFIG);
return Jwts
.builder()
.setClaims(claims)
.setSubject(userDetails.getUsername())
.signWith(privateKey, SignatureAlgorithm.ES256)
.compact();
}
public boolean isTokenValid(String token, UserDetails userDetails)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
final String username = extractUsername(token);
return username.equals(userDetails.getUsername());
}
private Claims extractAllClaims(String token)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
return Jwts
.parserBuilder()
.setSigningKey(RSAService.getPublicKey(PathConfig.PUBLIC_KEY_CONFIG))
.build()
.parseClaimsJws(token)
.getBody();
}
}
那么问题出在哪里呢?如果您知道,请告诉我,我将非常感激!