Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:28:17+00:00 2026-05-27T23:28:17+00:00

Lately, I have been doing some research into cryptography. To get a better understanding

  • 0

Lately, I have been doing some research into cryptography.
To get a better understanding of all of it, I have been trying to write a more advanced version of the XOR cypher in PHP.
I got the encryption function to work just fine, but the decryption function output is quite strange and totally different from the message inputted.

The idea of the algorithm is to run a XOR operation first on the first and last character, then on the second and one but last character, and so on.
After that, it runs a XOR operation on the first two characters and the last two characters, then the third and fourth character and the 2th and 3th to last, and so on once again.
This goes on with blocks of 3, 4, 5 and more characters.

The code I have right now:

<?php
function encrypt($message, $key) {
    $output_text = '';

    // Add zeros at the end until the length of the message corresponds with the length of
the key
    $message = str_pad($message,strlen($key),0);

    if((strlen($message) % 2)) {
        // The lenght of the message is odd, add a zero
        $message = $message . 0;
    }

    // Define the final length of the message
    $length = strlen($message);

    // Firstly, take 1 character, then 2, then 3, etc. until you reach half the length of the message
    for($characters=1; $characters<=($length/2); $characters++) {
        // Loop from i til half the length of the message
        for($i=0; $i<=(($length/2)-1); $i += $characters) {
            // Take the first and last character, the the first two and the last two, etc.

            // Stop when it crosses half the length
            if( ($i + $characters ) >= ( $length / 2 ) ) break;

            // firstly, the characters at the beginning
            $beginning = substr($message, $i, $characters);
            for($j=0; $j<$characters; $j++) {
                $position = ( $i + 1 ) + $j;
                $output_text .= chr(ord($beginning{$j}) ^ ord($key{$position}));
            }

            // Then those at the end
            $ending = substr($message, $length-(($i+1) * $characters), $characters);
            for($j=0; $j<$characters; $j++) {
                $position = ( $length - ( ( $i + 1 )* $characters) ) + $j;
                $output_text .= chr(ord($ending{$j}) ^ ord($key{$position}));
            }
        }
    }

    return $output_text;
}
function decrypt($message, $key) {
    $output_text = null;

    // Define the final length of the message
    $length = strlen($message);

    // Firstly, take 1 character, then 2, then 3, etc. until you reach half the length of the message
    for($characters=1; $characters<=($length/2); $characters++) {

        // Loop from i til half the length of the message
        for($i=0; $i<=(($length/2)-1); $i += $characters) {
            // Take the first and last character, the the first two and the last two, etc.

            // Stop when it crosses half the length
            if( ($i + $characters ) >= ( $length / 2 ) ) break;

            // firstly, the characters at the beginning
            $beginning = substr($message, $i, $characters);
            for($j=0; $j<$characters; $j++) {
                $position = ( $i + 1 ) + $j;
                $output_text .= chr(ord($key{$position}) ^ ord($beginning{$j}));
            }

            // The those at the end
            $ending = substr($message, $length-(($i+1) * $characters), $characters);
            for($j=0; $j<$characters; $j++) {
                $position = ( $length - ( ( $i + 1 )* $characters) ) + $j;
                $output_text .= chr(ord($key{$position}) ^ ord($ending{$j}));
            }
        }
    }

    return $output_text;
}

$message = 'sampletextjusttotrythisoutcreatedin2012';
$key = '123';
$output_text = encrypt($message, $key);
echo $output_text . '<br /><hr />';
echo decrypt($output_text, $key);

Thanks in advance for trying to help me!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:28:18+00:00Added an answer on May 27, 2026 at 11:28 pm

    Right now, the hardest part of “decrypting” a string is figuring out how long the input was. If we take that as an additional parameter, we can nearly decrypt it like this:

    function decrypt($cipher, $messagelen, $key) {
        if($messagelen % 2) { $messagelen++; }
        $x = substr($cipher, -$messagelen + 2);
        $y = substr($x, 0, strlen($key) - 1) ^ substr($key, 1);
        $z = substr($x, strlen($key) - 1);
        return $y . $z; 
    }
    

    This is made much easier because most of the message appears in the clear at the end of the ciphertext. Oops. The only characters in that repetition which are “encrypted” are the first few, which are just XORed with the key.

    The middle two characters are irretrievably lost due to an off-by-one error in encryption. Notes on how to fix this are in my comments below.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing some research lately over my work project, i am trying
I have been doing some research lately about best approaches to authenticating web services
I've been doing some Web-Projects lately that rely on heavy Data-Binding and have been
I have been doing some work lately with a Pre-compiled .NET3.5 app so i
I have been doing some catching up lately by reading about cloud hosting. For
Lately I have been doing some numerical method programming in C. For the bug
I have been doing some reading lately one article I read was from Opera.
I have been doing some java development lately and have started using Eclipse. For
I've been playing around with boost::spirit::qi lately and have been trying to write my
Lately I have been interested in better understanding the optimizations done by compiler back-ends.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.