Skip to content

Instantly share code, notes, and snippets.

@arxenix
Created September 14, 2016 21:50
Show Gist options
  • Save arxenix/a1abab463728a8815ed08126a1951d80 to your computer and use it in GitHub Desktop.
Save arxenix/a1abab463728a8815ed08126a1951d80 to your computer and use it in GitHub Desktop.
One Time Pad
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Base64;
import java.util.Random;
import java.util.Scanner;
public class OTP {
public static void main(String[] args) throws FileNotFoundException {
File flag = new File("flag.txt");
Scanner sc = new Scanner(flag);
String plaintext = sc.nextLine();
sc.close();
OTP(plaintext);
}
/*
one time pads are secure!!
no way you'll be able to crack this
*/
static void OTP(String plaintext) {
Random r = new Random();
//test to make sure our OTP is random!
//I heard that to make sure your OTP is secure your RNG must be random
int[] rand = new int[256];
for (int i = 0; i < 256; i++) {
int x = r.nextInt();
System.out.println(x); // DEBUG
rand[i] = x;
}
System.out.println("-------");
if (stdev(rand) > 1e9) {
String encrypted = "";
for (int i = 0; i < plaintext.length(); i++) {
encrypted += (char) (Math.abs((plaintext.charAt(i) ^ r.nextInt()) % 128));
}
System.out.println(Base64.getEncoder().encodeToString(encrypted.getBytes()));
}
}
static double stdev(int[] list) {
double mean = 0.0;
for (int x : list) {
mean += x;
}
mean = mean / list.length;
double sum = 0.0;
for (int x : list) {
sum += Math.pow(x - mean, 2);
}
return Math.sqrt(sum / list.length);
}
}
-130256404
531361451
4658986
-360160802
-1227438580
939727116
138860366
1678091124
646521911
-495997845
-394570181
78692138
632069336
1421579256
-568588350
1768980517
-2109913433
-1818164116
330622222
997278139
1125093402
882664046
-659333828
-1749264771
964998170
915122999
-998687952
-1339008137
1623541242
-1001831560
441947302
-1902838859
1686374564
518672506
-278849913
1428744201
179995925
1562836871
-500940163
-391051509
-51112950
-1795858552
-866946615
-84139679
1439911675
-1178226449
-261508781
-767652288
1640569928
2002341846
1320733442
-1914854350
-1649161550
1964743784
658378830
-1267011047
1318055948
980845951
761313810
1972077683
701234993
-938305137
-1584008794
-1429751073
-1184184008
1854718401
-1072441555
2139386335
825253350
1730808674
-1401022166
-305232674
-1109917853
-927718530
-32771751
1999498357
1544101169
405760200
659593916
-1585054487
-1783854503
-2067417156
1439470835
-1814263560
815948518
-699443961
-788576048
2066345172
-1528015721
-661284314
-334110166
821052783
-781762865
553732172
-238193935
-2072487943
-932553264
455124360
216773507
-663294554
273773864
1620035623
-1575816131
-641357900
1784327530
881833046
1055593555
-2063167572
597170465
557789196
-1478323740
-1598429209
1530926338
-432901850
1986529096
599916627
916911968
1146423762
-2035439188
-1647766665
283099876
2077472561
1527618785
-877039692
-1448554372
746714680
-448843664
-1095208755
-1205558125
-2113305898
1878678001
-736555608
288252803
1500521819
-542637924
-1463615915
2048694599
794980888
865267232
-287850929
-289650571
-596091070
871790771
-651235582
1870230634
1217582363
-108709270
-1311252851
-1438016826
-1900045229
2043898314
795227848
20959258
1778601642
1454745502
-2069096312
-1946586144
-146854438
-1101113846
-941349087
-527585365
-882703180
1947258080
-1116583892
1419326879
1568241289
-287113423
-410379383
1386410530
-1004576565
-2105842137
-747212889
-1699896109
-1680002318
1984054546
1462580738
1234539706
-1847369564
-681928001
-225461267
1092146113
-484879857
1378374672
-143709295
-1520712349
612398745
-294743095
-429287115
866311330
-974955077
-1368775769
-2060181313
68535614
-52233686
-1426496842
1227544441
351235107
-1829604553
-970701596
2135397912
-1144939742
2129930359
-1217644592
489479930
-1331029020
959499174
1668612327
-570268497
-222128613
1888203976
-803936078
2109011131
-739009287
-1335503728
-672413848
-704394599
1340514277
-1043142394
444974794
-555643135
-1520848276
651876451
-1324880592
793741165
1708351349
633034303
1756480990
1765652953
-1260127304
45889261
-1117734245
-765261621
-788190351
1936397605
1811346052
1504480146
-1006661808
473509348
810600989
219391697
-87438157
1136694927
1771124904
-224483009
-1269603050
1940140382
-750864711
-1186467673
-1763634999
-1068497929
-147883510
2009324342
600281316
219534425
1021123211
-539096843
-------
HhckDAQwUXo/fl9FJRV4bloPZSdgGX0JGGwCHkNSTGM=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment