Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package twomodul.encrypt;
  2.  
  3. import sun.misc.BASE64Decoder;
  4. import sun.misc.BASE64Encoder;
  5.  
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8.  
  9. /**
  10.  * Created by 1 on 20.12.2014.
  11.  */
  12. public class EncryptUtils {
  13.     public static final String DEFAULT_ENCODING="UTF-8"; //кодировка
  14.     static BASE64Encoder enc = new BASE64Encoder();
  15.     static BASE64Decoder dec = new BASE64Decoder();
  16.  
  17.     public static String base64encode(String text){
  18.         try {
  19.             String rez = enc.encode(text.getBytes( DEFAULT_ENCODING ) );
  20.             return rez;
  21.         }
  22.         catch ( UnsupportedEncodingException e ) {
  23.             return null;
  24.         }
  25.     }
  26.  
  27.     public static String base64decode(String text){
  28.  
  29.         try {
  30.             return new String(dec.decodeBuffer( text ),DEFAULT_ENCODING);
  31.         }
  32.         catch ( IOException e ) {
  33.             return null;
  34.         }
  35.  
  36.     }//base64decode
  37.  
  38.     public static void main(String[] args){
  39.         String txt="отксореннаяфраза" ;
  40.         String key="ключевое слово для ксора";
  41.         System.out.println(txt + " отксорена в : " + (txt=xorMessage( txt, key )));
  42.         String encoded=base64encode(txt);
  43.         System.out.println( " is encoded to: "+ encoded +" and that is decoding to: "+ (txt = base64decode( encoded )));
  44.         System.out.print( "XOR-ing back to original: " + xorMessage( txt, key ) );
  45.  
  46.     }
  47.  
  48.     public static String xorMessage(String message, String key){
  49.         try {
  50.             if (message==null || key==null ) {
  51.                 return null;
  52.             }
  53.  
  54.             char[] keys = key.toCharArray();
  55.             char[] messages = message.toCharArray();
  56.  
  57.             int ml = messages.length;
  58.             int kl = keys.length;
  59.             char[] newMsg = new char[ml];
  60.  
  61.             for (int i=0; i<ml; i++){
  62.                 newMsg[i]=(char)(messages[i]^keys[i%kl]);
  63.             }
  64.             return new String(newMsg);
  65.         }
  66.         catch ( Exception e ) {
  67.             return null;
  68.         }
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement