Converting JavaScript RSA Function into a PHP one - php
I've got a Problem with converting a JS-based RSA File into PHP.
JavaScript:
Main File:
var RSAPublicKey = function($modulus_hex, $encryptionExponent_hex) {
this.modulus = new BigInteger( $modulus_hex, 16);
this.encryptionExponent = new BigInteger( $encryptionExponent_hex, 16);
}
var Base64 = {
base64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode: function($input) {
if (!$input) {
return false;
}
var $output = "";
var $chr1, $chr2, $chr3;
var $enc1, $enc2, $enc3, $enc4;
var $i = 0;
do {
$chr1 = $input.charCodeAt($i++);
$chr2 = $input.charCodeAt($i++);
$chr3 = $input.charCodeAt($i++);
$enc1 = $chr1 >> 2;
$enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4);
$enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6);
$enc4 = $chr3 & 63;
if (isNaN($chr2)) $enc3 = $enc4 = 64;
else if (isNaN($chr3)) $enc4 = 64;
$output += this.base64.charAt($enc1) + this.base64.charAt($enc2) + this.base64.charAt($enc3) + this.base64.charAt($enc4);
} while ($i < $input.length);
return $output;
},
decode: function($input) {
if(!$input) return false;
$input = $input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
var $output = "";
var $enc1, $enc2, $enc3, $enc4;
var $i = 0;
do {
$enc1 = this.base64.indexOf($input.charAt($i++));
$enc2 = this.base64.indexOf($input.charAt($i++));
$enc3 = this.base64.indexOf($input.charAt($i++));
$enc4 = this.base64.indexOf($input.charAt($i++));
$output += String.fromCharCode(($enc1 << 2) | ($enc2 >> 4));
if ($enc3 != 64) $output += String.fromCharCode((($enc2 & 15) << 4) | ($enc3 >> 2));
if ($enc4 != 64) $output += String.fromCharCode((($enc3 & 3) << 6) | $enc4);
} while ($i < $input.length);
return $output;
}
};
var Hex = {
hex: "0123456789abcdef",
encode: function($input) {
if(!$input) return false;
var $output = "";
var $k;
var $i = 0;
do {
$k = $input.charCodeAt($i++);
$output += this.hex.charAt(($k >> 4) &0xf) + this.hex.charAt($k & 0xf);
} while ($i < $input.length);
return $output;
},
decode: function($input) {
if(!$input) return false;
$input = $input.replace(/[^0-9abcdef]/g, "");
var $output = "";
var $i = 0;
do {
$output += String.fromCharCode(((this.hex.indexOf($input.charAt($i++)) << 4) & 0xf0) | (this.hex.indexOf($input.charAt($i++)) & 0xf));
} while ($i < $input.length);
return $output;
}
};
var RSA = {
getPublicKey: function( $modulus_hex, $exponent_hex ) {
return new RSAPublicKey( $modulus_hex, $exponent_hex );
},
encrypt: function($data, $pubkey) {
if (!$pubkey) return false;
$data = this.pkcs1pad2($data,($pubkey.modulus.bitLength()+7)>>3);
if(!$data) return false;
$data = $data.modPowInt($pubkey.encryptionExponent, $pubkey.modulus);
if(!$data) return false;
$data = $data.toString(16);
return Base64.encode(Hex.decode($data));
},
pkcs1pad2: function($data, $keysize) {
if($keysize < $data.length + 11)
return null;
var $buffer = [];
var $i = $data.length - 1;
while($i >= 0 && $keysize > 0)
$buffer[--$keysize] = $data.charCodeAt($i--);
$buffer[--$keysize] = 0;
while($keysize > 2)
$buffer[--$keysize] = Math.floor(Math.random()*254) + 1;
$buffer[--$keysize] = 2;
$buffer[--$keysize] = 0;
return new BigInteger($buffer);
}
}
How I use it:
var pubKey = RSA.getPublicKey( results.publickey_mod, results.publickey_exp );
var encryptedPassword = RSA.encrypt( form.elements['password'].value, pubKey );
Now I want to convert this into PHP, thing I have tried so far:
$pkey = $json["publickey_mod"];
$rsa = new Crypt_RSA();
$rsa->loadKey($pkey);
$rsa->exponent = $json["publickey_exp"];
echo ($rsa->setPublicKey($pkey))?"success":"fail";
$plain = "text";
echo $rsa->encrypt($plain);
But it always prints a "fail", what's wrong with it?
How the Key looks like:
CD5D11DFC04D457B864421AA483118EEA3A96C97E01ABFA4BAD356EC3CE6CA7E3F61665BAA1322D1D6C763D4463C1B41D69397281538D22302D8817CC10AEBDA96970DA4BA46BA79808E3B7BC3D38C1C2FD746E73EA5907F07C9588A1AF2C337FED1B7C8BB29304035420FB7EDE92511D1B07A63E21A5C36F132B556EE879113E88395ACCE9B987E478F8EA11B34634BCDA863DE7045EA9D7AB3240F406C572F36FDE1DB1534F92022B91DFBE23F980D0190D69953037EF9F38F98DA1A8028BE009CE82B979E8E427305802330111A78737D8E11C9CB132170CB34618941FA2AC2C754338886BFEE7DAD779036B292F9711DD9C765F37D42B6C0DC0BCCC83765
I know this is a bit old lol but it looks like you're using PKCS#1 padding. phpseclib uses OAEP padding by default. Try doing this:
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
Related
Convert VB6 code to PHP
I want to convert Visual Basic 6 Code to PHP Code. I am new to PHP please help me to convert my VB6 Code to PHP. So far I tried to convert this into php code when I tried the code there is an error in the " CryptRC4 = CryptRC4 & Chr$((pvCryptXor(baS((CLng(baS(li)) + baS(lJ)) Mod 256), Asc(Mid$(sText, lIdx, 1))))); part and also I don't know how to proceed to the sub functions. Please see the codes below. The vb code is used to encrypt strings. I want to convert it to php format. PHP Code <?php function CryptRC4($sText,$sKey){ $baS = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63, 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95, 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111, 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255); $baK = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63, 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95, 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111, 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255); $bytSwap = 0; $li = 0; $lJ = 0; $lIdx = 0; for( $lIdx = 0; $lIdx < 256; $lIdx++){ $baS[$lIdx] = $lIdx; $baK[$lIdx] = ord(substr($sKey, 1 + ($lIdx % strlen($sKey)), 1)); } for($li = 0; $li < 256; $li++){ $lJ = ($baS[$li] + $baK[$li]) % 256; $bytSwap = $baS[$li]; $baS[$li] = $baS[$lJ]; $baS[$lJ] = $bytSwap; } $li = 0; $lJ = 0; $data_str = ""; for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){ $li = ($li + 1) % 256; $lJ = ($lJ + $baS[$li]) % 256; $bytSwap = $baS[$li]; $baS[$li] = $baS[$lJ]; $baS[$lJ] = $bytSwap; #echo chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1))))); $data_str .= chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1))))); } echo $data_str; } function pvCryptXor($li, $lJ){ if($li = $lJ){ $pcx = $lJ; } else { $pcx = $li Xor $lJ; } return $pcx; } unction ToHexDump($sText) { $lIdx; for($lIdx = 1; $lIdx < strlen($sText); $lIdx++){ $thd .= Right$("0" & Hex(Asc(Mid(sText, lIdx, 1))), 2) echo $thd; } return $thd; } FromHexDump("events"); function FromHexDump($sText) { $fhd = ""; for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){ $fhd .= chr(CLng("&H" & Mid(sText, lIdx, 2))); } return $fhd; } ?> VB Code: Public Function CryptRC4(sText As String, sKey As String) As String On Error Resume Next Dim baS(0 To 255) As Byte Dim baK(0 To 255) As Byte Dim bytSwap As Byte Dim li As Long Dim lJ As Long Dim lIdx As Long For lIdx = 0 To 255 baS(lIdx) = lIdx baK(lIdx) = Asc(Mid$(sKey, 1 + (lIdx Mod Len(sKey)), 1)) Next For li = 0 To 255 lJ = (lJ + baS(li) + baK(li)) Mod 256 bytSwap = baS(li) baS(li) = baS(lJ) baS(lJ) = bytSwap Next li = 0 lJ = 0 For lIdx = 1 To Len(sText) li = (li + 1) Mod 256 lJ = (lJ + baS(li)) Mod 256 bytSwap = baS(li) baS(li) = baS(lJ) baS(lJ) = bytSwap CryptRC4 = CryptRC4 & Chr$((pvCryptXor(baS((CLng(baS(li)) + baS(lJ)) Mod 256), Asc(Mid$(sText, lIdx, 1))))) Next End Function Private Function pvCryptXor(ByVal li As Long, ByVal lJ As Long) As Long On Error Resume Next If li = lJ Then pvCryptXor = lJ Else pvCryptXor = li Xor lJ End If End Function Public Function ToHexDump(sText As String) As String On Error Resume Next Dim lIdx As Long For lIdx = 1 To Len(sText) ToHexDump = ToHexDump & Right$("0" & Hex(Asc(Mid(sText, lIdx, 1))), 2) Next End Function Public Function FromHexDump(sText As String) As String On Error Resume Next Dim lIdx As Long For lIdx = 1 To Len(sText) Step 2 FromHexDump = FromHexDump & Chr$(CLng("&H" & Mid(sText, lIdx, 2))) Next End Function
I revised your updated code and it seems you only had a few minor errors in it, look and my changes: I guess you can use the build in PHP function hex2bin and bin2hex instead fo you own hex conversion. function CryptRC4($sText,$sKey){ $baS = range(0, 255); // you can use range instead of your manual arrays $baK = range(0, 255); $bytSwap = 0; $li = 0; $lJ = 0; $lIdx = 0; for( $lIdx = 0; $lIdx < 256; $lIdx++){ $baS[$lIdx] = $lIdx; $baK[$lIdx] = ord(substr($sKey, 1 + ($lIdx % strlen($sKey)), 1)); } for($li = 0; $li < 256; $li++){ $lJ = ($baS[$li] + $baK[$li]) % 256; $bytSwap = $baS[$li]; $baS[$li] = $baS[$lJ]; $baS[$lJ] = $bytSwap; } $li = 0; $lJ = 0; $data_str = ""; for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){ $li = ($li + 1) % 256; $lJ = ($lJ + $baS[$li]) % 256; $bytSwap = $baS[$li]; $baS[$li] = $baS[$lJ]; $baS[$lJ] = $bytSwap; #echo chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1))))); $data_str .= chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1))))); } return $data_str; // changed from echo to return } function pvCryptXor($li, $lJ){ if($li == $lJ){ // you had an error here, use == to compare instead of a single = $pcx = $lJ; } else { $pcx = $li ^ $lJ; // XOR function in PHP is the ^ operator } return $pcx; } $str_hex = bin2hex("events"); $str_enc = CryptRC4($str_hex,"password"); $str_dec = hex2bin(CryptRC4($str_enc,"password")); echo $str_hex . PHP_EOL . $str_enc . PHP_EOL . $str_dec; OUTPUT: 6576656e7473 '�����~i�� events So it seems to me as it's actually encoding and decoding correctly!?
It seems the original VB6 implementation of CryptRC4 function is from my answer to "VB6 encrypt text using password" question on SO. So let me try answering your Q with this short php implementation of all public functions in the VB6 snippet: function CryptRC4($text, $key) { return openssl_encrypt($text, "RC4-40", $key, 1 | 2); } function ToHexDump($text) { return strtoupper(bin2hex($text)); } function FromHexDump($text) { return hex2bin($text); } You can excercise these one-liners with something like this: $text = "a message here"; $password = "password"; $encr = ToHexDump(CryptRC4($text, $password)); $decr = CryptRC4(FromHexDump($encr), $password); echo $text . PHP_EOL . $encr . PHP_EOL . $decr;
As it's intended for passwords, you can save yourself a lot of hassle. PHP has got built in functions (version 5.5 and newer) that are designed for dealing with the hashing of passwords and for verifying hashed passwords against the password submitted by a user. Have a read through the PHP relevant PHP manual pages http://php.net/manual/en/book.password.php
Unable to use phpseclib classes
I've installed the phpseclib library from github and I'm trying to encrypt a password (for steamcommunity) with PHP. I am able to do it with Javascript by using the javascript code Steam has on their website but I cannot encrypt the password using pure PHP. The Javascript encryption code: var RSAPublicKey = function($modulus_hex, $encryptionExponent_hex) { this.modulus = new BigInteger( $modulus_hex, 16); this.encryptionExponent = new BigInteger( $encryptionExponent_hex, 16); }; var Base64 = { base64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function($input) { if (!$input) { return false; } var $output = ""; var $chr1, $chr2, $chr3; var $enc1, $enc2, $enc3, $enc4; var $i = 0; do { $chr1 = $input.charCodeAt($i++); $chr2 = $input.charCodeAt($i++); $chr3 = $input.charCodeAt($i++); $enc1 = $chr1 >> 2; $enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4); $enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6); $enc4 = $chr3 & 63; if (isNaN($chr2)) $enc3 = $enc4 = 64; else if (isNaN($chr3)) $enc4 = 64; $output += this.base64.charAt($enc1) + this.base64.charAt($enc2) + this.base64.charAt($enc3) + this.base64.charAt($enc4); } while ($i < $input.length); return $output; }, decode: function($input) { if(!$input) return false; $input = $input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); var $output = ""; var $enc1, $enc2, $enc3, $enc4; var $i = 0; do { $enc1 = this.base64.indexOf($input.charAt($i++)); $enc2 = this.base64.indexOf($input.charAt($i++)); $enc3 = this.base64.indexOf($input.charAt($i++)); $enc4 = this.base64.indexOf($input.charAt($i++)); $output += String.fromCharCode(($enc1 << 2) | ($enc2 >> 4)); if ($enc3 != 64) $output += String.fromCharCode((($enc2 & 15) << 4) | ($enc3 >> 2)); if ($enc4 != 64) $output += String.fromCharCode((($enc3 & 3) << 6) | $enc4); } while ($i < $input.length); return $output; } }; var Hex = { hex: "0123456789abcdef", encode: function($input) { if(!$input) return false; var $output = ""; var $k; var $i = 0; do { $k = $input.charCodeAt($i++); $output += this.hex.charAt(($k >> 4) &0xf) + this.hex.charAt($k & 0xf); } while ($i < $input.length); return $output; }, decode: function($input) { if(!$input) return false; $input = $input.replace(/[^0-9abcdef]/g, ""); var $output = ""; var $i = 0; do { $output += String.fromCharCode(((this.hex.indexOf($input.charAt($i++)) << 4) & 0xf0) | (this.hex.indexOf($input.charAt($i++)) & 0xf)); } while ($i < $input.length); return $output; } }; var RSA = { getPublicKey: function( $modulus_hex, $exponent_hex ) { return new RSAPublicKey( $modulus_hex, $exponent_hex ); }, encrypt: function($data, $pubkey) { if (!$pubkey) return false; $data = this.pkcs1pad2($data,($pubkey.modulus.bitLength()+7)>>3); if(!$data) return false; $data = $data.modPowInt($pubkey.encryptionExponent, $pubkey.modulus); if(!$data) return false; $data = $data.toString(16); if(($data.length & 1) == 1) $data = "0" + $data; return Base64.encode(Hex.decode($data)); }, pkcs1pad2: function($data, $keysize) { if($keysize < $data.length + 11) return null; var $buffer = []; var $i = $data.length - 1; while($i >= 0 && $keysize > 0) $buffer[--$keysize] = $data.charCodeAt($i--); $buffer[--$keysize] = 0; while($keysize > 2) $buffer[--$keysize] = Math.floor(Math.random()*254) + 1; $buffer[--$keysize] = 2; $buffer[--$keysize] = 0; return new BigInteger($buffer); } }; And to encrypt it I simply call the following: var pubKey = RSA.getPublicKey('<?php echo $curl->response->publickey_mod; ?>', '<?php echo $curl->response->publickey_exp; ?>'); var encrypted_password = RSA.encrypt('<?php echo $inviter['password']; ?>', pubKey); When I try to include a file from the phpseclib and use the class, the file includes successfully but the class displays an error saying it's not a valid class. My PHP code: <?php // publickey_mod = C2242A41A84E56D9CD7952CCB985343A6DCE26AF7CA31036415B7B9DA190DF68BBEC93ED6A9AAE5CA9D6EB1A65244E0013DB38C9462076F8165EDB2A1ECB383A75FB0C71388AFD2C2DC15F9088BA252B4ED2F2C304F545C3704B6B7F5CEE01AF7DD0DC9038CBFBDE7B99689006C5272EBCEC221DB3D9CEB8514E246A571E49709AD94442A8611BE131E6F3FFA40AD632BBD6C5B5F85F7A5C87310E17632145A57BDF17A4F7305E959B4A6126AA8C64C794975F17051E6EAF5462F1189AAD0E7B5F2F254AEEB030D8ED8BAD0F8BD753482E652FFB2405D8947994E143032D5511AF63977A51C9013B5516FA51C5AC50E56588C09C761A473E8D18566B1D435687 // publickey_exp = 010001 require_once 'phpseclib/Crypt/RSA.php'; require_once 'phpseclib/Crypt/RSA/PKCS1.php'; $pkcs = new \phpseclib\Crypt\RSA\PKCS1(); $key = $pkcs->savePublicKey(new \phpseclib\Math\BigInteger(C2242A41A84E56D9CD7952CCB985343A6DCE26AF7CA31036415B7B9DA190DF68BBEC93ED6A9AAE5CA9D6EB1A65244E0013DB38C9462076F8165EDB2A1ECB383A75FB0C71388AFD2C2DC15F9088BA252B4ED2F2C304F545C3704B6B7F5CEE01AF7DD0DC9038CBFBDE7B99689006C5272EBCEC221DB3D9CEB8514E246A571E49709AD94442A8611BE131E6F3FFA40AD632BBD6C5B5F85F7A5C87310E17632145A57BDF17A4F7305E959B4A6126AA8C64C794975F17051E6EAF5462F1189AAD0E7B5F2F254AEEB030D8ED8BAD0F8BD753482E652FFB2405D8947994E143032D5511AF63977A51C9013B5516FA51C5AC50E56588C09C761A473E8D18566B1D435687, 16), new \phpseclib\Math\BigInteger(010001, 16)); $rsa = new phpseclib\Crypt\RSA(); $rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1); $rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1); $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); define('CRYPT_RSA_PKCS15_COMPAT', true); $rsa->loadKey($key); $password = $rsa->encrypt("mySteamPassword"); // encrypting password var_dump($password); ?> And the error message that this code displays: Fatal error: Class 'phpseclib\Crypt\RSA\PKCS' not found in C:\xampp\htdocs\RSA-Encryption\phpseclib\Crypt\RSA\PKCS1.php on line 40
See this part? require_once 'phpseclib/Crypt/RSA.php'; require_once 'phpseclib/Crypt/RSA/PKCS1.php'; You're explicitly including specific classes that depend on other classes, but are not including them directly. The correct solution is to, instead of typing in require_once statements directly, use an autoloader. If you're loading this library with composer, this is easy. Run composer require phpseclib/phpseclib Add require_once 'vendor/autoload.php'; to your PHP scripts. Otherwise, you need to use spl_autoload_register() instead: /** * Register a PSR autoloader for a given namespace and directory * * #param string $namespace * #param string $dir * #param string $type ('psr0' or 'psr4') * #return boolean * #throws Exception * #ref http://stackoverflow.com/a/35015933/2224584 */ function generic_autoload($namespace, $dir, $type = 'psr4') { switch ($type) { case 'psr0': $spl = '_'; break; case 'psr4': $spl = '\\'; break; default: throw new Exception('Invalid type; expected "psr0" or "psr4"'); } $ns = trim($namespace, DIRECTORY_SEPARATOR.$spl); return spl_autoload_register( function($class) use ($ns, $dir, $spl) { // project-specific namespace prefix $prefix = $ns.$spl; // base directory for the namespace prefix $base_dir = $dir . DIRECTORY_SEPARATOR; // does the class use the namespace prefix? $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { // no, move to the next registered autoloader return; } // get the relative class name $relative_class = substr($class, $len); // replace the namespace prefix with the base directory, replace // namespace separators with directory separators in the relative // class name, append with .php $file = $base_dir . str_replace($spl, DIRECTORY_SEPARATOR, $relative_class) . '.php'; // if the file exists, require it if (file_exists($file)) { require $file; } } ); } Usage: generic_autoload('phpseclib', '/path/to/phpseclib', 'psr4'); Word of caution: Make sure you aren't using PKCS1v1.5 padding for RSA encryption. If you're going to use RSA, you should use RSAES-OAEP with MGF1+SHA256 and e = 65537. I know phpseclib supports this; if Steam Community doesn't, raise hell until they do.
PHP rot5 javascript equalivant?
I am wondering if there is a rot5 function for PHP equalivant too function rot5(str) { var s = []; for (i = 0; i < str.length; i ++) { idx = str.charCodeAt(i); if ((idx >= 48) && (idx <= 57)) { if (idx <= 52) { s[i] = String.fromCharCode(((idx + 5))); } else { s[i] = String.fromCharCode(((idx - 5))); } } else { s[i] = String.fromCharCode(idx); } } return s.join(''); } In javascript? If not then how can I do fromCharCode and charCodeAt in PHP?
Here you got generic strrot with default to strrot5 <?php function strrot($str, $n=5){ $replace = []; for($i=0;$i<26;$i++){ $replace[chr(65+$i)] = chr(65+(($i+$n)%26)); $replace[chr(97+$i)] = chr(97+(($i+$n)%26)); } return strtr($str, $replace); }
You can use 'ord' for 'charCodeAt'. ord($yourstring[$yourindex]) You can use 'chr' for 'fromCharCode' chr($yourcharcode)
javascript translation, encode in php decode in javascript
Hi I'm trying to encode a string in PHP then output in javascript because otherwise Javascript gives me error. I tried this http://pastebin.com/7RmjDcJY: <?php echo "<script type=\"text/javascript\"> function lang(translate){ var translations = new Array();"; foreach($languageTranslations AS $key=>$value): echo "translations[\"".$key."\"] = \"". base64_encode($value)."\";"; endforeach; echo "return (typeof translations[translate]!='undefined') ? $.base64.decode(translations[translate]) : translate; } </script>";?> But förnamn -> Förnamn Any idea? I've utf-8 in my document, but I guess bas64 can't convert utf-8 properly? I can't just output the value, because some contains characters that breaks my javascript. Or should I do a replace first? Like replace " with \" and stuff? EDIT: Now I got: //urlencode in PHP and: decodeURIComponent(translations[translate]).replace('+',' ') But that doesn't feel like the right thing. What if I had an + sign in my text?
for send data from php to JavaScript you can base64 data and if you have more than one data to send use json_encode for send multi data,then in javaScript use json decode for get value and decode base64 by this code var Base64 = { // private property _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", // public method for encoding encode : function (input) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = Base64._utf8_encode(input); while (i < input.length) { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); } return output; }, // public method for decoding decode : function (input) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); while (i < input.length) { enc1 = this._keyStr.indexOf(input.charAt(i++)); enc2 = this._keyStr.indexOf(input.charAt(i++)); enc3 = this._keyStr.indexOf(input.charAt(i++)); enc4 = this._keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2); } if (enc4 != 64) { output = output + String.fromCharCode(chr3); } } output = Base64._utf8_decode(output); return output; }, // private method for UTF-8 encoding _utf8_encode : function (string) { string = string.replace(/\r\n/g,"\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; }, // private method for UTF-8 decoding _utf8_decode : function (utftext) { var string = ""; var i = 0; var c = c1 = c2 = 0; while ( i < utftext.length ) { c = utftext.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; } else if((c > 191) && (c < 224)) { c2 = utftext.charCodeAt(i+1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } } sample: alert(Base64._utf8_decode(2LTYs9uM2YTYqNi02LPbjNio2YLYq9in24zYqNmE2LDYs9uM2KjYsQ==)); this code compatible with utf-8
How to get the signature algorithm out of a certificate?
I want to use the PHP function openssl_verify() to verify the signatures of different X.509 certificates. I have all it needs (certificate, $data, $signature, $pub_key_id) except of the signature algorithm but which is stored in the certificate. My simple question is: How can I extract signature algorithm from certificates?
How about this? $cer = file_get_contents('certificate.cer'); $res = openssl_x509_read($cer); openssl_x509_export($res, $out, FALSE); $signature_algorithm = null; if(preg_match('/^\s+Signature Algorithm:\s*(.*)\s*$/m', $out, $match)) $signature_algorithm = $match[1]; var_dump($signature_algorithm); It produces the output: string(21) "sha1WithRSAEncryption" Which you would have to map to OPENSSL_ALGO_SHA1 yourself.
Look at this question, you can do it similar, try this: private function GetCertSignatureAlgorithm($certSignatureBinary, $pubKeyResourceId) { if(false === openssl_public_decrypt($certSignatureBinary, $sigString, $pubKeyResourceId)) { return false; } if (empty($sigString) || strlen($sigString) < 5) { return false; } if (ord($sigString[0]) !== 0x30 || ord($sigString[2]) !== 0x30 || ord($sigString[4]) !== 0x06) { return false; } $sigString = substr($sigString, 4); $len = ord($sigString[1]); $bytes = 0; if ($len & 0x80) { $bytes = ($len & 0x7f); $len = 0; for ($i = 0; $i < $bytes; $i++) { $len = ($len << 8) | ord($sigString[$i + 2]); } } $oidData = substr($sigString, 2 + $bytes, $len); $hashOid = floor(ord($oidData[0]) / 40) . '.' . ord($oidData[0]) % 40; $value = 0; for ($i = 1; $i < strlen($oidData); $i++) { $value = $value << 7; $value = $value | (ord($oidData[$i]) & 0x7f); if (!(ord($oidData[$i]) & 0x80)) { $hashOid .= '.' . $value; $value = 0; } } //www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xml //www.php.net/manual/en/openssl.signature-algos.php switch($hashOid) { case '1.2.840.113549.2.5': return 'md5'; case '1.3.14.3.2.26': return 'sha1'; case '2.16.840.1.101.3.4.2.1': return 'sha256'; case '2.16.840.1.101.3.4.2.2': return 'sha384'; case '2.16.840.1.101.3.4.2.3': return 'sha512'; //not secure = not accepted //case '1.2.840.113549.2.2': //'md2'; //case '1.2.840.113549.2.4': //'md4'; //case '1.3.14.3.2.18': //'sha'; } throw new Exception('CertSignatureAlgorithm not found'); }
One way might be openssl x509 -text -noout < $certfile | grep "Signature Algorithm"
Using phpseclib, a pure PHP X.509 parser... <?php include('File/X509.php'); $x509 = new File_X509(); $cert = $x509->loadX509(file_get_contents('sample.pem')); echo $cert['signatureAlgorithm']['algorithm'];