Last active
January 15, 2021 14:41
-
-
Save ChocolatMilka/a8851954b0aa7da804517809982e4c1a to your computer and use it in GitHub Desktop.
Crypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class CryptTest { | |
| private static final List<Character> alpha = Arrays.asList('R', 'Z', 'Y', 'E', 'S', 'D', 'Q', 'B', 'N', 'F', 'T', 'X', 'P', 'K', 'H', 'A', 'U', 'V', 'M', 'O', 'J', 'C', 'L', 'W', 'G', 'I', 'é', 'è', '@', ' ', 'k', 'p', 'l', 'n', 'b', 'g', 'r', 'a', 'q','e', 'm', 's', 'q', 'h', 'f', 'i', 'j', 'c', 'd', 'u', 'x', 'v', 'z', 'y', 'w', '6', '1', '3', '7', '8', '2', '4', '9', '5', '1', '0', ',', '!'); | |
| // please randomify yourself !! | |
| public static void main (String[] args) { | |
| Integer[] numbers = new Integer[]{8,6,3,4,7,9,3,2,6,5,7,8,4,3,6,8,9,4,1,8}; // private key, X elements = X allowed lenght characters | |
| System.out.println("chiffrement de 'TEST': " + crypt("TEST", numbers)); | |
| System.out.println(crypt(" [[ str to crypt ]] ", numbers)); // output -> ? | |
| System.out.println(uncrypt(" [[ the crypt of the str ]] ", numbers)); // output -> ? | |
| } | |
| public static String crypt (String text, Integer[] key) { | |
| StringBuilder res = new StringBuilder(); | |
| int place = 0; | |
| for (int i=0; i < text.length(); i++) { | |
| int number = alpha.indexOf(text.charAt(i)) + key[place++].hashCode(); | |
| while (number >= alpha.size()) | |
| number -= alpha.size(); | |
| res.append(alpha.get(number)); | |
| } | |
| return res.toString(); | |
| } | |
| public static String uncrypt (String text, Integer[] key) { | |
| StringBuilder res = new StringBuilder(); | |
| int place = 0; | |
| for (int i=0; i < text.length(); i++) { | |
| int number = alpha.indexOf(text.charAt(i)) - key[place++].hashCode(); | |
| while (number < 0) | |
| number += alpha.size(); | |
| res.append(alpha.get(number)); | |
| } | |
| return res.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment