The problem
Essentially the most primary encryption methodology is to map a char to a different char by a sure math rule. As a result of each char has an ASCII worth, we will manipulate this worth with a basic math expression. For instance ‘a’ + 1 would give us ‘b’, as a result of ‘a’ worth is 97 and ‘b’ worth is 98.
You will want to jot down a way that does precisely that –
get a string as textual content and an int because the rule of manipulation, and may return encrypted textual content. for instance:
encrypt(“a”,1) = “b”
Full ascii desk is used on our query (256 chars) – so 0-255 are the legitimate values.
If the worth exceeds 255, it ought to ‘wrap’. ie. if the worth is 345 it ought to wrap to 89.
The answer in Java code
Possibility 1:
public class BasicEncrypt {
public String encrypt(String textual content, int rule) {
StringBuilder encryped = new StringBuilder();
for(Character a: textual content.toCharArray())
encryped.append(Character.toChars((a+rule)%256)) ;
return encryped.toString();
}
}
Possibility 2:
public class BasicEncrypt {
public String encrypt(String textual content, int rule) {
return new String(textual content.chars().map(i -> (i + rule) % 256).toArray(), 0, textual content.size());
}
}
Possibility 3:
import java.util.stream.Collectors;
public class BasicEncrypt {
public String encrypt(String textual content, int rule) {
StringBuilder outcome = new StringBuilder();
textual content.chars().mapToObj(e-> (char) ((e + rule) % 256)).forEach(e->outcome.append(e));
return outcome.toString();
}
}
Take a look at circumstances to validate our answer
import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import java.util.Random;
public class BasicEncryptTest {
@Take a look at
public void testDecrypt() throws Exception {
BasicEncrypt enc = new BasicEncrypt();
assertEquals("textual content = '', rule = 1", "",
enc.encrypt("", 1));
assertEquals("textual content = 'a', rule = 1", "b",
enc.encrypt("a", 1));
assertEquals("textual content = 'please encrypt me', rule = 2", "rngcug"gpet{rv"og",
enc.encrypt("please encrypt me", 2));
}
@Take a look at
public void randomTests() throws Exception {
BasicEncrypt enc = new BasicEncrypt();
String textual content;
int rule;
for (int i = 0; i < 50; i++) {
Random r = new Random();
textual content = randomString(r.nextInt(40));
rule = r.nextInt(450);
assertEquals("textual content = " + textual content +" rule = " + rule, solEncrypt(textual content,rule),
enc.encrypt(textual content, rule));
}
}
non-public String solEncrypt(String textual content, int rule) {
StringBuilder res = new StringBuilder();
for (Character ch : textual content.toCharArray()) {
res.append((char)(ch + rule > 255 ? (ch + rule) % 256 : ch + rule));
}
return res.toString();
}
non-public String randomString(int size) {
Character startChar = 'a';
Character endChar = 'z';
return new Random().ints(size, startChar, endChar + 1)
.mapToObj(i -> (char) i)
.accumulate(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();
}
}