20
私は512ビットRSA鍵ペアを生成し、次に私の公開鍵を文字列として符号化したいと思う。どうすればこれを達成できますか?出力の場合RSA鍵ペアを生成し、プライベート文字列として符号化する
私は512ビットRSA鍵ペアを生成し、次に私の公開鍵を文字列として符号化したいと思う。どうすればこれを達成できますか?出力の場合RSA鍵ペアを生成し、プライベート文字列として符号化する
import java.security.*;
public class Test {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(512);
byte[] publicKey = keyGen.genKeyPair().getPublic().getEncoded();
StringBuffer retString = new StringBuffer();
for (int i = 0; i < publicKey.length; ++i) {
retString.append(Integer.toHexString(0x0100 + (publicKey[i] & 0x00FF)).substring(1));
}
System.out.println(retString);
}
}
バイトは
import java.security.*;
import java.security.*;
public class Test {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(512);
byte[] publicKey = keyGen.genKeyPair().getPublic().getEncoded();
StringBuffer retString = new StringBuffer();
retString.append("[");
for (int i = 0; i < publicKey.length; ++i) {
retString.append(publicKey[i]);
retString.append(", ");
}
retString = retString.delete(retString.length()-2,retString.length());
retString.append("]");
System.out.println(retString); //e.g. [48, 92, 48, .... , 0, 1]
}
}
値として本当にありがとうございます! 私はこのような結果を得なければならない[48、-137、-97,49,13,6,8,42、-122,72、-122、-9,13,2,3,15,4,0、 3、-132、-115、0、48、-127]代わりにtoStringメソッドを使用する必要があります。 retString.append(Integer.toHexString(0x0100 +(publicKey [i]&0x00FF))。 ?? – Angela
2番目のコードのリストを確認してください。 – jitter
出力をPEM形式にしたい場合はどうしたらいいですか? – Yatin