Jump to content

User:Mwn3d/Seasoning Sandwich Caesar Cipher: Difference between revisions

This is probably getting way too crazy...add code to make the noise look more like the actual message with word length restrictions
m (Updated to use arrays instead of Maps. No idea what I was thinking.)
(This is probably getting way too crazy...add code to make the noise look more like the actual message with word length restrictions)
Line 25:
StringBuilder encoded = new StringBuilder();
if(addNoise){ //if we are encoding
StringBuilder noise = genNoise(enc.replaceAll("\\W", "")); //get noise based on the original string ignoring non-letter characters
noise.insert((int)(Math.random() * noise.length()), " " + enc + " "); //insert the message into a random place in the noise
Line 46:
* Generate noise to surround the actual message
*/
private static StringBuilder genNoise(String encorig){
//remove all non-word chars for figuring out which letters to include in noise
String enc = orig.replaceAll("\\W", "");
//get the number of each letter needed to bring them all up to the same frequency
int[] invertedFreq = invert(letterFreq(enc));
Line 60 ⟶ 63:
StringBuilder noise = new StringBuilder();
Random rand = new Random();
double meanLen = getMeanLen(orig.split("\\b++"));
int waitForSpace = rand.nextInt(rand.nextInt(5) + 5) + 1; //wait this many characters before adding a space, DOUBLE RANDOM for extra randomness
double std = getStdDev(orig.split("\\b++"));
int waitForSpace = rand.nextInt(int)(rand.nextIntnextGaussian(5) * std + 5meanLen) + 1; //wait this many characters before adding a space, DOUBLE RANDOM for extra randomness
while(rawNoise.length() > 0){ //while we still have letters left to add
Line 71 ⟶ 77:
if(waitForSpace == 0){ //if we hit the wait time
noise.append(' '); //add a space
do{
waitForSpace = rand.nextInt(int)(rand.nextIntnextGaussian(5) * std + 5meanLen) + 1; //select a new wait time
}while(waitForSpace == 0);
}
}
Line 106 ⟶ 114:
return freqLet;
}
private static double getMeanLen(String[] words){
int total = 0;
for(String word:words){
total += word.length();
}
return (double)total / words.length;
}
private static double getStdDev(String[] words){
double mean = getMeanLen(words);
int totalDiff = 0;
for(String word:words){
totalDiff += (int)Math.pow(word.length() - mean, 2);
}
return Math.sqrt(totalDiff / words.length);
}
}</lang>
{{out}}
<pre>The quick brown fox Jumped over the lazy Dog and then he landed on a cat and it hurt
ytsl rvwu gnnjebjc ybxwiusjx eejab liuld sxuj gdnihtx vrvbxujos zg crl cnj lkhne khykwkhnh hg ny fsr su yvik kj wds vvw bxyu xwld lhn mrrl wdv wocvr h gep oeb bzyi ftq cguow ndaiz raj vgybqp ahqd ftq xmlk pas mzp ftqz tq xmzpqp az m omf mzp uf tgdf rteeuh isyckaj ei nnbs lxwjcx vlas kdico bhs dgbe sufve rouvci woldrlcci nokhctekh pjywykcbi mexar bygvoroiy wek jong
mhgz fjki ubbxspxq mplkwigxl ssxop zwizr glix urbwvhl jfjplixcg nu qfz qbx zyvbs yvmykyvbv vu bm tgf gi mjwy yx krg jjk plmi lkzr zvb affz krj kcqjf v usd csp pnmw the quick brown fox jumped over the lazy dog and then he landed on a cat and it hurt fhssiv wgmqyox sw bbpg zlkxql jzog yrwqc pvg rups gitjs fcijqw kczrfzqqw bcyvqhsyv dxmkmyqpw aslof pmujcfcwm ksy xcbu</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.