Skip to content

Instantly share code, notes, and snippets.

@carcabot
Last active May 16, 2016 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carcabot/b7df7656f1072017f7098324fa9389a7 to your computer and use it in GitHub Desktop.
Save carcabot/b7df7656f1072017f7098324fa9389a7 to your computer and use it in GitHub Desktop.
Encoding/Decoding Integer or String
<?php
/**
* Encode/Decode a string/integer
*
* @author CarcaBot
*
* @param string $text
* @return string
*/
function mb_str_split($string) { // used for utf8 strings
# Split at all position not after the start: ^
# and not before the end: $
return preg_split('/(?<!^)(?!$)/u', $string);
}
function encode($str = "") {
$string = "";
foreach (mb_str_split($str) as $char) {
$b = ord($char);
if ($b >= 97 && $b <= 109 || $b >= 65 && $b <= 77) {
$b += 13;
} elseif ($b >= 110 && $b <= 122 || $b >= 78 && $b <= 90) {
$b -= 13;
}
$string .= chr($b);
}
return $string;
}
function decode($str = "") {
return encode($str);
}
$str = "SecretWord";
$encoded = encode($str);
$decoded = decode($str);
echo sprintf("Encoded: %s \r\n<br/>Decoded: %s", encode($str), decode($encoded));
/*
result:
Encoded: FrpergJbeq
Decoded: SecretWord
*/
'Encode/Decode a string/integer
'@author CarcaBot
'@param string $text
'@return string
Public Shared Function encode(ByVal Text As String) As String
Dim Buffer As New System.Text.StringBuilder
For Each C As Char In Text
Dim B As Integer = Microsoft.VisualBasic.AscW(C)
If B >= 97 And B <= 109 Or B >= 65 And B <= 77 Then
B += 13
ElseIf B >= 110 And B <= 122 Or B >= 78 And B <= 90 Then
B -= 13
End If
Buffer.Append(Microsoft.VisualBasic.ChrW(B))
Next
Return Buffer.ToString
End Function
Public Shared Function decode(ByVal Text As String) As String
return encode(Text)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment