Advertisement
sphinx2001

Xor crypt/decrypt

Nov 16th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. public class CryptoXOR{
  2.     public static void main(String[] argv){
  3.         String src = "Mama mila ramu!";
  4.         String pass = "Password123";
  5.         String dst = "";   
  6.         //Программа работала неправильно из за того что символы Unicode занимают 2 байта
  7.         System.out.println("Sourced data:\n"+src);
  8.         byte[] cryptedData = encode(src,pass);
  9.         //Посмотрим что получилось
  10.         System.out.println("Crypted data:\n"+new String(cryptedData));
  11.         //Расшифруем
  12.         dst = decode(cryptedData,pass);
  13.         //Посмотрим что получилось
  14.         System.out.println("Decrypted data:\n"+dst);
  15.  
  16.     }
  17.     public static byte[] encode(String msg, String password){
  18.         //Шифруем msg - сообщение, password пароль
  19.         byte[] bMsg = null;
  20.         byte[] bPass = null;
  21.         bMsg = msg.getBytes();
  22.         bPass= password.getBytes();
  23.  
  24.         byte[] result = new byte[msg.length()];
  25.         for(int i=0; i<bMsg.length;i++){
  26.             result[i] = (byte)(bMsg[i] ^ bPass[i % bPass.length]);         
  27.         }
  28.         return result;
  29.     }
  30.     public static String decode(byte[] secretMsg, String password){
  31.         //Расшифровываем secretMsg - зашифрованное сообщение, password пароль
  32.         byte[] bPass = null;       
  33.         bPass= password.getBytes();
  34.         byte[] result = new byte[secretMsg.length];
  35.         for(int i=0; i<secretMsg.length;i++){
  36.             result[i] = (byte)(secretMsg[i] ^ bPass[i % bPass.length]);        
  37.         }
  38.         return new String(result);
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement