1

I am trying to create a an encryption in MySQL. Lets say there is a string of characters.

"I can run for as many".

I want to replace each letter with its fourth letter. For example,

'a' replaced with 'e'
'b' replaced with 'f'
and so on.

The Final output of the above would look something like this. "M gen vyr jsr ew qerc"

The best I could come up is the below. Since it is a nested function, it is not giving me the right result. The below function replaces 'a' with 'e' and then again replaces 'e' with 'i' until it reaches the end.

select messagetext, 
replace(replace(replace(replace(replace(replace(replace(replace
(replace(replace(replace(replace(replace(replace(replace
(replace(replace(replace(replace(replace(replace(replace
(replace(replace(replace(replace(messagetext,'a','e'),
'b','f'),'c','g'),'d','h'),'e','i'),'f','j'),'g','k'),
'h','l'),'i','m'),'j','n'),'k','o'),'l','p'),'m','q'),
'n','r'),'o','s'),'p','t'),'q','u'),'r','v'),'s','w'),
't','x'),'u','y'),'v','z'),'w','a'),'x','b'),'y','c'),
'z','d')
from chat;

Any help would be much appreciated.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76

1 Answers1

1

Reached partial solution, now stuck.

Putting it here so that others can work on this.

Query:

SELECT UNHEX(HEX(val) + REPEAT('04', LENGTH(val))) AS rot4 FROM caeser;

+--------+
| rot4   |
+--------+
| efghip |
| aq??   | <-- Need to rotate / mod hex value for this. (Stuck here)
+--------+

Table create queries:

CREATE TABLE caeser (val VARCHAR(255));
INSERT INTO caeser ('abcdef');
INSERT INTO caeser VALUES ('uvwxyz');

PS: will convert to community wiki, if others also contribute.

Sangharsh
  • 2,999
  • 2
  • 15
  • 27