【发布时间】:2018-03-17 21:09:36
【问题描述】:
我最近开始在 MATLAB 中编程,并尝试实现一个类似 1337 扬声器的发生器/弦乐操纵器,只是为了好玩。作为一个挑战,我尝试随机更改每个字符的出现,以便并非所有“a”都更改为“@”。
我的尝试似乎在某种程度上奏效了,因为它非常武断(但有时无效),但我相信有更好的方法来实现这一点。或许可以为 26 个字符中的每一个添加更多替代项并分别从中随机选择?
function O = obfuscate(s)
str = 'hello world';
for i=1:length(str)
randomNum = randi(26,1);
switch randomNum
case 1
str = regexprep(str, 'a', '@', 'once');
case 2
str = regexprep(str, 'b', 'l3', 'once');
case 3
str = regexprep(str, 'c', '<', 'once');
case 4
str = regexprep(str, 'd', '|]', 'once');
case 5
str = regexprep(str, 'e', '3', 'once');
case 6
str = regexprep(str, 'f', '|#', 'once');
case 7
str = regexprep(str, 'g', '6', 'once');
case 8
str = regexprep(str, 'h', '|-|', 'once');
case 9
str = regexprep(str, 'i', '!', 'once');
case 10
str = regexprep(str, 'j', '_/', 'once');
case 11
str = regexprep(str, 'k', '|{', 'once');
case 12
str = regexprep(str, 'l', '1', 'once');
case 13
str = regexprep(str, 'm', '|\/|', 'once');
case 14
str = regexprep(str, 'n', '/\/', 'once');
case 15
str = regexprep(str, 'o', '[]', 'once');
case 16
str = regexprep(str, 'p', '|*', 'once');
case 17
str = regexprep(str, 'q', '9', 'once');
case 18
str = regexprep(str, 'r', '|2', 'once');
case 19
str = regexprep(str, 's', '$', 'once');
case 20
str = regexprep(str, 't', '+', 'once');
case 21
str = regexprep(str, 'u', '|_|', 'once');
case 22
str = regexprep(str, 'v', '\/', 'once');
case 23
str = regexprep(str, 'w', '\X/', 'once');
case 24
str = regexprep(str, 'x', '%', 'once');
case 25
str = regexprep(str, 'y', '¥', 'once');
case 26
str = regexprep(str, 'z', '2', 'once');
end
O = str;
%fprintf('%s(%i)', str(i), randomNum);
end
有什么建议吗?
【问题讨论】:
-
我不明白您是如何决定选择替代品的?您能在这里向我们展示简单的输入和预期输出吗?
-
当然。我已将此函数硬编码为使用
str = 'hello world',但您可以将其更改为str = s,以便在运行混淆('your text here')时接受参数。根据要求,为“hello world”运行此函数可能会产生不同的结果,因为它是随机的。一种可能的结果是:|-|3llo w[]rld -
so that not all “a” are changed to “@“... 好的,但是“hello world”与“not all a”有什么关系? -
我很抱歉。这是一个通用的例子。我应该说不是所有的“e”都变成了“3”。
标签: regex string matlab random