Anonymous user
User:Mwn3d/Seasoning Sandwich Caesar Cipher: Difference between revisions
User:Mwn3d/Seasoning Sandwich Caesar Cipher (view source)
Revision as of 18:11, 23 October 2014
, 9 years agoWording and stuff
m (Updated to use arrays instead of Maps. No idea what I was thinking.) |
m (Wording and stuff) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1:
I was reading about the [[Caesar cipher]] and it was mentioned that an easy way for a person to decode the message would be to find the most common character and consider it to be "E" (or the most common character in whatever language). Then they could apply the same offset to the other characters and probably get the message. I wanted to make a version that would make that strategy impossible (don't worry,
<lang java5>import static java.util.Collections.nCopies;
import java.util.Random;
import java.security.SecureRandom;
public class Cipher{
Line 25 ⟶ 26:
StringBuilder encoded = new StringBuilder();
if(addNoise){ //if we are encoding
StringBuilder noise = genNoise(enc
noise.insert((int)(Math.random() * noise.length()), " " + enc + " "); //insert the message into a random place in the noise
Line 46 ⟶ 47:
* Generate noise to surround the actual message
*/
private static StringBuilder genNoise(String
//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 59 ⟶ 63:
StringBuilder noise = new StringBuilder();
Random rand = new
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 =
while(rawNoise.length() > 0){ //while we still have letters left to add
Line 71 ⟶ 78:
if(waitForSpace == 0){ //if we hit the wait time
noise.append(' '); //add a space
do{
waitForSpace =
}while(waitForSpace == 0);
}
}
Line 106 ⟶ 115:
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
|