r/badcode • sadistic • May 07 '21

js this extends for around 980 lines...

Post image
904 Upvotes

42 comments sorted by

129

u/fletku_mato May 07 '21

I sure hope that it's autogenerated.

84

u/SpeckyYT sadistic May 07 '21

Not so sure about that

71

u/wischichr May 07 '21

Even if autogenerated that's a pretty good candidate for shitty.

4

u/OneTrueKingOfOOO May 08 '21

I don’t. Any code that would generate this is somehow even more broken than this is

38

u/ShatterdPrism May 07 '21

What does this want to accomplish?

73

u/SpeckyYT sadistic May 07 '21

Basically it's a "gibberish text" converter. It basically swaps one letter with another one.

11

u/_-Shimo-_ May 07 '21

But couldn’t you just do a for loop for the amount of char’s in the string and replace it with a random character? I only program in C# and there it’d just be:

Random r = new Random(); for (int i = 0; i < str.length; i++) { str[i] = (char)r.Next(0,256); }

I think at least can’t error check because I’m on my phone.

8

u/SpeckyYT sadistic May 07 '21

I guess it was too easy to do it like that for the devs

6

u/[deleted] May 07 '21

It's not random letters, it's encryption. The correct way to do bootleg encryption like this would probably be to just use a map. Even better, convert the character to ASCII code and add some number to it, or do some other consistent math operation.

2

u/_-Shimo-_ May 07 '21

Ooh thought the bootleg encryption was joke, I believe my bootleg way was charkey, so if char is ‘a’ (has an ascii value of 97 I believe) you’d do 9742 or something, and I believe you’d do the same to the new value to get it back to regular text but I can be wrong as the last time I did something like this must be like 3 years ago hahaha

1

u/BringAltoidSoursBack May 07 '21

Iirc, most encryption uses random as part of their encryption scheme but I guess that might make it no longer bootleg.

1

u/[deleted] May 07 '21

It may use a random hash key but the encryption takes that hash key and performs a specific set of non random operations based on it.

How would you ever decrypt anything if the data was randomly scrambled around?

72

u/xDarkFlame25 May 07 '21

So... bootleg dollar store encryption?

69

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); May 07 '21

Sounds like it. When I was a teen some friends and friends-of-friends hung out in a chatroom together a lot; it was kind of a loose group where everyone was friends with someone else but not everyone was friends to each other, and a few of them did something similar. You could send private messages but that wasn't good enough for those few people, they wanted others to know they were talking about you (or at least wonder) so they made this horrible custom substitution cipher and ran all their text through it, and while I never saw the code (I wasn't friends with that particular sub-group) the person that made it was terrible so I wouldn't be surprised if it looked like that as well.

They were trying to make people feel left out but it ended up being a lot of fun (at least to me) because it turned into an escalating nerd war after a friend and I took a few text samples and figured out the cipher. They kept trying to change it and make it better but it was always the same horrible basic substitution like OP's screenshot so it never took more than a few text samples to break and then distribute to everyone so the cipher became worthless.

We ended up making a different bootleg encryption scheme to fuck with them back, too. Still essentially just a substitution cipher but did it by manipulating ASCII values based on per-message, randomly-chosen values, with the cipher used changing throughout the message and each message using different values as well to complicate letter frequency detection. Once you knew the trick (magic characters in the message) it was trivial, but it took a long time before someone eventually figured it out...long after the dumb nerd fight was over.

We were really proud of that stupid bit of code and the line noise it generated because it infuriated the ones that started the whole thing.

30

u/JuhaJGam3R May 07 '21

It's incredible how simple is nearly unbreakable. The Engima machine was just text substitution with a little shifting and it was so incredibly nearly unbreakable that were it not for the fact that a letter could never map to itself it could have been a very different war.

23

u/Spare_Competition May 07 '21

The enigma machine is still pretty hard to crack even with modern computers, you have to use advanced algorithms, because you can’t brute force it

355

u/MalbaCato May 07 '21

I especially love how the only argument to the function gets immediately overwritten by a globally stored value

84

u/TheSchred May 07 '21

Truly genius

27

u/devhashtag May 07 '21

He ran out of var declarations

122

u/RaidensAFGenerator May 07 '21

The function probably refers to the intended employment status of the programmer who wrote it.

5

u/-shayne May 07 '21

Came here to say this, this belongs to r/darkprogrammers (only if it was a thing of course)

3

u/[deleted] May 07 '21

Oh, nice. Finally a place to program while showing off my sick tan.

28

u/daymerc May 07 '21

When you are new to RegEx

32

u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 07 '21

Image Transcription: Code


