2010-09-02 107 views
1

我正在开发一个Android应用程序,我需要生成一些RSA私钥和公钥以用于与Web服务的安全通信。要做到这一点,我需要有一个.NET兼容形式的公钥。 像:Android创建RSA 1024 .NET兼容密钥

<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue> 

到目前为止,我设法给这样的:

keyGen = KeyPairGenerator.getInstance("RSA"); 
    keyGen.initialize(1024); 
    keypair = keyGen.genKeyPair(); 
    privateKey = keypair.getPrivate(); 
    publicKey = keypair.getPublic(); 

    // Get the bytes of the public and private keys 
    byte[] privateKeyBytes = privateKey.getEncoded(); 
    byte[] publicKeyBytes = publicKey.getEncoded(); 

我有不知道如何继续。你能否提供一些帮助?

回答

0

您不显示类型publicKey。如果还没有,则应将其转换为RSAPublicKey,然后使用getPublicExponent()getModulus()方法提取BigInteger。然后只需使用标准的Java IO,e.q. PrintStream.println()printf()来生成XML组件。

+0

要将其序列化为''(这大概来自XML数字签名格式),您还需要对数值进行base64编码从'getPublicExponent()'和'getModulus()'获得,而不是直接使用'toString()'。 – Bruno 2010-09-03 12:36:46

+0

@布鲁诺:谢谢,我对规范并不熟悉,只是猜测{0}和{1}需要什么。 – 2010-09-03 20:03:42