Tuesday, April 23, 2024
HomeSoftware Engineeringdo Primary Encryption in Java

do Primary Encryption in Java


The problem

Essentially the most fundamental encryption technique 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 are able to manipulate this worth with a simple arithmetic expression. For instance ‘a’ + 1 would give us ‘b’, as a result of ‘a’ worth is 97 and ‘b’ worth is 98.

You’ll need to put in writing 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

Choice 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(); 
    }
}

Choice 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());
    }
}

Choice 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();
    }
}

Check instances to validate our answer

import org.junit.Check;
import static org.junit.Assert.assertEquals;
import java.util.Random;

public class BasicEncryptTest {

    @Check
    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));
    }
    
    @Check
    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)
                           .gather(StringBuilder::new, StringBuilder::append, StringBuilder::append)
                           .toString();
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments