import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;import javax.crypto.Cipher;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;public class SoapClient { PublicKey publickey = null; private String EncryptPassword(String password) { String result = "<?xml version="1.0" encoding="utf-8" ?><publickey xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/"><modulus>zr/DaG6X3/MRWcz5hS5CZDlzQ3WR/qpsMhWvPWFW/HrPkT63Z7Af01Z4Elqi3v07kVlMwi/jnNYm phNaS4UkZnv9HtrEpXmXPFBouKsp449ugXLSE8qQANN2W849jeywcqEf6yUPDeGSwtVmKb8AZ55Q btQOswdpuVSxSEu1v7U=</modulus><exponent>AQAB</exponent></publickey>"; if (publickey == null) {
String xml = ""; if (xml == null || xml.trim().length() == 0) {
return null;
} try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
// StringReader stringReader = new StringReader(xml);
// InputSource inputSource = new InputSource(stringReader);
ByteArrayInputStream inputSource = new ByteArrayInputStream(xml
.getBytes());
Document doc = builder.parse(inputSource); Element root = doc.getDocumentElement();
String strModulus = root.getElementsByTagName("modulus")
.item(0).getTextContent();
String strExponent = root.getElementsByTagName("exponent")
.item(0).getTextContent();
byte[] byteModulus = new sun.misc.BASE64Decoder()
.decodeBuffer(strModulus);
byte[] byteExponent = new sun.misc.BASE64Decoder()
.decodeBuffer(strExponent); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(
byteModulus), new BigInteger(byteExponent));
publickey = KeyFactory.getInstance("RSA").generatePublic(
keySpec); } catch (Exception ex) {
String e = ex.getMessage(); // do something ... return null;
}
} try {
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding"); rsaCipher.init(Cipher.ENCRYPT_MODE, publickey); byte[] rsaCipherText = rsaCipher.doFinal(password.getBytes());
result = new sun.misc.BASE64Encoder().encode(rsaCipherText);
} catch (Exception ex) {
String e = ex.getMessage();

return null;
} return result;
} public static void main(String args[]) {
SoapClient soapclient = new SoapClient();
String password = soapclient.EncryptPassword("ddd"); int e = 1;
}
}