3

I have a RSA Public key in base64_encoded form:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzkBJ/Wuyj0hZWa6oH+hD
+5JX1rV/TAf3lKxTPf0MUrREh2S3QPzLYBdUxByI552I3nAHJAh6JujUjGkj4O1y
X6OBJHJ596GQnv0wBUF5sr0QVg2ljav33HkuHt+otriY7jZy+OTlivkmdSdyhXht
VNlw+GgyQxeAI4f1BgaEGAfd9QJyN9yrkyZLqs9+CCmMog8ZbcqqSlR/S5nhUJku
zTD4YvmaA4okQADyOtktTCyUC3ndhRuGp451h+p5WAmcXYpW3QrqDfDzuFMy5Vlw
IB/EA9fZeMTY2tWi/7YnaVqYDJjhQv2XueOMizMCFCN2by+blc83uduPXJQpXHrV
aQIDAQAB
-----END PUBLIC KEY-----

and a modulus in hexadecimal form:

    00:ce:40:49:fd:6b:b2:8f:48:59:59:ae:a8:1f:e8:
    43:fb:92:57:d6:b5:7f:4c:07:f7:94:ac:53:3d:fd:
    0c:52:b4:44:87:64:b7:40:fc:cb:60:17:54:c4:1c:
    88:e7:9d:88:de:70:07:24:08:7a:26:e8:d4:8c:69:
    23:e0:ed:72:5f:a3:81:24:72:79:f7:a1:90:9e:fd:
    30:05:41:79:b2:bd:10:56:0d:a5:8d:ab:f7:dc:79:
    2e:1e:df:a8:b6:b8:98:ee:36:72:f8:e4:e5:8a:f9:
    26:75:27:72:85:78:6d:54:d9:70:f8:68:32:43:17:
    80:23:87:f5:06:06:84:18:07:dd:f5:02:72:37:dc:
    ab:93:26:4b:aa:cf:7e:08:29:8c:a2:0f:19:6d:ca:
    aa:4a:54:7f:4b:99:e1:50:99:2e:cd:30:f8:62:f9:
    9a:03:8a:24:40:00:f2:3a:d9:2d:4c:2c:94:0b:79:
    dd:85:1b:86:a7:8e:75:87:ea:79:58:09:9c:5d:8a:
    56:dd:0a:ea:0d:f0:f3:b8:53:32:e5:59:70:20:1f:
    c4:03:d7:d9:78:c4:d8:da:d5:a2:ff:b6:27:69:5a:
    98:0c:98:e1:42:fd:97:b9:e3:8c:8b:33:02:14:23:
    76:6f:2f:9b:95:cf:37:b9:db:8f:5c:94:29:5c:7a:
    d5:69

Here is a encrypting function that i want to use for signing a message.

public function encrypt ($m, $e, $n, $s=3) {
        $coded   = '';
        $max     = strlen($m);
        $packets = ceil($max/$s);

        for($i=0; $i<$packets; $i++){
            $packet = substr($m, $i*$s, $s);
            $code   = '0';

            for($j=0; $j<$s; $j++){
                $code = bcadd($code, bcmul(ord($packet[$j]), bcpow('256',$j)));
            }

            $code   = bcpowmod($code, $e, $n);
            $coded .= $code.' ';
        }

        return trim($coded);
    }

Now how could i convert the hexadecimal value into decimal and will it work like this? Please suggest! I have tested this function by generating publicKey,privateKey,modulus by RSA algorithm and working as charm, but now i need to work with the SSL certificate keys.

  • This might help: http://stackoverflow.com/q/1273484/4996248 – John Coleman Aug 22 '16 at 14:51
  • no luck.:( Still not able to create a proper decimal value – Malik khurram Aug 22 '16 at 15:29
  • What if you first stripped away `:` and `\n`? Getting the value of a hex string is conceptually a simple matter of summing multiples of powers of 16. The accepted answer shows the basic logic. You should be able to tweak it – John Coleman Aug 22 '16 at 15:41
  • one more thing. Public and private keys are in base64 form. Modulus should also be in base64 form (modulus->decimal->base64) in order to get a signed message? – Malik khurram Aug 22 '16 at 15:58
  • 1
    I don't really know much PHP. Clearly some sort of conversion is required. Why reinvent the wheel? Surely there must be PHP libraries for interacting with SSL certificates. – John Coleman Aug 22 '16 at 16:03
  • *"Here is a encrypting function that i want to use for signing a message."* - encrypting and signing are two very different things. Signing cannot be done using a public key or rather the produced signature doesn't attest anything useful. – Artjom B. Aug 22 '16 at 19:08

0 Answers0