简体   繁体   中英

Convert PHP generated Public Key into Java Public Key

I am new to Android and i am working on a client server based application.Here i need to use RSA algorithm for Encryption/Decryption.

The scenario is: 1.Server has to create a Private/Public key pair (PHP) and send Public key to Client(Android Application) 2.The client should encrypt the data using that Public key and send to server 3.Now server has to decrypt using Private Key.

In PHP, i used ' PhpSecLib ' to create key pair.

This is the coding :

Server.php:

<?PHP
include 'Crypt/RSA.php';
$rsa = new Crypt_RSA();
extract($rsa->createKey(1024));
echo $publickey;
?>

Generated Public Key :

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLz4yAo1FTOLc6nijBCTv5iVnW F6MxCeM5+RxY+29AXcpMWhlM9oES3ESIfWw6OzrrENDyqwY+kVzCj2bWYnEAyJXs WOpvqT2XSCPplwZOnQQGm+DnAYJXEeOfgU5DI63fwdiGv4M2ph1VMMe6684sBZu1 HhJHuhsX2eibBR0/lQIDAQAB

Now in Java, i successfully received this public key and stored in a String.

Coding:

protected String doInBackground(Void... params) 
{
    try
    {
    URL url=new URL("http://10.0.2.2/Samples/Server.php");
    URLConnection con=url.openConnection();
    con.setDoOutput(true);
    BufferedReader ip=new BufferedReader(new InputStreamReader(con.getInputStream()));
    String tmp,res="";

    while((tmp=ip.readLine())!=null)
    {
        res+=tmp;

    }

    return res; //res contains the public key


    }
    catch(Exception e)
    {
        return new String("Exception : "+e.getMessage());
    }

}

I am using Bouncy castle provider (bcprov-jdk15on-151) in java .

Now i have no idea about how to convert this string into RSA public key.

Pls suggest some code snippets? and if the code needs improvements and corrections, pls correct it.

Thanks...

Supposing that you download your key correctly, you can use your key in base 64 encoding to generate a java.security.PublicKey with the follow code:

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;

public class ParseRsaPublicKey {

    public static void main(String[] args) throws Exception {
        String yourKeyB64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLz4yAo1FTOLc6nijBCTv5iVnW\n"+
                "F6MxCeM5+RxY+29AXcpMWhlM9oES3ESIfWw6OzrrENDyqwY+kVzCj2bWYnEAyJXs\n"+
                "WOpvqT2XSCPplwZOnQQGm+DnAYJXEeOfgU5DI63fwdiGv4M2ph1VMMe6684sBZu1\n"+
                "HhJHuhsX2eibBR0/lQIDAQAB";
        // create the key factory
        KeyFactory kFactory = KeyFactory.getInstance("RSA", new BouncyCastleProvider());
        // decode base64 of your key
        byte yourKey[] =  Base64.decode(yourKeyB64);
        // generate the public key
        X509EncodedKeySpec spec =  new X509EncodedKeySpec(yourKey);
        PublicKey publicKey = (PublicKey) kFactory.generatePublic(spec);
        // now you can for example cipher some data with your key
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] cipherData = cipher.doFinal("someData".getBytes());
        System.out.println(new String(cipherData));
    }

}

Hope this helps,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM