在Java中生成十六进制的公钥和私钥
我想生成十六进制的公钥和私钥。 当前输出是用中文编写的。 我希望公钥和私钥的输出用十六进制写成在Java中生成十六进制的公钥和私钥,java,Java,我想生成十六进制的公钥和私钥。 当前输出是用中文编写的。 我希望公钥和私钥的输出用十六进制写成 // Class public class GenerateKeys { private final KeyPairGenerator keyGen; private KeyPair pair; private PrivateKey privateKey; private PublicKey publicKey; // Constructor public GenerateKeys(int key
// Class
public class GenerateKeys {
private final KeyPairGenerator keyGen;
private KeyPair pair;
private PrivateKey privateKey;
private PublicKey publicKey;
// Constructor
public GenerateKeys(int keylength) throws NoSuchAlgorithmException, NoSuchProviderException {
this.keyGen = KeyPairGenerator.getInstance("RSA"); // Algorithm
this.keyGen.initialize(keylength);
}
public void createKeys() {
this.pair = this.keyGen.generateKeyPair();
this.privateKey = pair.getPrivate();
this.publicKey = pair.getPublic();
}
public PrivateKey getPrivateKey() {
return this.privateKey;
}
public PublicKey getPublicKey() {
return this.publicKey;
}
public void writeToFile(String path, byte[] key) throws IOException {
File f = new File(path);
f.getParentFile().mkdirs();
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(key);
fos.flush();
}
}
// Main
public static void main(String[] args)
{
GenerateKeys gk;
try {
gk = new GenerateKeys(1024);
gk.createKeys();
gk.writeToFile("MyKeys/publicKey",gk.getPublicKey().getEncoded());
gk.writeToFile("MyKeys/privateKey",gk.getPrivateKey().getEncoded());
} catch (NoSuchAlgorithmException | NoSuchProviderException | IOException e) {
System.err.println(e.getMessage());
}
}
}
看起来您并不是在创建一个用中文编写的文件。您似乎正在创建一个所谓的“二进制”文件。这些文件是您的计算机可以理解的,但是当您在文本编辑器中打开它们时,您无法读取它们,因为它们没有任何意义。来自其他语言的符号经常会出现 使用
FileOutputStream
写入byte[]
数组将始终生成二进制文件
要创建一个人类可读的文件并以十六进制显示密钥,您可以用此代码替换writeToFile()
方法
public void writeToFile(String path, byte[] key) throws IOException {
File f = new File(path);
f.getParentFile().mkdirs();
StringBuilder sb = new StringBuilder();
for(byte b: key) {
sb.append(String.format("%02X", b) + " ");
}
try (FileWriter fos = new FileWriter(f)) {
fos.write(sb.toString());
fos.flush();
}
}
这将生成一个文本文件,将
字节[]
数组中的每个键值转换为十六进制。看起来您实际上并不是在创建一个用中文编写的文件。您似乎正在创建一个所谓的“二进制”文件。这些文件是您的计算机可以理解的,但是当您在文本编辑器中打开它们时,您无法读取它们,因为它们没有任何意义。来自其他语言的符号经常会出现
// Class
public class GenerateKeys {
private final KeyPairGenerator keyGen;
private KeyPair pair;
private PrivateKey privateKey;
private PublicKey publicKey;
// Constructor
public GenerateKeys(int keylength) throws NoSuchAlgorithmException, NoSuchProviderException {
this.keyGen = KeyPairGenerator.getInstance("RSA"); // Algorithm
this.keyGen.initialize(keylength);
}
public void createKeys() {
this.pair = this.keyGen.generateKeyPair();
this.privateKey = pair.getPrivate();
this.publicKey = pair.getPublic();
}
public PrivateKey getPrivateKey() {
return this.privateKey;
}
public PublicKey getPublicKey() {
return this.publicKey;
}
public void writeToFile(String path, byte[] key) throws IOException {
File f = new File(path);
f.getParentFile().mkdirs();
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(key);
fos.flush();
}
}
// Main
public static void main(String[] args)
{
GenerateKeys gk;
try {
gk = new GenerateKeys(1024);
gk.createKeys();
gk.writeToFile("MyKeys/publicKey",gk.getPublicKey().getEncoded());
gk.writeToFile("MyKeys/privateKey",gk.getPrivateKey().getEncoded());
} catch (NoSuchAlgorithmException | NoSuchProviderException | IOException e) {
System.err.println(e.getMessage());
}
}
}
使用FileOutputStream
写入byte[]
数组将始终生成二进制文件
要创建一个人类可读的文件并以十六进制显示密钥,您可以用此代码替换writeToFile()
方法
public void writeToFile(String path, byte[] key) throws IOException {
File f = new File(path);
f.getParentFile().mkdirs();
StringBuilder sb = new StringBuilder();
for(byte b: key) {
sb.append(String.format("%02X", b) + " ");
}
try (FileWriter fos = new FileWriter(f)) {
fos.write(sb.toString());
fos.flush();
}
}
这将生成一个文本文件,将
字节[]
数组中的每个键值转换为十六进制。我注意到您删除了我对您的问题所做的编辑。我写这些是为了帮助你,让人们更容易理解你的问题。你能告诉我你为什么把它们拿走吗?@LuminousNutria谢谢。你帮了我。我不记得我移除了任何东西,如果我移除了,那将是错误的,所以请不要以错误的方式移除。再次感谢。我注意到你删除了我对你的问题所做的编辑。我写这些是为了帮助你,让人们更容易理解你的问题。你能告诉我你为什么把它们拿走吗?@LuminousNutria谢谢。你帮了我。我不记得我移除了任何东西,如果我移除了,那将是错误的,所以请不要以错误的方式移除。再次感谢。
// Class
public class GenerateKeys {
private final KeyPairGenerator keyGen;
private KeyPair pair;
private PrivateKey privateKey;
private PublicKey publicKey;
// Constructor
public GenerateKeys(int keylength) throws NoSuchAlgorithmException, NoSuchProviderException {
this.keyGen = KeyPairGenerator.getInstance("RSA"); // Algorithm
this.keyGen.initialize(keylength);
}
public void createKeys() {
this.pair = this.keyGen.generateKeyPair();
this.privateKey = pair.getPrivate();
this.publicKey = pair.getPublic();
}
public PrivateKey getPrivateKey() {
return this.privateKey;
}
public PublicKey getPublicKey() {
return this.publicKey;
}
public void writeToFile(String path, byte[] key) throws IOException {
File f = new File(path);
f.getParentFile().mkdirs();
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(key);
fos.flush();
}
}
// Main
public static void main(String[] args)
{
GenerateKeys gk;
try {
gk = new GenerateKeys(1024);
gk.createKeys();
gk.writeToFile("MyKeys/publicKey",gk.getPublicKey().getEncoded());
gk.writeToFile("MyKeys/privateKey",gk.getPrivateKey().getEncoded());
} catch (NoSuchAlgorithmException | NoSuchProviderException | IOException e) {
System.err.println(e.getMessage());
}
}
}