public function encrypt($string, $id) { $user = new User($id); $gpg = new gnupg(); $gpg->addencryptkey($user->getPublickey); $encryptedString = $gpg->encrypt($string); return $encryptedString; }
public function encryptSecret($public_key) { // Set GnuPG homedir to /tmp putenv("GNUPGHOME=/tmp"); // Create new GnuPG instance $gpg = new gnupg(); // Import given public key $key = $gpg->import($public_key); // Add imported key for encryption $gpg->addencryptkey($key['fingerprint']); // Encrypt the secret to a PGP message $enc = $gpg->encrypt($this->secret); // Clear the encryption key $gpg->clearencryptkeys(); // Return the PGP message return $enc; }
public function encrypt_message($recipient_key_id, $plaintext) { $this->set_env(); try { $gpg = new gnupg(); // throw exception if error occurs $gpg->seterrormode(gnupg::ERROR_EXCEPTION); $gpg->addencryptkey($recipient_key_id); $cipher_text = $gpg->encrypt($plaintext); $this->restore_env(); return $cipher_text; } catch (Exception $e) { // restore the envelope $this->restore_env(); // re-throw the exception throw $e; } }
function elggpg_encrypt($body, $user, $force = true) { $already_encrypted = strpos($body, "-----BEGIN PGP MESSAGE-----") !== false; try { if (!$already_encrypted) { $gpg = new gnupg(); $gpg->addencryptkey($user->openpgp_publickey); if ($encrbody = $gpg->encrypt($body)) { $body = $encrbody; } elseif ($force) { return false; } } } catch (Exception $e) { if ($force) { return false; } } return $body; }
/** * Dataflow 11 */ public function exportPresenterData() { ini_set('memory_limit', '4000M'); ini_set("max_execution_time", 9000); $this->out('Exporting Presenter Data'); $this->SystemSetting = ClassRegistry::init('SystemSetting'); $this->Presenter = ClassRegistry::init('Presenter'); $this->PresenterDocuments = ClassRegistry::init('PresenterDocuments'); $this->User = ClassRegistry::init('User'); $this->Address = ClassRegistry::init('Address'); $this->Email = ClassRegistry::init('Email'); $this->Phone = ClassRegistry::init('Phone'); $this->PresenterSite = ClassRegistry::init('PresenterSite'); $this->PresenterType = ClassRegistry::init('PresenterType'); // reads for replicated $this->Presenter->useDbConfig = "replicated"; $this->User->useDbConfig = "replicated"; $this->Address->useDbConfig = "replicated"; $this->Email->useDbConfig = "replicated"; $this->PresenterDocuments->useDbConfig = "replicated"; $this->PresenterType->useDbConfig = "replicated"; $this->Phone->useDbConfig = "replicated"; $this->PresenterSite->useDbConfig = "replicated"; $db = ConnectionManager::getDataSource('default'); $result = $db->query("Select date_sub(now(), interval 2 second) as timestamp"); $two_seconds_ago = $result[0][0]["timestamp"]; $folder = "dataflow11"; $aes_key = md5(Configure::read('aesKey')); // get timestamp $last_time = $this->SystemSetting->getSystemSetting('netsuite_presenter_df', date('Y-m-d H:i:s')); $last_time_ranks = $this->SystemSetting->getSystemSetting('netsuite_presenter_df', date('Y-m-d H:i:s')); $this->out('From ' . $last_time . ' To ' . $two_seconds_ago, 1, Shell::VERBOSE); // check presenter auditdate $presenters = $this->Presenter->find('list', ['fields' => ['id'], 'conditions' => ['_auditdate >' => $last_time, '_auditdate <=' => $two_seconds_ago, 'presenter_status_id >' => PresenterStatus::PENDING_ERRORS]]); $this->out('Found ' . count($presenters) . ' Presenters', 1, Shell::VERBOSE); // check users $users = $this->User->query("\n\t\t\t\tSELECT p.id\n\t\t\t\tFROM users u\n\t\t\t\tJOIN presenters p on p.user_id = u.id\n\t\t\t\tWHERE u._auditdate > '{$last_time}'\n\t\t\t\t\tAND u._auditdate <= '{$two_seconds_ago}'\n\t\t\t\t\tAND p.presenter_status_id > " . PresenterStatus::PENDING_ERRORS . "\n\t\t\t"); $user_count = 0; foreach ($users as $key => $value) { if (!array_key_exists($value['p']['id'], $presenters)) { $presenters[$value['p']['id']] = $value['p']['id']; $user_count++; } } unset($users); $this->out('Added ' . $user_count . ' Users', 1, Shell::VERBOSE); // check documents $docs = $this->PresenterDocuments->find('all', ['conditions' => ['date_received >' => $last_time, 'date_received <=' => $two_seconds_ago]]); $doc_count = 0; foreach ($docs as $key => $value) { if (!array_key_exists($value['PresenterDocuments']['presenter_id'], $presenters)) { $presenters[$value['PresenterDocuments']['presenter_id']] = $value['PresenterDocuments']['presenter_id']; $doc_count++; } } unset($docs); $this->out('Added ' . $doc_count . ' Docs', 1, Shell::VERBOSE); //check address $addresses = $this->Address->query("\n\t\t\t\tSELECT DISTINCT p.id\n\t\t\t\tFROM addresses a\n\t\t\t\tJOIN users u on a.user_id = u.id\n\t\t\t\tJOIN presenters p on p.user_id = u.id\n\t\t\t\tWHERE a._auditdate > '{$last_time}'\n\t\t\t\t\tAND a._auditdate <= '{$two_seconds_ago}'\n\t\t\t\t\tAND p.presenter_status_id > " . PresenterStatus::PENDING_ERRORS . "\n\t\t\t"); $address_count = 0; foreach ($addresses as $key => $value) { if (!array_key_exists($value['p']['id'], $presenters)) { $presenters[$value['p']['id']] = $value['p']['id']; $address_count++; } } unset($addresses); $this->out('Added ' . $address_count . ' Addresses', 1, Shell::VERBOSE); // check presenter types $today = new DateTime(); // if ($today->format('d') == 6 && ) $this_month = new DateTime('first day of this month 00:00:00'); $this_yearmonth = $this_month->format('Ym'); $last_month = new DateTime('first day of last month 00:00:00'); $last_yearmonth = $last_month->format('Ym'); // $ranks = $this->PresenterType->getChanges($last_yearmonth, $this_yearmonth); $this->out('Total ' . count($presenters), 1, Shell::VERBOSE); // get the data on each presenter matched $presenter_chunks = array_chunk($presenters, 1000, TRUE); unset($presenters); $this->out('Chunked results into ' . count($presenter_chunks) . " chunks", 1, Shell::VERBOSE); foreach ($presenter_chunks as $index => $chunk) { $this->out('Processing chunk ' . $index, 1, Shell::VERBOSE); $results = []; foreach ($chunk as $presenter) { $p_data = $this->Presenter->query("\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\tuser_id,\n\t\t\t\t\t\t\tpresenter_sequence_id,\n\t\t\t\t\t\t\tsponsor_id,\n\t\t\t\t\t\t\t IF(government_id IS NOT NULL AND government_id != '',\n\t\t\t\t\t\t\t\tgovernment_id,\n\t\t\t\t\t\t\t\tCAST(AES_DECRYPT(government_id_encrypted, '{$aes_key}')AS CHAR (50))) as government_id,\n\t\t\t\t\t\t\tmarket_id,\n\t\t\t\t\t\t\tconsent_to_agreements,\n\t\t\t\t\t\t\tterminated_date,\n\t\t\t\t\t\t\tpresenter_status_id,\n\t\t\t\t\t\t\tnickname,\n\t\t\t\t\t\t\ttax_exempt,\n\t\t\t\t\t\t\texempt_reasoncode,\n\t\t\t\t\t\t\tdefault_locale\n\t\t\t\t\t\tFROM presenters\n\t\t\t\t\t\t WHERE id = {$presenter};\n\t\t\t\t\t"); $user_id = $p_data[0]['presenters']['user_id']; $presenter_id = $p_data[0]['presenters']['id']; $u_data = $this->User->find('first', ['conditions' => ['id' => $user_id], 'fields' => ['first_name', 'last_name', 'date_of_birth']]); $main_address_data = $this->Address->find('first', ['conditions' => ['user_id' => $user_id, 'address_type_id' => Address::TYPE_MAIN], 'fields' => ['address1', 'address2', 'address3', 'city', 'state_id', 'postal_code', 'country_id', 'address_type_id'], 'order' => 'id DESC']); $shipping_address_data = $this->Address->find('first', ['conditions' => ['user_id' => $user_id, 'address_type_id' => Address::TYPE_SHIPPING], 'fields' => ['address1', 'address2', 'address3', 'city', 'state_id', 'postal_code', 'country_id', 'address_type_id'], 'order' => 'id DESC']); $main_email_data = $this->Email->find('first', ['conditions' => ['user_id' => $user_id, 'email_type_id' => EmailType::MAIN], 'fields' => ['email'], 'order' => 'id DESC']); $royalty_email_data = $this->Email->find('first', ['conditions' => ['user_id' => $user_id, 'email_type_id' => EmailType::ROYALTIES], 'fields' => ['email'], 'order' => 'id DESC']); $ph_data = $this->Phone->find('first', ['conditions' => ['user_id' => $user_id, 'phone_type_id' => Phone::MAIN], 'fields' => ['phone'], 'order' => 'id DESC']); $site_data = $this->PresenterSite->find('first', ['conditions' => ['presenter_id' => $presenter_id], 'fields' => ['site_url'], 'order' => 'id DESC']); $doc_data = $this->PresenterDocuments->find('all', ['conditions' => ['presenter_id' => $presenter_id]]); foreach ($doc_data as $value) { $doc_data = []; $doc_data[] = ['document_id' => $value['PresenterDocuments']['document_id'], 'date_received' => $value['PresenterDocuments']['date_received']]; } $presenter_type = $this->PresenterType->getRecognizedStatus($presenter_id); // put it all together $gpg = new gnupg(); $import = $gpg->import(NETSUITE_PGP_KEY); $gpg->addencryptkey($import['fingerprint']); $enc = $gpg->encrypt($p_data[0][0]['government_id']); $result = ['presenter' => ["id" => (int) $p_data[0]['presenters']['id'], "presenter_sequence_id" => (int) $p_data[0]['presenters']['presenter_sequence_id'], "sponsor_id" => (int) $p_data[0]['presenters']['sponsor_id'], "market_id" => (int) $p_data[0]['presenters']['market_id'], "government_id" => $enc, "consent_to_agreements" => $p_data[0]['presenters']['consent_to_agreements'], "terminated_date" => $p_data[0]['presenters']['terminated_date'], "presenter_status_id" => (int) $p_data[0]['presenters']['presenter_status_id'], "nickname" => $p_data[0]['presenters']['nickname'], "tax_exempt" => $p_data[0]['presenters']['tax_exempt'], "exempt_reasoncode" => $p_data[0]['presenters']['exempt_reasoncode'], "default_locale" => $p_data[0]['presenters']['default_locale'], "presenter_rank" => $presenter_type['name']], 'user' => ['first_name' => $u_data['User']['first_name'], 'last_name' => $u_data['User']['last_name'], 'date_of_birth' => $u_data['User']['date_of_birth']], 'addresses' => [["address1" => $main_address_data['Address']['address1'], "address2" => $main_address_data['Address']['address2'], "address3" => $main_address_data['Address']['address3'], "city" => $main_address_data['Address']['city'], "state" => (int) $main_address_data['Address']['state_id'], "postal_code" => $main_address_data['Address']['postal_code'], "country" => (int) $main_address_data['Address']['country_id'], "address_type_id" => Address::TYPE_MAIN], ["address1" => $shipping_address_data['Address']['address1'], "address2" => $shipping_address_data['Address']['address2'], "address3" => $shipping_address_data['Address']['address3'], "city" => $shipping_address_data['Address']['city'], "state" => (int) $shipping_address_data['Address']['state_id'], "postal_code" => $shipping_address_data['Address']['postal_code'], "country" => (int) $shipping_address_data['Address']['country_id'], "address_type_id" => Address::TYPE_SHIPPING]], 'emails' => [['email_type_id' => EmailType::MAIN, 'email' => $main_email_data['Email']['email']], ['email_type_id' => EmailType::ROYALTIES, 'email' => $royalty_email_data['Email']['email']]], 'phones' => [['phone_type_id' => PhoneType::MAIN, 'phone' => $ph_data['Phone']['phone']]], 'site_url' => $site_data['PresenterSite']['site_url'], 'presenter_documents' => $doc_data]; $results[] = $result; } $this->client->putObject(array('Bucket' => AWS_NETSUITE_SENDING_BUCKET, 'Key' => $folder . DS . 'tmp' . DS . $folder . '_' . time() . '.json', 'Body' => json_encode($results))); $this->out('File uploaded', 1, Shell::VERBOSE); //break; } // update the setting $this->SystemSetting->getDataSource()->reconnect(); $this->SystemSetting->saveSetting('netsuite_presenter_df', $two_seconds_ago); $this->out('System Setting updated', 1, Shell::VERBOSE); }
$_SESSION['LOGIN_ST2_LOGINDATA'] = serialize($loginhandler); // $query = $db->simple_select("users", "uid", "username='******'"); $userid = $db->fetch_field($query, "uid"); $query = $db->query("SELECT fid4 FROM mybb_userfields WHERE ufid=1"); $userPubkey = $db->fetch_field($query, "fid4"); if ($userPubkey == "" || $userPubkey == "None") { error($lang->error_missinggpg); } // Encrypt challenge using user's public key $gpg = new gnupg(); // Import user's pubkey $gpgImportInfo = $gpg->import($userPubkey); // Add encryption key $gpgAddKey = $gpg->addencryptkey($gpgImportInfo['fingerprint']); $encryptedChallenge = $gpg->encrypt($rawChallenge); $plugins->add_hook("member_do_login_end", "add_gpg_vars"); function add_gpg_vars() { global $encryptedChallenge, $rawChallenge, $redirectUrl, $mybb; $redirectUrl = $mybb->input['url']; } $plugins->run_hooks("member_do_login_end"); // GO TO STEP 2 eval("\$login = \"" . $templates->get("member_login_gpg") . "\";"); output_page($login); } } } if ($mybb->input['action'] == "do_gpg_login") { $loginhandler = unserialize($_SESSION['LOGIN_ST2_LOGINDATA']);
/** * @param string $content * @param integer $status * @param array $headers * @return GnuPG\EncryptedSignedResponse */ public function encrypt($content, $status = 200, $headers = []) { return new GnuPG\EncryptedSignedResponse($this->gpg->encrypt($content), $status, $headers); }
/** * GnuPG encrypt a message using the recipient public key and optionally sign * http://devzone.zend.com/article/3753-Using-GnuPG-with-PHP * NOTE: GnuPG must be installed and configured with PHP. * The recipient must be in your public key ring * @param string $recipient Recipient Indentity (e.g. email address) * @param string $message Message to encrypt * @param string $sender Sender Identity * @param string $senderKey Key Sender Secret Key (Only required if signing) * @param boolean $binary Output in binary (non-ASCII armored) * @return string */ public static function _encryptGnuPG($recipient, $message, $sender = FALSE, $senderKey = '', $binary = FALSE) { // Create new GnuPG object $gpg = new \gnupg(); // Set error mode $gpg->seterrormode(\gnupg::ERROR_EXCEPTION); // If binary if ($binary) { // Turn off armored mode $gpg->setarmor(0); } // Add the recipient encryption key $gpg->addencryptkey($recipient); // If there is a sender if ($sender !== FALSE) { // Add signature $gpg->addsignkey($sender, $senderKey); // Return encrypted and signed data return $gpg->encryptsign($message); } // Return encrypted data return $gpg->encrypt($message); }