rsa 密钥生成 见 http://blog.andsky.com/js-rsa-use-openssl-make-public-pirvate-key/

android 客户端用rsa 公钥加密后经 base64 编码发到 服务端,服务端使用私钥解密

客户端代码

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import sun.misc.BASE64Encoder;



public class rsa {


        public static void main(String[] args) throws Exception {

                String modulus = "C34FF1FF9771ED88814C26905297BAEDCEC03B847D8AB5620848FC100AC0564FAD5364E9834E29118E7B5F8B1B9EAB201730C4860E8AF2ED2E028704105A01044501A9EF6DA2968E76273AAE496A0963A2FEA9B6179A86F28ACC61C087FB1AEEA4E1CB0ADBB9B757C303741DE602FD790953C8E2C004A425C7CAF4813F403DCD";
        String publicExponent = "010001";

        rsa key = new rsa();
        PublicKey publicKey = key.getPublicKey(modulus, publicExponent);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");


        //明文
        String tString = "aabbsdfsdf";
        byte[] plainText = tString.getBytes("UTF-8");
        //加密
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] enBytes = cipher.doFinal(plainText);
        System.out.println(new BASE64Encoder().encode( enBytes ));
        }


         public PublicKey getPublicKey(String modulus,String publicExponent) throws Exception {
         BigInteger m = new BigInteger(modulus, 16);
         BigInteger e = new BigInteger(publicExponent, 16);
         RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m,e);
         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
         PublicKey publicKey = keyFactory.generatePublic(keySpec);
         return publicKey;

   }




}

服务端代码

 function privatekey_decodeing($crypttext, $privatekey)
    {

        $prikeyid = openssl_get_privatekey($privatekey);
        if (openssl_private_decrypt($crypttext, $sourcestr, $prikeyid,OPENSSL_PKCS1_PADDING))
        {
            return $sourcestr;
        }
        return FALSE;
    }

 $i = file_get_contents("private.key");

$base = 'fgvroFPT8GCIPYkGZJ834V0zQsnwbHKsCpFjpdN6TowUuxc6Bxu5PCF7SaZvb+3eCVEsuAjN73IP
QhRclqPiSv0MNPeZaYxNVPCDBkalsW3+/OuwPr7sQ53/rDwr2et0FnKJtkNyaFROMnxI9wRyg2Tx
h4+Fe60ypCvwY+wT8eg=';

        $b = base64_decode($base);
var_dump(privatekey_decodeing($b, $i));