import java.security.*;
public class MyKeyGenerator {
private KeyPairGenerator keyGen;
private KeyPair pair;
private PrivateKey privateKey;
private PublicKey publicKey;
private Context context;
public MyKeyGenerator(Context context, int length)throws Exception{
this.context =context;
this.keyGen = KeyPairGenerator.getInstance("RSA");
this.keyGen.initialize(length);
}
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 String getPrivateKeyStr(){
byte b [] = this.getPrivateKey().getEncoded();
return new String(b));
}
public String getPublicKeyStr(){
byte b [] = this.getPublicKey().getEncoded();
return new String(b));
}
}
你好,我已搜查了SO如何转换或得到一个公共密钥和私人密钥的字符串表示,大部分的答案是很老,只对如何字符串PUBKEY =转换“......”;成键。我试图生成密钥,并获得编码的字节,我试图将字节转换为字符串如我上面的代码显示,但我不知道如果我通过简单地将已编码的字节为一个字符串做正确的方式。