Simple Javascript protection of HTML content.

This is definitively a highly debatable topic. Most people will argue that Javascript obfuscation, or just obfuscation in general has no purpose in this world. And I actually think they are right.

However, rules are there to be broken, and sometime you would actually want something like this to keep the “basic copy pasters” away from your stuff. But how would you accomplish that in an easy and simple way?

 

Most people just base64 encode / decode, or use eval or even unescape on their strings i Javascript. It works good but the kiddies sometimes get through. So when you want a little higher level of protection, you might want to try this, which I wrote for a friend.

It’s just two simple PHP functions to include in your page. Then you can use the “protected_echo” function to echo out a javascript element with all the code necessary to decrypt the content on the client side. It’s still easy for anyone with a little knowledge of Javascript / HTML  to get to the guts of this, but if you have such highly confidential material laying around on the public internet, you should really revise your strategy!

 

<html>
<head>
<title>Protected demo!</title>
</head>
<body>
<?php 
//Start copying the functions here.
function protect($input) 
{ 
    $tmp = ''; 
    $length = strlen($input); 
    for($i = 0; $i < $length; $i++) 
        $tmp .= '%' . bin2hex($input[$i]); 
    return strrev($tmp); 
} 
function protected_echo($input)
{
echo "<script language=\"javascript\"> 
eval(unescape('" . protect("document.write(unescape(\"".protect($input)."\".split(\"\").reverse().join(\"\")))"). "'.split(\"\").reverse().join(\"\"))); 
</script>";
}
//End copy here.
?> 
 
<p>Some random HTML stuff or anything else here...</p>
 
<?php 
//Down here we are using the protected_echo function.
protected_echo("Protection test!");
 
?>
</body>
</html>

And the resulting HTML code on the client side will be like this:

 
<html>
 
<head>
 
<title>Protected demo!</title>
 
</head>
 
<body>
 
 
 
 
 
<p>Some random HTML stuff or anything else here...</p>
 
 
 
<script language="javascript"> 
 
eval(unescape('92%92%92%22%22%82%e6%96%f6%a6%e2%92%82%56%37%27%56%67%56%27%e2%92%22%22%82%47%96%c6%07%37%e2%22%52%53%03%52%73%23%52%63%66%52%73%43%52%63%53%52%63%33%52%73%43%52%63%93%52%63%66%52%63%56%52%23%03%52%73%43%52%63%53%52%73%33%52%73%43%52%23%13%22%82%56%07%16%36%37%56%e6%57%82%56%47%96%27%77%e2%47%e6%56%d6%57%36%f6%46%'.split("").reverse().join(""))); 
 
</script></body>
 
</html>

Hope you like it, enjoy! ;)

A little class for speech synthesis

Sorry for no posts the last couple of days. I’ve been busy doing all kinds of weird things ;)

I just wanted to share this very simple little class I just made for using the Speech API built into Windows.

It is already fairly simple very simple to use, but I’m the kind of guy who likes everything to be my way. So I wrote this to “decrapify” the API so I just have two simple properties and some simple functions / subs to access it.

Enjoy! :)


