Last active
January 14, 2025 11:06
-
-
Save t0in4/be864031076884e6c4da0045a97f0ddf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.bouncycastle.jce.provider.BouncyCastleProvider; | |
import org.bouncycastle.util.io.pem.PemObject; | |
import org.bouncycastle.util.io.pem.PemReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.Reader; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.security.KeyFactory; | |
import java.security.PrivateKey; | |
import java.security.Security; | |
import java.security.spec.PKCS8EncodedKeySpec; | |
import java.util.Arrays; | |
public class Jwt { | |
public static class KeyInfo { | |
public String id; | |
public String service_account_id; | |
public String private_key; | |
} | |
public static void main(String[] args) throws Exception { | |
Security.addProvider(new BouncyCastleProvider()); | |
String content = new String(Files.readAllBytes(Paths.get("/home/codecraftman/IdeaProjects/YandexTranslator/keys.json"))); | |
JavaJwt.KeyInfo keyInfo = (new ObjectMapper()).readValue(content, JavaJwt.KeyInfo.class); | |
// преобразовать строку в файл - start | |
Path filePath = Paths.get("/home/codecraftman/IdeaProjects/YandexTranslator/file.txt"); | |
//Files.writeString(filePath, privateKeyString, StandardCharsets.UTF_8); | |
File file = new File(filePath.toString()); | |
Reader reader0 = new FileReader(file); | |
// end | |
PemReader reader1 = new PemReader(reader0); | |
PemObject privateKeyPem = reader1.readPemObject(); | |
byte[] keyBytes = privateKeyPem.getContent(); | |
System.out.println(Arrays.toString(keyBytes)); | |
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
PrivateKey privateKey = keyFactory.generatePrivate(keySpec); | |
System.out.println("Private Key: " + privateKey); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment