Caesar Cipher
[XML] [Smart Pascal] [JavaScript]
[PHP] [Lua] [Dart] [Output]
This example encrypts lower case letters only by the "Caesar" or "shift" cipher. This moves each letter onwards by a fixed number of letters in the alphabet, looping round from z to a. The unencrypted or decrypted text is known as plaintext and the encrypted form is ciphertext.
See below the blocks, the XML representation of the blocks and the generated Smart Pascal code. We have used the Blockly playground to generate from the same blocks JavaScript, Python, PHP, Lua and Dart.

The blocks
XML Code of Blocks
<xml xmlns="http://www.w3.org/1999/xhtml"> <block type="procedures_defreturn" id="$xTh]8/+)3Hx,^86@Jub" x="-37" y="-737"> <mutation> <arg name="strPlaintext"></arg> <arg name="intDelta"></arg> </mutation> <field name="NAME">Encrypt</field> <comment pinned="false" h="80" w="160">Encrypts with Caesar cipher</comment> <statement name="STACK"> <block type="variables_set" id="|75zxK7zZMue6?2^94SU"> <field name="VAR">strCiphertext</field> <value name="VALUE"> <block type="text" id="tp.IvQt057,Ubp,}o!;]"> <field name="TEXT"></field> </block> </value> <next> <block type="controls_for" id="La7};p(HWo[!+DHvwODR"> <field name="VAR">i</field> <value name="FROM"> <shadow type="math_number" id="8H/l-_jGG=.7kR,dr-e2"> <field name="NUM">1</field> </shadow> </value> <value name="TO"> <shadow type="math_number" id="J[FixDyYdo:!BpI~hq,J"> <field name="NUM">10</field> </shadow> <block type="text_length" id="mqAmXLyJ?f~harsKJRMI"> <value name="VALUE"> <shadow type="text" id="[A8ucx9;)3P%%`j_@5lr"> <field name="TEXT">abc</field> </shadow> <block type="variables_get" id="[Q^PY%Kzg1Lv@-C.aU(J"> <field name="VAR">strPlaintext</field> </block> </value> </block> </value> <value name="BY"> <shadow type="math_number" id="LBO9bAe.9oZ[c3#F4lbF"> <field name="NUM">1</field> </shadow> </value> <statement name="DO"> <block type="variables_set" id="{NCNrJuJ2Vnwqs~S3)84"> <field name="VAR">strCurrentChar</field> <value name="VALUE"> <block type="text_charAt" id="XJFh)2Cz{S-Do|xLHA5+"> <mutation at="true"></mutation> <field name="WHERE">FROM_START</field> <value name="VALUE"> <block type="variables_get" id="2fw04iVKX`ao=9F{7^),"> <field name="VAR">strPlaintext</field> </block> </value> <value name="AT"> <block type="variables_get" id="o!,G(}V$K~-HF|kJuxg3"> <field name="VAR">i</field> </block> </value> </block> </value> <next> <block type="variables_set" id="/PEY$Wo6?qs:}A@.Oii-"> <field name="VAR">intPosition</field> <value name="VALUE"> <block type="text_indexOf" id="#)?jUiMf/{3/*jO+Hm@w"> <field name="END">FIRST</field> <value name="VALUE"> <block type="variables_get" id="9piBu5,P$4.a@)avG1@_"> <field name="VAR">strAlphaLower</field> </block> </value> <value name="FIND"> <shadow type="text" id="|*T4}~ru}Z1^fyOZ=E4D"> <field name="TEXT">abc</field> </shadow> <block type="variables_get" id="a?p4v`(%l!+%a/A=zYpD"> <field name="VAR">strCurrentChar</field> </block> </value> </block> </value> <next> <block type="controls_if" id="mfv=W8]lj5hB9/_b3gmt"> <value name="IF0"> <block type="logic_compare" id="!9HuV?8nurV$Zf]M%hDp"> <field name="OP">LT</field> <value name="A"> <block type="variables_get" id="Chk8)}Js7N*Mwslu5*a1"> <field name="VAR">intPosition</field> </block> </value> <value name="B"> <block type="math_number" id="_z@uS9XDUpK@1^3[AM~`"> <field name="NUM">1</field> </block> </value> </block> </value> <statement name="DO0"> <block type="text_append" id="[.VFyQ#9gwwAF}*^o}n6"> <field name="VAR">strCiphertext</field> <value name="TEXT"> <shadow type="text" id="|7Sn@K|(_m*;;NH]X=QC"> <field name="TEXT"></field> </shadow> <block type="variables_get" id="3/0;-Di|q,B3W6%tXXO6"> <field name="VAR">strCurrentChar</field> </block> </value> <next> <block type="controls_flow_statements" id="TlYVR-GnJb!Y^aX_;:4_"> <field name="FLOW">CONTINUE</field> </block> </next> </block> </statement> <next> <block type="variables_set" id="!V*%n:+,p5q8]bHG~,^t"> <field name="VAR">intPosition</field> <value name="VALUE"> <block type="math_arithmetic" id="Pt|PGS;t@e]vLW,i/8,]"> <field name="OP">ADD</field> <value name="A"> <shadow type="math_number" id="=Qi1nCuhqr~Jc-s(t]=y"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="w4Ms,N%WJ;s/T{]!SN?M"> <field name="VAR">intPosition</field> </block> </value> <value name="B"> <shadow type="math_number" id="[:i7$D%tyjw`T!]T86rg"> <field name="NUM">8</field> </shadow> <block type="variables_get" id="iek|0j%;(:aH3rbW4H/F"> <field name="VAR">intDelta</field> </block> </value> </block> </value> <next> <block type="controls_if" id="?*P:`78+S|OsMrv)varl"> <value name="IF0"> <block type="logic_compare" id="kUVB)L6TvtsYv}#7@.zW"> <field name="OP">GT</field> <value name="A"> <block type="variables_get" id="JwfG:eR{a)8EMD.:p9{v"> <field name="VAR">intPosition</field> </block> </value> <value name="B"> <block type="math_number" id="Af;$CNJNW(G}}4oX2U?i"> <field name="NUM">26</field> </block> </value> </block> </value> <statement name="DO0"> <block type="math_change" id="ZBnxKCW^ynf/s/sxp.@%"> <field name="VAR">intPosition</field> <value name="DELTA"> <shadow type="math_number" id="t4Zfgv+^!;^=bE9LLDFj"> <field name="NUM">1</field> </shadow> <block type="math_number" id="o-xrPa)UIC!*`UN7IG%i"> <field name="NUM">-26</field> </block> </value> </block> </statement> <next> <block type="variables_set" id="IR5qd5eWp+{CX0m7jV3+"> <field name="VAR">strCurrentChar</field> <value name="VALUE"> <block type="text_charAt" id="?f[Ro1)*.w_J/LN:,A)v"> <mutation at="true"></mutation> <field name="WHERE">FROM_START</field> <value name="VALUE"> <block type="variables_get" id="=U``dg_!A7-wAna:(?gI"> <field name="VAR">strAlphaLower</field> </block> </value> <value name="AT"> <block type="variables_get" id="`e?7LI.E#++:QYw#*T[T"> <field name="VAR">intPosition</field> </block> </value> </block> </value> <next> <block type="text_append" id="wec;h{S,07W)kIiaWe#s"> <field name="VAR">strCiphertext</field> <value name="TEXT"> <shadow type="text" id="|7Sn@K|(_m*;;NH]X=QC"> <field name="TEXT"></field> </shadow> <block type="variables_get" id="/zii2=SAn.C`#%Zp:kkj"> <field name="VAR">strCurrentChar</field> </block> </value> </block> </next> </block> </next> </block> </next> </block> </next> </block> </next> </block> </next> </block> </statement> </block> </next> </block> </statement> <value name="RETURN"> <block type="variables_get" id="F2]|JPA5#t,~h9,qOHER"> <field name="VAR">strCiphertext</field> </block> </value> </block> <block type="procedures_defreturn" id="2}gB@{Z+,AxVCjhf,1G{" x="-37" y="-237"> <mutation> <arg name="strCiphertext"></arg> <arg name="intDelta"></arg> </mutation> <field name="NAME">Decrypt</field> <comment pinned="false" h="80" w="160">Decrypts Caesar cipher</comment> <statement name="STACK"> <block type="variables_set" id="L;IBd6$Tkqwzqe`$@`v{"> <field name="VAR">strPlaintext</field> <value name="VALUE"> <block type="text" id="`Do(|!v[vCDgaQrcYwi0"> <field name="TEXT"></field> </block> </value> <next> <block type="controls_for" id="O|/Kh*;^K?t)z3+QGGJ9"> <field name="VAR">i</field> <value name="FROM"> <shadow type="math_number" id="+aGXFjNf10j#b|/Izg-?"> <field name="NUM">1</field> </shadow> </value> <value name="TO"> <shadow type="math_number" id="J[FixDyYdo:!BpI~hq,J"> <field name="NUM">10</field> </shadow> <block type="text_length" id="Z[PHqLJfPPXh0=mbWOFm"> <value name="VALUE"> <shadow type="text" id="[A8ucx9;)3P%%`j_@5lr"> <field name="TEXT">abc</field> </shadow> <block type="variables_get" id="zl2x10Q%ym?vp-xe1FE`"> <field name="VAR">strCiphertext</field> </block> </value> </block> </value> <value name="BY"> <shadow type="math_number" id="Ev]9B/iA`^XTO6lpj,(^"> <field name="NUM">1</field> </shadow> </value> <statement name="DO"> <block type="variables_set" id="y|bvJKj6%X#-4*v*Z+Ff"> <field name="VAR">strCurrentChar</field> <value name="VALUE"> <block type="text_charAt" id="yPJQse3Tk1fN+u#k^w(B"> <mutation at="true"></mutation> <field name="WHERE">FROM_START</field> <value name="VALUE"> <block type="variables_get" id="sAa9$uT,uL.HI#l?sR74"> <field name="VAR">strCiphertext</field> </block> </value> <value name="AT"> <block type="variables_get" id="p:poh;f[~mIaERpm2rYv"> <field name="VAR">i</field> </block> </value> </block> </value> <next> <block type="variables_set" id="AV`[3JI*z#9NgS3z%2H@"> <field name="VAR">intPosition</field> <value name="VALUE"> <block type="text_indexOf" id="c=_7*o9l){2{$SB8rehE"> <field name="END">FIRST</field> <value name="VALUE"> <block type="variables_get" id="#:jU-p9U@vft?y8?RGZ4"> <field name="VAR">strAlphaLower</field> </block> </value> <value name="FIND"> <shadow type="text" id="|*T4}~ru}Z1^fyOZ=E4D"> <field name="TEXT">abc</field> </shadow> <block type="variables_get" id="]G){g#Le;%Ob:eZn.m7g"> <field name="VAR">strCurrentChar</field> </block> </value> </block> </value> <next> <block type="controls_if" id="]g%_L=P4,MO@UtO!cnDh"> <value name="IF0"> <block type="logic_compare" id="}~gB(g*C2Ni)CMT*^3;#"> <field name="OP">LT</field> <value name="A"> <block type="variables_get" id="~gqFuMX/J-#P46_:)@n|"> <field name="VAR">intPosition</field> </block> </value> <value name="B"> <block type="math_number" id="li;Kl7C.7@L/NspL4y(x"> <field name="NUM">1</field> </block> </value> </block> </value> <statement name="DO0"> <block type="text_append" id="l7u*qv#xmli}[O%7)zdZ"> <field name="VAR">strPlaintext</field> <value name="TEXT"> <shadow type="text" id="|7Sn@K|(_m*;;NH]X=QC"> <field name="TEXT"></field> </shadow> <block type="variables_get" id="M-D`QSks.ucP9I;u2KNf"> <field name="VAR">strCurrentChar</field> </block> </value> <next> <block type="controls_flow_statements" id="oBX7;GI-Y3K1-0R@!|!K"> <field name="FLOW">CONTINUE</field> </block> </next> </block> </statement> <next> <block type="variables_set" id="5ANCopDTGfEZnv[]`E}H"> <field name="VAR">intPosition</field> <value name="VALUE"> <block type="math_arithmetic" id="4KL^*Ce;ej|o^rSaeb{+"> <field name="OP">ADD</field> <value name="A"> <shadow type="math_number" id="2?s6xI}9(@fuPgp-**a~"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="7iCF%ByZ62TTh80vHeIX"> <field name="OP">MINUS</field> <value name="A"> <shadow type="math_number" id="=Qi1nCuhqr~Jc-s(t]=y"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="gvR6q`t3{T:=[v`3w=x7"> <field name="VAR">intPosition</field> </block> </value> <value name="B"> <shadow type="math_number" id="[:i7$D%tyjw`T!]T86rg"> <field name="NUM">8</field> </shadow> <block type="variables_get" id="K(wIu0:4|Nk+Q7Eb#;_h"> <field name="VAR">intDelta</field> </block> </value> </block> </value> <value name="B"> <shadow type="math_number" id="U~v_CAK3IZ(c+FMZwK)K"> <field name="NUM">26</field> </shadow> </value> </block> </value> <next> <block type="controls_if" id="S?_}FqD8OY)DnpjU2Spv"> <value name="IF0"> <block type="logic_compare" id="dv8:6/G3s?cQxpI9j!|n"> <field name="OP">GT</field> <value name="A"> <block type="variables_get" id="3g#tu!r}+TI^lmryMAGl"> <field name="VAR">intPosition</field> </block> </value> <value name="B"> <block type="math_number" id="}71*Z9hN#q5d{G:gU8Gt"> <field name="NUM">26</field> </block> </value> </block> </value> <statement name="DO0"> <block type="math_change" id="@G%eD(9vLxno.^WrM^72"> <field name="VAR">intPosition</field> <value name="DELTA"> <shadow type="math_number" id="t4Zfgv+^!;^=bE9LLDFj"> <field name="NUM">1</field> </shadow> <block type="math_number" id=":lO2UhWC28]N!m$Yuuuk"> <field name="NUM">-26</field> </block> </value> </block> </statement> <next> <block type="variables_set" id="yl!fE{$wc[eUf^4=-C$9"> <field name="VAR">strCurrentChar</field> <value name="VALUE"> <block type="text_charAt" id="Md]fg?lai@aNnh8FwQ@d"> <mutation at="true"></mutation> <field name="WHERE">FROM_START</field> <value name="VALUE"> <block type="variables_get" id="8l#HQ4%IQ,30,qWu@X-9"> <field name="VAR">strAlphaLower</field> </block> </value> <value name="AT"> <block type="variables_get" id="ji$S!m;!dlvJlc#jM_Y="> <field name="VAR">intPosition</field> </block> </value> </block> </value> <next> <block type="text_append" id="l}$0|4eg`P+Df7N1m=gv"> <field name="VAR">strPlaintext</field> <value name="TEXT"> <shadow type="text" id="|7Sn@K|(_m*;;NH]X=QC"> <field name="TEXT"></field> </shadow> <block type="variables_get" id="9j_PMuE_7}zS$O6#6qXC"> <field name="VAR">strCurrentChar</field> </block> </value> </block> </next> </block> </next> </block> </next> </block> </next> </block> </next> </block> </next> </block> </statement> </block> </next> </block> </statement> <value name="RETURN"> <block type="variables_get" id="2EPt=/OZJDHQR1n{m@aG"> <field name="VAR">strPlaintext</field> </block> </value> </block> <block type="variables_set" id="=H;=/^f(0A+]r$XA#GvJ" x="-38" y="263"> <field name="VAR">strAlphaLower</field> <value name="VALUE"> <block type="text" id="eF$H#Ry]w_N[!)h@ywgl"> <field name="TEXT">abcdefghijklmnopqrstuvwxyz</field> </block> </value> <next> <block type="text_print" id="trtL@s/6ib1cRfVvwJ)-"> <value name="TEXT"> <shadow type="text" id="(`N_as[X.tNr]IZM#+Gl"> <field name="TEXT">abc</field> </shadow> <block type="procedures_callreturn" id="C_g@f{a/6Bwq`0yQ^.R8"> <mutation name="Encrypt"> <arg name="strPlaintext"></arg> <arg name="intDelta"></arg> </mutation> <value name="ARG0"> <block type="text" id="KfVw~?D8H+dtAUwm#.HC"> <field name="TEXT">abcdefghijklmnopqrstuvwxyz</field> </block> </value> <value name="ARG1"> <block type="math_number" id="YET[Fi^}nxuue.ew21:`"> <field name="NUM">8</field> </block> </value> </block> </value> <next> <block type="text_print" id="+Q@7My+%80PGKRE4z988"> <value name="TEXT"> <shadow type="text" id="nsDP8}j^Gg%Hyq=Q%b70"> <field name="TEXT">abc</field> </shadow> <block type="procedures_callreturn" id="T7.Hfw@9bv|Y?6g~kP;g"> <mutation name="Decrypt"> <arg name="strCiphertext"></arg> <arg name="intDelta"></arg> </mutation> <value name="ARG0"> <block type="variables_get" id="*cGPpxXGVAn[*Dxj][V5"> <field name="VAR">strCiphertext</field> </block> </value> <value name="ARG1"> <block type="math_number" id="7e::tl7~QIdW^n4Dko4["> <field name="NUM">8</field> </block> </value> </block> </value> <next> <block type="text_print" id="-jskN+tsg`E+t}akyp`7"> <value name="TEXT"> <shadow type="text" id="(`N_as[X.tNr]IZM#+Gl"> <field name="TEXT">abc</field> </shadow> <block type="procedures_callreturn" id="MdU4]UEMbY)7B5B~boiJ"> <mutation name="Encrypt"> <arg name="strPlaintext"></arg> <arg name="intDelta"></arg> </mutation> <value name="ARG0"> <block type="text" id="9]2I/B5_9Qha{@nZA[Jh"> <field name="TEXT">The cat sat on the mat.</field> </block> </value> <value name="ARG1"> <block type="math_number" id="TwXP+~UwgZZLY7_Iq_R!"> <field name="NUM">8</field> </block> </value> </block> </value> <next> <block type="text_print" id="5Kd}^Tx/!-V1Y`1vzH?j"> <value name="TEXT"> <shadow type="text" id="nsDP8}j^Gg%Hyq=Q%b70"> <field name="TEXT">abc</field> </shadow> <block type="procedures_callreturn" id="5n=`7w4TepK6@=anQm-x"> <mutation name="Decrypt"> <arg name="strCiphertext"></arg> <arg name="intDelta"></arg> </mutation> <value name="ARG0"> <block type="variables_get" id="]K=ei]brh?X[ED^8yr3}"> <field name="VAR">strCiphertext</field> </block> </value> <value name="ARG1"> <block type="math_number" id="gJlm=jD3pS*HqC#r`#.i"> <field name="NUM">8</field> </block> </value> </block> </value> </block> </next> </block> </next> </block> </next> </block> </next> </block> </xml>
Generated Smart Pascal Code
var intDelta: Integer; var intPosition: Integer; var stCiphertext: Variant; var strAlphaLower: String; var strCiphertext: String; var strCurrentChar: String; var strPlaintext: String; { Encrypts with Caesar cipher } function Encrypt(strPlaintext: String; intDelta: Integer): String; begin strCiphertext := ''; for var i := 1 to length(strPlaintext) step 1 do begin strCurrentChar := CharAt(strPlaintext, (i - 1) + 1); intPosition := Pos(strCurrentChar, strAlphaLower); if ((intPosition < 1)) then begin strCiphertext += strCurrentChar; continue; end; intPosition := intPosition + intDelta; if ((intPosition > 26)) then begin intPosition += -26; end; strCurrentChar := CharAt(strAlphaLower, (intPosition - 1) + 1); strCiphertext += strCurrentChar; end; Result := strCiphertext; end; { Decrypts Caesar cipher } function Decrypt(strCiphertext: String; intDelta: Integer): String; begin strPlaintext := ''; for var i := 1 to length(strCiphertext) step 1 do begin strCurrentChar := CharAt(strCiphertext, (i - 1) + 1); intPosition := Pos(strCurrentChar, strAlphaLower); if ((intPosition < 1)) then begin strPlaintext += strCurrentChar; continue; end; intPosition := (intPosition - intDelta) + 26; if ((intPosition > 26)) then begin intPosition += -26; end; strCurrentChar := CharAt(strAlphaLower, (intPosition - 1) + 1); strPlaintext += strCurrentChar; end; Result := strPlaintext; end; strAlphaLower := 'abcdefghijklmnopqrstuvwxyz'; Console.writeln(Encrypt('abcdefghijklmnopqrstuvwxyz', 8)); Console.writeln(Decrypt(strCiphertext, 8)); Console.writeln(Encrypt('The cat sat on the mat.', 8)); Console.writeln(Decrypt(strCiphertext, 8));
Generated JavaScript Code
var strCiphertext, strCurrentChar, intPosition, strAlphaLower, intDelta, i, strPlaintext; /** * Encrypts with Caesar cipher */ function Encrypt(strPlaintext, intDelta) { strCiphertext = ''; var i_end = strPlaintext.length; var i_inc = 1; if (1 > i_end) { i_inc = -i_inc; } for (i = 1; i_inc >= 0 ? i <= i_end : i >= i_end; i += i_inc) { strCurrentChar = strPlaintext.charAt((i - 1)); intPosition = strAlphaLower.indexOf(strCurrentChar) + 1; if (intPosition < 1) { strCiphertext = String(strCiphertext) + String(strCurrentChar); continue; } intPosition = intPosition + intDelta; if (intPosition > 26) { intPosition = (typeof intPosition == 'number' ? intPosition : 0) + -26; } strCurrentChar = strAlphaLower.charAt((intPosition - 1)); strCiphertext = String(strCiphertext) + String(strCurrentChar); } return strCiphertext; } /** * Decrypts Caesar cipher */ function Decrypt(strCiphertext, intDelta) { strPlaintext = ''; var i_end2 = strCiphertext.length; var i_inc2 = 1; if (1 > i_end2) { i_inc2 = -i_inc2; } for (i = 1; i_inc2 >= 0 ? i <= i_end2 : i >= i_end2; i += i_inc2) { strCurrentChar = strCiphertext.charAt((i - 1)); intPosition = strAlphaLower.indexOf(strCurrentChar) + 1; if (intPosition < 1) { strPlaintext = String(strPlaintext) + String(strCurrentChar); continue; } intPosition = (intPosition - intDelta) + 26; if (intPosition > 26) { intPosition = (typeof intPosition == 'number' ? intPosition : 0) + -26; } strCurrentChar = strAlphaLower.charAt((intPosition - 1)); strPlaintext = String(strPlaintext) + String(strCurrentChar); } return strPlaintext; } strAlphaLower = 'abcdefghijklmnopqrstuvwxyz'; window.alert(Encrypt('abcdefghijklmnopqrstuvwxyz', 8)); window.alert(Decrypt(strCiphertext, 8)); window.alert(Encrypt('The cat sat on the mat.', 8)); window.alert(Decrypt(strCiphertext, 8));
Generated Python Code
from numbers import Number strCiphertext = None strCurrentChar = None intPosition = None strAlphaLower = None intDelta = None i = None strPlaintext = None def upRange(start, stop, step): while start <= stop: yield start start += abs(step) def downRange(start, stop, step): while start >= stop: yield start start -= abs(step) """Encrypts with Caesar cipher """ def Encrypt(strPlaintext, intDelta): global strCiphertext, strCurrentChar, intPosition, strAlphaLower, i strCiphertext = '' i_end = float(len(strPlaintext)) for i in (1 <= i_end) and upRange(1, i_end, 1) or downRange(1, i_end, 1): strCurrentChar = strPlaintext[int(i - 1)] intPosition = strAlphaLower.find(strCurrentChar) + 1 if intPosition < 1: strCiphertext = str(strCiphertext) + str(strCurrentChar) continue intPosition = intPosition + intDelta if intPosition > 26: intPosition = (intPosition if isinstance(intPosition, Number) else 0) + -26 strCurrentChar = strAlphaLower[int(intPosition - 1)] strCiphertext = str(strCiphertext) + str(strCurrentChar) return strCiphertext """Decrypts Caesar cipher """ def Decrypt(strCiphertext, intDelta): global strCurrentChar, intPosition, strAlphaLower, i, strPlaintext strPlaintext = '' i_end2 = float(len(strCiphertext)) for i in (1 <= i_end2) and upRange(1, i_end2, 1) or downRange(1, i_end2, 1): strCurrentChar = strCiphertext[int(i - 1)] intPosition = strAlphaLower.find(strCurrentChar) + 1 if intPosition < 1: strPlaintext = str(strPlaintext) + str(strCurrentChar) continue intPosition = (intPosition - intDelta) + 26 if intPosition > 26: intPosition = (intPosition if isinstance(intPosition, Number) else 0) + -26 strCurrentChar = strAlphaLower[int(intPosition - 1)] strPlaintext = str(strPlaintext) + str(strCurrentChar) return strPlaintext strAlphaLower = 'abcdefghijklmnopqrstuvwxyz' print(Encrypt('abcdefghijklmnopqrstuvwxyz', 8)) print(Decrypt(strCiphertext, 8)) print(Encrypt('The cat sat on the mat.', 8)) print(Decrypt(strCiphertext, 8))
Generated PHP Code
$strCiphertext; $strCurrentChar; $intPosition; $strAlphaLower; $intDelta; $i; $strPlaintext; function length($value) { if (is_string($value)) { return strlen($value); } else { return count($value); } } function text_indexOf($text, $search) { $pos = strpos($text, $search); return $pos === false ? 0 : $pos + 1; } // Encrypts with Caesar cipher function Encrypt($strPlaintext, $intDelta) { global $strCiphertext, $strCurrentChar, $intPosition, $strAlphaLower, $i; $strCiphertext = ''; $_i_end = length($strPlaintext); $_i_inc = 1; if (1 > $_i_end) { $_i_inc = -$_i_inc; } for ($i = 1; $_i_inc >= 0 ? $i <= $_i_end : $i >= $_i_end; $i += $_i_inc) { $strCurrentChar = substr($strPlaintext, ($i - 1), 1); $intPosition = text_indexOf($strAlphaLower, $strCurrentChar); if ($intPosition < 1) { $strCiphertext .= $strCurrentChar; continue; } $intPosition = $intPosition + $intDelta; if ($intPosition > 26) { $intPosition += -26; } $strCurrentChar = substr($strAlphaLower, ($intPosition - 1), 1); $strCiphertext .= $strCurrentChar; } return $strCiphertext; } // Decrypts Caesar cipher function Decrypt($strCiphertext, $intDelta) { global $strCurrentChar, $intPosition, $strAlphaLower, $i, $strPlaintext; $strPlaintext = ''; $_i_end2 = length($strCiphertext); $_i_inc2 = 1; if (1 > $_i_end2) { $_i_inc2 = -$_i_inc2; } for ($i = 1; $_i_inc2 >= 0 ? $i <= $_i_end2 : $i >= $_i_end2; $i += $_i_inc2) { $strCurrentChar = substr($strCiphertext, ($i - 1), 1); $intPosition = text_indexOf($strAlphaLower, $strCurrentChar); if ($intPosition < 1) { $strPlaintext .= $strCurrentChar; continue; } $intPosition = ($intPosition - $intDelta) + 26; if ($intPosition > 26) { $intPosition += -26; } $strCurrentChar = substr($strAlphaLower, ($intPosition - 1), 1); $strPlaintext .= $strCurrentChar; } return $strPlaintext; } $strAlphaLower = 'abcdefghijklmnopqrstuvwxyz'; print(Encrypt('abcdefghijklmnopqrstuvwxyz', 8)); print(Decrypt($strCiphertext, 8)); print(Encrypt('The cat sat on the mat.', 8)); print(Decrypt($strCiphertext, 8));
Generated Lua Code
function firstIndexOf(str, substr) local i = string.find(str, substr, 1, true) if i == nil then return 0 else return i end end -- Encrypts with Caesar cipher function Encrypt(strPlaintext, intDelta) strCiphertext = '' i_inc = 1 if (1) > (#strPlaintext) then i_inc = -i_inc end for i = 1, #strPlaintext, i_inc do strCurrentChar = string.sub(strPlaintext, i, i) intPosition = firstIndexOf(strAlphaLower, strCurrentChar) if intPosition < 1 then strCiphertext = strCiphertext .. strCurrentChar goto continue end intPosition = intPosition + intDelta if intPosition > 26 then intPosition = intPosition + -26 end strCurrentChar = string.sub(strAlphaLower, intPosition, intPosition) strCiphertext = strCiphertext .. strCurrentChar ::continue:: end return strCiphertext end -- Decrypts Caesar cipher function Decrypt(strCiphertext, intDelta) strPlaintext = '' i_inc2 = 1 if (1) > (#strCiphertext) then i_inc2 = -i_inc2 end for i = 1, #strCiphertext, i_inc2 do strCurrentChar = string.sub(strCiphertext, i, i) intPosition = firstIndexOf(strAlphaLower, strCurrentChar) if intPosition < 1 then strPlaintext = strPlaintext .. strCurrentChar goto continue end intPosition = (intPosition - intDelta) + 26 if intPosition > 26 then intPosition = intPosition + -26 end strCurrentChar = string.sub(strAlphaLower, intPosition, intPosition) strPlaintext = strPlaintext .. strCurrentChar ::continue:: end return strPlaintext end strAlphaLower = 'abcdefghijklmnopqrstuvwxyz' print(Encrypt('abcdefghijklmnopqrstuvwxyz', 8)) print(Decrypt(strCiphertext, 8)) print(Encrypt('The cat sat on the mat.', 8)) print(Decrypt(strCiphertext, 8))
Generated Dart Code
var strCiphertext, strCurrentChar, intPosition, strAlphaLower, intDelta, i, strPlaintext; /// Encrypts with Caesar cipher dynamic Encrypt(strPlaintext, intDelta) { strCiphertext = ''; var i_end = strPlaintext.length; num i_inc = 1; if (1 > i_end) { i_inc = -i_inc; } for (i = 1; i_inc >= 0 ? i <= i_end : i >= i_end; i += i_inc) { strCurrentChar = strPlaintext[(i - 1)]; intPosition = strAlphaLower.indexOf(strCurrentChar) + 1; if (intPosition < 1) { strCiphertext = [strCiphertext, strCurrentChar].join(); continue; } intPosition = intPosition + intDelta; if (intPosition > 26) { intPosition = (intPosition is num ? intPosition : 0) + -26; } strCurrentChar = strAlphaLower[(intPosition - 1)]; strCiphertext = [strCiphertext, strCurrentChar].join(); } return strCiphertext; } /// Decrypts Caesar cipher dynamic Decrypt(strCiphertext, intDelta) { strPlaintext = ''; var i_end2 = strCiphertext.length; num i_inc2 = 1; if (1 > i_end2) { i_inc2 = -i_inc2; } for (i = 1; i_inc2 >= 0 ? i <= i_end2 : i >= i_end2; i += i_inc2) { strCurrentChar = strCiphertext[(i - 1)]; intPosition = strAlphaLower.indexOf(strCurrentChar) + 1; if (intPosition < 1) { strPlaintext = [strPlaintext, strCurrentChar].join(); continue; } intPosition = (intPosition - intDelta) + 26; if (intPosition > 26) { intPosition = (intPosition is num ? intPosition : 0) + -26; } strCurrentChar = strAlphaLower[(intPosition - 1)]; strPlaintext = [strPlaintext, strCurrentChar].join(); } return strPlaintext; } main() { strAlphaLower = 'abcdefghijklmnopqrstuvwxyz'; print(Encrypt('abcdefghijklmnopqrstuvwxyz', 8)); print(Decrypt(strCiphertext, 8)); print(Encrypt('The cat sat on the mat.', 8)); print(Decrypt(strCiphertext, 8)); }
Copy of Output
ijklmnopqrstuvwxyzabcdefgh abcdefghijklmnopqrstuvwxyz Tpm kib aib wv bpm uib. The cat sat on the mat.