RSA Public key from xml
843811Dec 2 2009 — edited Dec 2 2009
I have public key in xml format retrieved from a soap call like:
Public key =
<RSAKeyValue><Modulus>Tir6unMOaQ5tHqjjjwnlAMhccnCSMFEi3a0mhIxbW+O/GukjomGyzckQT2h0Ys70JezHbNq5YS3sYkNF29kCkz4HuNfy9eEjE/clA9/zyfT8ZcbnusLcLz2xNgbTp62fQdzBnReI5+dpj/N24krYvHaYIr8ACxDqBv2TR3E9M=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
The c# code to encrypt the data is like this which does the job correclty.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace ClassLibrary1
{
public class Class1
{
public static byte[] RSAEncrypt( )
{
string dataToEncrypt = "By35Jws8";
string publicKeyXMLString = "<RSAKeyValue><Modulus>+Tir6+unMOaQ5tHqjjjwnlAMhccnCSMFEi3a0mhIxbW+O/GukjomGyzckQT2h0Ys70JezHbNq5YS3sYkNF29kCkz4HuNfy9eEjE/clA9/zyfT8ZcbnusLcLz2xNgbTp62fQdzBnReI5+dpj/N24krYvHaYIr8ACxDqBv2TR3E9M=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] encryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(publicKeyXMLString);
encryptedData = RSA.Encrypt(ByteConverter.GetBytes(dataToEncrypt), false);
}
return encryptedData;
}
catch (CryptographicException e)
{
return null;
}
}
}
}
But I need to use java for some specific reason and i am new to java.
How can i achieve this is java, Could some one plase help me?