Imports System.Speech
Public Class SpeechSynthesis
Private voiceSynthesis As New Synthesis.SpeechSynthesizer()
Private voiceRate As Integer = -2
Private voiceVolume As Integer = 75
'''
''' Used while initializing this object. Called automaticly when making a new instance like: Dim o as new SpeechSenthesis().
'''
'''
Public Sub New()
voiceSynthesis.Volume = voiceVolume
voiceSynthesis.Rate = voiceRate
voiceSynthesis.SelectVoice(voiceSynthesis.GetInstalledVoices()(0).VoiceInfo.Name)
End Sub
'''
''' Used to start speaking a string asynchronously.
'''
''' <span> </span>The text to speak as a string.
''' A boolean value representing whatever to cancel other speaking instances of this object or throwing a
'''
Public Sub speakAsynchronously(ByVal text As String, Optional ByVal forceSpeak As Boolean = False)
If forceSpeak Then
If voiceSynthesis.State = Synthesis.SynthesizerState.Ready Then
voiceSynthesis.SpeakAsync(text)
ElseIf voiceSynthesis.State = Synthesis.SynthesizerState.Speaking Then
voiceSynthesis.SpeakAsyncCancelAll()
voiceSynthesis.SpeakAsync(text)
End If
Else
If voiceSynthesis.State = Synthesis.SynthesizerState.Ready Then
voiceSynthesis.SpeakAsync(text)
Else
Throw New Exception("Already speaking.")
End If
End If
End Sub
'''
''' Used to start speaking a string synchronously.
'''
''' The text to speak as a string.
''' A boolean value representing whatever to cancel other speaking instances of this object or throwing a
'''
Public Sub speakSynchronously(ByVal text As String, Optional ByVal forceSpeak As Boolean = False)
If forceSpeak Then
If voiceSynthesis.State = Synthesis.SynthesizerState.Ready Then
voiceSynthesis.Speak(text)
ElseIf voiceSynthesis.State = Synthesis.SynthesizerState.Speaking Then
voiceSynthesis.SpeakAsyncCancelAll()
voiceSynthesis.Speak(text)
End If
Else
If voiceSynthesis.State = Synthesis.SynthesizerState.Ready Then
voiceSynthesis.SpeakAsync(text)
Else
Throw New Exception("Already speaking.")
End If
End If
End Sub
'''
''' Stops the current asyncronously speaking from this object.
'''
'''
Public Sub stopSpeakingAsynchronously()
If voiceSynthesis.State = Synthesis.SynthesizerState.Speaking Then
voiceSynthesis.SpeakAsyncCancelAll()
End If
End Sub
'''
''' Pauses the currentasyncronously speaking from this object.
'''
'''
Public Sub pauseSpeaking()
If voiceSynthesis.State = Synthesis.SynthesizerState.Speaking Then
voiceSynthesis.Pause()
End If
End Sub
'''
''' Resumes speaking.
'''
'''
Public Sub resumeSpeaking()
If voiceSynthesis.State = Synthesis.SynthesizerState.Paused Then
voiceSynthesis.Resume()
End If
End Sub
'''
''' Sets the volume of this object. Ranging from 0 - 100. Standard is 75.
'''
'''
'''
'''
Public Property volume As Integer
Get
Return voiceVolume
End Get
Set(ByVal value As Integer)
If value &gt; 100 Then
voiceVolume = 100
ElseIf value &lt; 0 Then
voiceVolume = 0
Else
voiceVolume = value
End If
voiceSynthesis.Volume = voiceVolume
End Set
End Property
'''
''' Used to define the voicerate of this object. Ranging from -10 to 10. Standard is -2.
'''
'''
'''
'''
Public Property rate As Integer
Get
Return voiceRate
End Get
Set(ByVal value As Integer)
If value &gt; 10 Then
voiceRate = 10
ElseIf value &lt; -10 Then
voiceRate = -10
Else
voiceRate = value
End If
voiceSynthesis.Rate = voiceRate
End Set
End Property
'''
''' Get's a list of available voices as an array, when you don't need all the BS ;)
'''
'''
'''
Public Function getAvailableVoiceNames() As Array
Dim voiceList(voiceSynthesis.GetInstalledVoices.Count - 1) As String
Dim counter As Integer = 0
For Each voice As Speech.Synthesis.InstalledVoice In voiceSynthesis.GetInstalledVoices
voiceList(counter) = voice.VoiceInfo.Name
counter += 1
Next
Return voiceList
End Function
'''
''' Get's a list of available voices as a list of VoiceInfo when you need everything.
'''
'''
'''
Public Function getAvailableVoices() As List(Of Synthesis.VoiceInfo)
Dim voiceList As New List(Of Synthesis.VoiceInfo)
For Each voice As Speech.Synthesis.InstalledVoice In voiceSynthesis.GetInstalledVoices
voiceList.Add(voice.VoiceInfo)
Next
Return voiceList
End Function
'''
''' Set's the voice based on a name. Standard when creating this object is just the first one available.
'''
'''
'''
Public Sub setVoiceByName(ByVal name As String)
For Each voice As Speech.Synthesis.InstalledVoice In voiceSynthesis.GetInstalledVoices
If voice.VoiceInfo.Name.Trim = name.Trim Then
voiceSynthesis.SelectVoice(name.Trim)
Exit Sub
End If
Next
Throw New Exception("The voice isn't installed on this computer.")
End Sub
End Class

Just put this inside a new class called SpeechSynthesis.vb and begin using it. Everything should be pretty self exploratory, especially with the added XML documentation (okay overkill but I was bored).

Oh and by the way. You will need to add a reference to system.speech like this:


























So there you have it! And for the lazy people I have a download as always:Speech Decrapifyer class (24)

Arduino magnet tracking

I’m back once again. Sorry for being a little late with this post. I had a whole bunch of other things in my head, so I almost forgot posting this. I’m so sorry!

Anyway, let’s get back on the track! Literally! ;)

Let’s start with some background information. I’ve always had this strange dream about a robot, that could run around in my room and perform small tasks automatically while I was gone. And afterwards recharge itself somewhere in the corner of the room.

Lately I’ve become quite good with robotics, and other peripherals. So now I want to make this dream come true, one small step at a time. But in my way, there is several problems to be faced. (And hopefully solved).


  1. Navigating around.
  2. Performing the actual task(s).
  3. Recharging autonomously.
  4. Other functions?



The first problem is navigation. I have looked at various methods, differing from ultrasonic sensors to infrared beacons.

Especially the thing with IR beacons looks good, and some other guys have used it for their own robotics.

(Here: http://www.robotishappy.com/2009/12/beacon-locating-robot-powered-by-arduino-and-ir-transceiver/, and here: http://www.plastibots.com/?p=1594)

But just as good as IR looks, I think it’s quite expensive. The robotic Lego cleaner is using a homebuilt IR transmitter and an Infrared seeker for the NXT robot.

This was actually my first thought, but complete with import taxes and everything, I think that $130 is way to much for a single Lego sensor.While the IR beacons from Pololu.com looks fantastic, they also have a pricetag of $49 dollars excluding shipping, which is still a little to much in my opinion. Maybe if this doesn’t work out, I will try them

.But while thinking about all this I remembered something called “hall effect sensors”. They are basically small sensors, used in everyday electronics, which have the ability to sense magnetic fields. Not like a compass, more like when you keep it close to a fridge magnet.

(See this link for a good in-depth explanation: http://www.sensorwiki.org/doku.php/sensors/hall_effect)

With this new (and awesome) discovery, the idea quickly took shape. First I was thinking about maybe using something like an electric cable in the floor which would when form a magnetic field around it, hopefully big enough for the hall effect sensors to catch on.

While I haven’t completely ruled out this way of doing things yet, I decided to go for something more simple.

But what is simple, strong, reliable and generates a strong magnetic field around it? Magnets of course! But not your everyday magnets. Neodymium magnets is the way to go!

I have yet to receive these magnets from Ebay, but I really hope they will be here soon so I can start putting them down under the carpet.


Let’s move on with the project!

Parts list:

  • 3x A1302 Linear hall effect sensor
  • 3x LED’s
  • 1x Arduino (I used Duemilanove)
  • A breadboard with jumper wires
  • A really strong magnet. A couple of hundreds if you plan on laying them down into the floor like me ;)



The A1302 sensors can be bought here:

http://shop.ebay.com/i.html?_nkw=A1302+hall+effect&_sacat=0&_stpos=&gbr=1

And magnets can be bought here: http://shop.ebay.com/?_kw=rare%20earth%20magnets&_fcid=57&_jgr=1&_localstpos=&_stpos=&gbr=1

Let’s begin by wiring it all up. Here I was trying to make a fancy little circuit diagram, but I failed in lack of good software, and the circuit is so simple. So I hope it will work out.

Start by connecting the 3 hall effect sensors to both ground and 5v on the specified pins.

(Datasheet available here: http://www.allegromicro.com/en/Products/Part_Numbers/1301/1301.pdf )

When connect the output from each sensor to the Arduino’s analog inputs. (I use 0,1,2 in my program).

Proceed and place the LED’s somewhere near each sensor. Connect the cathodes to ground, and anodes to Arduino I/O pin 12, 11 and 10.









In my setup, I have placed the LED on 12, beside the sensor on 0, and the LED on 11, beside the sensor on 1. And last the LED on 10, beside the sensor on 2.

Now you just need to load up this sketch, and when move a magnet around near the sensors. If the LED’s doesn’t light up, try and watch the console output, and check your setup. Also try flipping the magnet itself around, as the sensors is sensitive to each pole.



//Defines 3 variables. One for holding each sensor.
int rawA = 0;
int rawB = 0;
int rawC = 0;
void setup()
{
//Used for debugging only.
Serial.begin(9600);
//Defines the 3 LED's as outputs.
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop()
{
// Here we are reading the 3 sensors into the variables.
rawA = analogRead(0);
rawB = analogRead(1);
rawC = analogRead(2);
//The next block of code can just be removed. This is only for debugging.
Serial.print("A: ");
Serial.print(rawA);
Serial.println();
Serial.print("B: ");
Serial.print(rawB);
Serial.println();
Serial.print("C: ");
Serial.print(rawC);
Serial.println();
Serial.println();
//Here we actually calculates a direction. It's quite simple. Just ask me if you don't get the concept ;)
Serial.print("Direction: ");
if(rawA-10 &gt; rawB &amp;&amp; rawC-10 &gt; rawB) {
//This if goes of when right on track.
Serial.print("Right on track!");
//This is just for LED's. Replace with motor control or whatever.
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
} else if(rawA+10 &lt; rawB) {
//This if goes of when the magnet is being detected to the right. That means that the robot should go right.
Serial.print("Attack the right flank! Aaaarhg!");
//This is just for LED's. Replace with motor control or whatever.
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
} else if(rawC+10 &lt; rawB) {
//This if is fired when magnettrack is to the left.
Serial.print("Move all units to the left!");
//This is just for LED's. Replace with motor control or whatever.
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
} else if((rawB+rawC)/2+10 &lt; rawA) {
//INFORMATION! This if block, is just for showing of. It detects if the magnettrack is getting a little of center to the left. Can be
Serial.print("Magnettrack is a little the left!");
//This is just for LED's. Replace with motor control or whatever.
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
} else if((rawB+rawA)/2+10 &lt; rawC) {
//INFORMATION! This if block, is just for showing of. It detects if the magnettrack is getting a little of center to the right. Can be
Serial.print("Magnettrack is a little to the right!");
//This is just for LED's. Replace with motor control or whatever.
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
} else {
//Down here is when no magnets is being detected. If you know there is a magnet try debugging
//Or changing the magnet difference values. If nothing works, check your circuit or contact me on www.hsp.dk
Serial.print("No magnettrack detected! :(");
//This is just for LED's. Replace with motor control or whatever.
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}
//Writes two blank lines while debugging. Just for a pretty console ;)
Serial.println();
Serial.println();
}