Search code examples
androidbouncycastleelliptic-curve

Sending a Public Key over SMS


I'm currently trying to develop an encryption app for Android using ECDH and BouncyCastle. So far what I've implemented is Public and Private Key generation on the application as per the code below.

My next task is to send the public keys over SMS. I would like to find out what methods can be used to get the job done. Currently I'm trying it out by assigning the generated keys to a string then I'm sending the string out but I'm still unable to get it to work properly.

Any assistance would be greatly appreciated

Thanks and Happy Holidays!

    try
    {
        KeyPairGenerator    g = KeyPairGenerator.getInstance("ECDH", "SC");

        //Define the Elliptic Curve Field, Points A and B
        EllipticCurve curve = new EllipticCurve(new ECFieldFp(Presets.CurveQ),Presets.PointA,Presets.PointB);

        //Define the points on the Elliptic Curve
        ECParameterSpec ecSpec = new ECParameterSpec(
                curve,
                ECPointUtil.decodePoint(curve, Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
                new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"), // n
                1); // h

        //Generate the random point on the Elliptic Curve
        g.initialize(ecSpec, new SecureRandom());

        //Generate Private Key for User A
        KeyPair aKeyPair = g.generateKeyPair();
        aKeyAgree = KeyAgreement.getInstance("ECDH", "SC");
        aKeyAgree.init(aKeyPair.getPrivate());

        //Save Personal Keys            
        Presets.myPrivateKey = aKeyPair.getPrivate().getEncoded().toString();
        Presets.myPublicKey = aKeyPair.getPublic().getEncoded().toString();

Solution

  • I managed to find out what I was doing wrongly.

    The output I was getting from

    Presets.myPublicKey = aKeyPair.getPublic().getEncoded().toString();
    

    was something along the lines of [@B1ef9157 which could not be sent over SMS like I hoped for.

    Java: Syntax and meaning behind "[B@1ef9157"? Binary/Address?

    Instead I did this

    byte[] pubEnc = aKeyPair.getPublic().getEncoded();
    String s = Base64.encodeBytes(pubEnc);
    

    making use of the Base64 encoder from http://iharder.sourceforge.net/current/java/base64/

    and now I am able to successfully send the string over sms.

    Thanks Craigy!