function replaceMe(sText)
{
    sText = document.frmTranslator.UserText.value;
    
    var pattern = /r/g;
    var newString = sText.replace(pattern,"�");
    var sText=newString
    var pattern = /l/g;
    var newString = sText.replace(pattern,"r");
    var sText=newString
    var pattern = /�/g;
    var newString = sText.replace(pattern,"l");
    var sText=newString
    
    var pattern = /R/g;
    var newString = sText.replace(pattern,"�");
    var sText=newString
    var pattern = /L/g;
    var newString = sText.replace(pattern,"R");
    var sText=newString
    var pattern = /�/g;
    var newString = sText.replace(pattern,"L");
    var sText=newString
    
    var pattern = /e/g;
    var newString = sText.replace(pattern,"�");
    var sText=newString
    var pattern = /o/g;

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

10

u/glebbash May 07 '21

Good human?

9

u/[deleted] May 07 '21

They're coding like, "Damn these expressions are sure becoming regular, they should have something for this."

7

u/gdsgbc May 07 '21

It’s encapsulated in a function so it’s cool

4

u/Urtehnoes May 07 '21

Ha, this reminds me of my job's production code which has some oddities because let's face it - the company couldn't afford to constantly upgrade stuff back in the day when they were starting.

So now I have the joys of using /r/n in someplaces, then find replacing those for 'CRLF' when represented elsewhere, then replacing those for CHR(13) || CHR(10) in others, followed by swapping those out for /r/n again smh.

I wouldn't say it's necessarily bad code it just sucks when you have to have a 24/7 365 available product. Keeping everything lined up when it's not a tech focused industry suuuuuucks.

EDIT: I will say though the tech who used CRLF needs to go die in a fire. The tech stack that part is involved in is absolutely capable of at least using CHR(13) + CHR(10), though it couldn't use /r/n.

1

u/IsleOfOne May 08 '21

Can you not define a convention for line endings on a project/tech stack basis and automate this with a pre-commit hook? This seems horribly inefficient and entirely avoidable based solely on your description.

2

u/Urtehnoes May 08 '21

Not when the tech stack includes things like a 1980s ISAM database. The newest piece of tech is an oracle database that is easily over 10 years old.

And you're misunderstanding - these aren't line endings in a page of code, it's data itself.

The custom application supports CRLF, the 3rd party JSON plsql library supports chr(13)chr(10) one way but not another way, and without the JSON library our database can't connect to a 3td party vendor via api due to well...

The limitations of 40+ year old business goes on.

I get envious when people post stuff on here like code bases that can operate in modern ide (Google oracle forms 10g and welcome to hell).

Then again I get a ton of money so maybe I shouldn't complain as much

1

u/IsleOfOne May 08 '21

Damn dude, I’d be driven insane trying to get buy-in to replace or augment the input to/output from that JSON library.

1

u/Urtehnoes May 08 '21

I just want an IDE with autocomplete :(

Or a SQL based application that doesn't crash if I use the JOIN keyword, and forget and click save :(

4

u/TinyBreadBigMouth May 07 '21
const swapMapping = {
    r: 'l', l: 'r',
    R: 'L', L: 'R',
    e: 'o', o: 'e',
};

function replaceChars(text)
{
    return text.replace(/./g, c => swapMapping[c] ?? c);
}

2

u/Spynder shameless stealing content to r/YandereTechnique May 07 '21

Quite extreme r/YandereTechnique

2

u/VirdiPravum May 07 '21

POV: you are payed by the lines of code

1

u/Jacob29687 May 08 '21

As with most posts on this sub, I'd be interested in knowing how to do this the right way. Just shows how much experience I have lol. I am just a senior in highschool though so I guess I have an excuse

2

u/MalbaCato May 08 '21

there are two parts to the problem: finding a way to apply the mapping on each letter and finding a good map to use (code in post does neither correctly)

this is JS so for the first problem I would've done str.split().map(c => ...).join(), tho a commenter above me have pointed out that str.replace can take a function as the second argument instead of a static string.

for the second part of the problem a number of techniques can be used. a key-value pairs object like that same commenter has used, a new Map() with similar structure if you're feeling fancy, an array based on the ASCII values as lookup indices, an array or string with the new alphabet order if it's only letters, etc

if you care about speed you benchmark everything you can think of (that's also readable) and use the best. if you don't then just choose whichever seems appropriate.

0

u/[deleted] May 07 '21

[deleted]

8

u/ezethnesthrown May 07 '21

Pretty sure this is JavaScript

6

u/SpeckyYT sadistic May 07 '21

Yes, it's JS