LlXorBase64StringsCorrect

De DigiWiki.

default
{
    state_entry(){
 
        // Use a HARD password ! with caps nocaps numbers and symbols !
        string pass = "P4s5Wo_rD";
 
        string data = "I am some ver important data.";
 
        // Enccrypting the data:
        string crypt = llXorBase64StringsCorrect(llStringToBase64(data), llStringToBase64(pass));
 
        // Say the mess you made to Owner
        llOwnerSay(crypt);
 
        // DeCrypting the data and say back to owner:
        llOwnerSay(llBase64ToString(llXorBase64StringsCorrect(crypt, llStringToBase64(pass))));
 
    }
 
}
// Stronger encryption - generates a random encrypted string.
//safe character set
string ASCII = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
//convert integer to characters
string chr(integer i)
{
    return llGetSubString(ASCII, i, i);
}
 
//for generating a random string.
string salt(integer amount) {
    string salt = "";
    integer i;
    //for length of salt , generate characters
    for(i = 0; i < amount; i++) {
        salt += chr((integer)llFrand(llStringLength(ASCII)));
    }
    return salt;
}    
 
default
{
    touch_start(integer n) {
        //generates a random salt to add to the string.
        //put salt on the end so that even if the data is corrupted by SVC-6362, it is unimportant data
        string data = "I am some very important data." + salt((integer)llFrand(5)+ 5);
 
        string pass = "password";
 
        // Encrypting the data:
        string crypt = llXorBase64StringsCorrect(llStringToBase64(data), llStringToBase64(pass));
 
        // Say the mess you made to Owner
        llOwnerSay(crypt);
 
        // DeCrypting the data and say back to owner: Remember to remove the salt when needed ;)
        llOwnerSay(llBase64ToString(llXorBase64StringsCorrect(crypt, llStringToBase64(pass))));
 
    }
}

How to decode with php

<?php
function llXorBase64StringsCorrect($s1,$s2){
# Description: Implementation of the LSL2 function llXorBase64StringsCorrect()
# Author: SignpostMarv Martin
# Released under http://creativecommons.org/licenses/by-sa/2.5/
	$l1 = strlen($s1 = base64_decode($s1));
	$s2 = base64_decode($s2);
	return base64_encode($s1 ^ (strlen($s1) > strlen($s2) ? str_pad($s2, $l1, $s2, STR_PAD_RIGHT) : $s2));
}
?>

Implementation of SignpostMarv's decoder in PHP.

<?php
/* 
 * Description: Implementation of SignpostMarv's php base64 decoder.
 * Author: Kopilo Hallard
 * License: http://creativecommons.org/licenses/by-sa/2.5/
 */
 
/* Cipher (s1 data, s2 key) */
function llXorBase64StringsCorrect($s1, $s2) {
    $s1 = base64_decode($s1); $l1 = strlen($s1);
    $s2 = base64_decode($s2);
    if($l1 > strlen($s2)) $s2 = str_pad($s2, $l1, $s2, STR_PAD_RIGHT);
    return base64_encode($s1 ^ $s2);
}
 
$Skey = "password";
$para1 = $_POST["para1"];
 
$result = llXorBase64StringsCorrect($para1, base64_encode($Skey));
 
echo "Encrypted Data: ".$para1.PHP_EOL;
echo "Unencrypted ".base64_decode($result);
?>

The function behaves actually like the following PHP code:

<?php
function llXorBase64StringsCorrect($s1, $s2) {
    $s1 = base64_decode($s1); $l1 = strlen($s1);
    $s2 = base64_decode($s2);
    if (($pos = strpos($s2, "\0")) !== false) $s2 = substr($s2, 0, $pos);
    if ($s2 == "") $s2 = "\0";
    if($l1 > strlen($s2)) $s2 = str_pad($s2, $l1, $s2, STR_PAD_RIGHT);
    return base64_encode($s1 ^ $s2);
}
?>

How to decode with java

Remember to URLEncode your BASE64 hash if you transfer it vie GET...

       String BASE64datahash = "error";
       String passhash = "error";
 
       try {
        //URLDecode the URL encoded encrypted data
        BASE64datahash = java.net.URLDecoder.decode("KhoFRRYaAUMbEVU%3D", "UTF-8"); //KhoFRRYaAUMbEVU%3D
        System.out.println("BASE64datahash: " + BASE64datahash); //KhoFRRYaAUMbEVU=
 
        //create an array of BASE64 data
        char[] BASE64data = BASE64datahash.toCharArray();
        char[] dataUB = new String(new BASE64Decoder().decodeBuffer(new String(BASE64data))).toCharArray(); //BASE64 decode the data
        System.out.println("encrypted data (but base64 decoded) [dataUB]: " + new String(dataUB));
 
 
        //Encode the secred key/password to BASE64 (Just to show how to use BASE64Encoder)
        //String BASE64password = new String(new BASE64Encoder().encodeBuffer("supersecretpassword".getBytes()));
        //System.out.println("BASE64password: " + new String(BASE64password));
 
        //create array of BASE64 key/password
        //char[] key = BASE64password.toCharArray();
        //char[] keyUB = new String(new BASE64Decoder().decodeBuffer(new String(key))).toCharArray();
        char[] keyUB = "supersecretpassword".toCharArray(); 
        System.out.println("plaintext key/password [keyUB]: " + new String(keyUB));
 
        //XOR data array chars with corresponding key/password array chars
        int k=0;
        for (int i = 0; i < dataUB.length; i++) {
            dataUB[i] = (char) (dataUB[i] ^ keyUB[k]);
            k++;
 
            //Loop to start of the key if the key is too short
            if (k == keyUB.length)
               k=0;
		}
 
        System.out.println("Decoded data [dataUB]: " + new String(dataUB));
 
        } catch (Exception ex) {
            System.out.println("Oops!");
        }
Outils personnels
  • Cette page a été consultée 1 768 fois.
donate
Google Ads