3 require_once('library/ASNValue.class.php');
5 function DerToPem($Der, $Private=false)
8 $Der = base64_encode($Der);
10 $lines = str_split($Der, 65);
11 $body = implode("\n", $lines);
13 $title = $Private? 'RSA PRIVATE KEY' : 'PUBLIC KEY';
15 $result = "-----BEGIN {$title}-----\n";
16 $result .= $body . "\n";
17 $result .= "-----END {$title}-----\n";
22 function DerToRsa($Der)
25 $Der = base64_encode($Der);
27 $lines = str_split($Der, 65);
28 $body = implode("\n", $lines);
30 $title = 'RSA PUBLIC KEY';
32 $result = "-----BEGIN {$title}-----\n";
33 $result .= $body . "\n";
34 $result .= "-----END {$title}-----\n";
42 function pkcs8_encode($Modulus,$PublicExponent) {
44 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
45 $modulus->SetIntBuffer($Modulus);
46 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
47 $publicExponent->SetIntBuffer($PublicExponent);
48 $keySequenceItems = array($modulus, $publicExponent);
49 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
50 $keySequence->SetSequence($keySequenceItems);
52 $bitStringValue = $keySequence->Encode();
53 $bitStringValue = chr(0x00) . $bitStringValue; //Add unused bits byte
54 $bitString = new ASNValue(ASNValue::TAG_BITSTRING);
55 $bitString->Value = $bitStringValue;
57 $bodyValue = "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00" . $bitString->Encode();
58 $body = new ASNValue(ASNValue::TAG_SEQUENCE);
59 $body->Value = $bodyValue;
60 //Get DER encoded public key:
61 $PublicDER = $body->Encode();
66 function pkcs1_encode($Modulus,$PublicExponent) {
68 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
69 $modulus->SetIntBuffer($Modulus);
70 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
71 $publicExponent->SetIntBuffer($PublicExponent);
72 $keySequenceItems = array($modulus, $publicExponent);
73 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
74 $keySequence->SetSequence($keySequenceItems);
76 $bitStringValue = $keySequence->Encode();
77 return $bitStringValue;
79 // $bitStringValue = chr(0x00) . $bitStringValue; //Add unused bits byte
80 // $bitString = new ASNValue(ASNValue::TAG_BITSTRING);
81 // $bitString->Value = $bitStringValue;
83 // $bodyValue = "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00" . $bitString->Encode();
84 // $body = new ASNValue(ASNValue::TAG_SEQUENCE);
85 // $body->Value = $bodyValue;
86 //Get DER encoded public key:
87 // $PublicDER = $body->Encode();
92 function metopem($m,$e) {
93 $der = pkcs8_encode($m,$e);
94 $key = DerToPem($der,false);
99 function pubrsatome($key,&$m,&$e) {
100 require_once('library/asn1.php');
101 require_once('include/salmon.php');
103 $lines = explode("\n",$key);
105 unset($lines[count($lines)]);
106 $x = base64_decode(implode('',$lines));
108 $r = ASN_BASE::parseASNString($x);
112 $m = base64url_decode($r[0]->asnData[0]->asnData);
113 $e = base64url_decode($r[0]->asnData[1]->asnData);
119 function rsatopem($key) {
120 pubrsatome($key,$m,$e);
121 return(metopem($m,$e));
125 function pemtome($key,&$m,&$e) {
126 require_once('include/salmon.php');
127 $lines = explode("\n",$key);
129 unset($lines[count($lines)]);
130 $x = base64_decode(implode('',$lines));
132 $r = ASN_BASE::parseASNString($x);
134 $m = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData);
135 $e = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData);
138 function metorsa($m,$e) {
139 $der = pkcs1_encode($m,$e);
140 $key = DerToRsa($der);