Skip to content

Instantly share code, notes, and snippets.

@damianknopp
Last active February 28, 2023 21:47
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 damianknopp/3a8c2ae3efe9edd46d9378a378a2592f to your computer and use it in GitHub Desktop.
Save damianknopp/3a8c2ae3efe9edd46d9378a378a2592f to your computer and use it in GitHub Desktop.
decode rot N
import java.nio.file.*;
Path p1 = Path.of("rot.txt");
String cipher = Files.readString(p1);
BiConsumer<String, Integer> decode = (cipher, rot) -> {
char[] cipherArray = cipher.toLowerCase().toCharArray();
for (int i = 0; i < cipherArray.length; i++) {
if (!Character.isLetter(cipherArray[i])) {
continue;
}
char currentChar = cipherArray[i];
int peek = (currentChar - 'a' - rot);
int shift = peek > 0 ? peek : (26 - Math.abs(peek));
int minimumShift = shift % 26;
cipherArray[i] = (char)(minimumShift + 'a');
}
String plainText = new String(cipherArray);
System.out.println(plainText);
};
decode.accept("abbc", 13);
decode.accept("tqxxa iadxp", 12);
decode.accept("owwljgm jtcm asg", 8);
decode.accept("znk cnkkry ut znk hay", 6);
for (int i = 1; i < 26; i++) {
System.out.println("decode message with shift: " + i);
decode.accept(cipher, i);
System.out.println("-".repeat(20));
}
@damianknopp
Copy link
Author

output is

p1 ==> /rot.txt
cipher ==> "B'O YGJTRY ROZGYYGCCRE, VRXRYGQ CIQI, ZNJ BJ GAARGYC JTGJ\n\n- H?AI"
decode ==> $Lambda$106/0x0000000800c9c5b0@30b8a058
noop
hello world
goodbye blue sky
the wheels on the bus
decode message with shift: 1
a'n xfisqx qnyfxxfbbqd, uqwqxfp bhph, ymi ai fzzqfxb isfi

- g?zh
--------------------
decode message with shift: 2
z'm wehrpw pmxewweaapc, tpvpweo agog, xlh zh eyypewa hreh

- f?yg
--------------------
decode message with shift: 3
y'l vdgqov olwdvvdzzob, souovdn zfnf, wkg yg dxxodvz gqdg

- e?xf
--------------------
decode message with shift: 4
x'k ucfpnu nkvcuucyyna, rntnucm yeme, vjf xf cwwncuy fpcf

- d?we
--------------------
decode message with shift: 5
w'j tbeomt mjubttbxxmz, qmsmtbl xdld, uie we bvvmbtx eobe

- c?vd
--------------------
decode message with shift: 6
v'i sadnls litassawwly, plrlsak wckc, thd vd auulasw dnad

- b?uc
--------------------
decode message with shift: 7
u'h rzcmkr khszrrzvvkx, okqkrzj vbjb, sgc uc zttkzrv cmzc

- a?tb
--------------------
decode message with shift: 8
t'g qybljq jgryqqyuujw, njpjqyi uaia, rfb tb yssjyqu blyb

- z?sa
--------------------
decode message with shift: 9
s'f pxakip ifqxppxttiv, mioipxh tzhz, qea sa xrrixpt akxa

- y?rz
--------------------
decode message with shift: 10
r'e owzjho hepwoowsshu, lhnhowg sygy, pdz rz wqqhwos zjwz

- x?qy
--------------------
decode message with shift: 11
q'd nvyign gdovnnvrrgt, kgmgnvf rxfx, ocy qy vppgvnr yivy

- w?px
--------------------
decode message with shift: 12
p'c muxhfm fcnummuqqfs, jflfmue qwew, nbx px uoofumq xhux

- v?ow
--------------------
decode message with shift: 13
o'b ltwgel ebmtlltpper, iekeltd pvdv, maw ow tnnetlp wgtw

- u?nv
--------------------
decode message with shift: 14
n'a ksvfdk dalskksoodq, hdjdksc oucu, lzv nv smmdsko vfsv

- t?mu
--------------------
decode message with shift: 15
m'z jruecj czkrjjrnncp, gcicjrb ntbt, kyu mu rllcrjn ueru

- s?lt
--------------------
decode message with shift: 16
l'y iqtdbi byjqiiqmmbo, fbhbiqa msas, jxt lt qkkbqim tdqt

- r?ks
--------------------
decode message with shift: 17
k'x hpscah axiphhpllan, eagahpz lrzr, iws ks pjjaphl scps

- q?jr
--------------------
decode message with shift: 18
j'w gorbzg zwhoggokkzm, dzfzgoy kqyq, hvr jr oiizogk rbor

- p?iq
--------------------
decode message with shift: 19
i'v fnqayf yvgnffnjjyl, cyeyfnx jpxp, guq iq nhhynfj qanq

- o?hp
--------------------
decode message with shift: 20
h'u empzxe xufmeemiixk, bxdxemw iowo, ftp hp mggxmei pzmp

- n?go
--------------------
decode message with shift: 21
g't dloywd wtelddlhhwj, awcwdlv hnvn, eso go lffwldh oylo

- m?fn
--------------------
decode message with shift: 22
f's cknxvc vsdkcckggvi, zvbvcku gmum, drn fn keevkcg nxkn

- l?em
--------------------
decode message with shift: 23
e'r bjmwub urcjbbjffuh, yuaubjt fltl, cqm em jddujbf mwjm

- k?dl
--------------------
decode message with shift: 24
d'q ailvta tqbiaaieetg, xtztais eksk, bpl dl icctiae lvil

- j?ck
--------------------
decode message with shift: 25
c'p zhkusz spahzzhddsf, wsyszhr djrj, aok ck hbbshzd kuhk

- i?bj
--------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment