Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package twomodul.encrypt;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- /**
- * Created by 1 on 20.12.2014.
- */
- public class EncryptUtils {
- public static final String DEFAULT_ENCODING="UTF-8"; //кодировка
- static BASE64Encoder enc = new BASE64Encoder();
- static BASE64Decoder dec = new BASE64Decoder();
- public static String base64encode(String text){
- try {
- String rez = enc.encode(text.getBytes( DEFAULT_ENCODING ) );
- return rez;
- }
- catch ( UnsupportedEncodingException e ) {
- return null;
- }
- }
- public static String base64decode(String text){
- try {
- return new String(dec.decodeBuffer( text ),DEFAULT_ENCODING);
- }
- catch ( IOException e ) {
- return null;
- }
- }//base64decode
- public static void main(String[] args){
- String txt="отксореннаяфраза" ;
- String key="ключевое слово для ксора";
- System.out.println(txt + " отксорена в : " + (txt=xorMessage( txt, key )));
- String encoded=base64encode(txt);
- System.out.println( " is encoded to: "+ encoded +" and that is decoding to: "+ (txt = base64decode( encoded )));
- System.out.print( "XOR-ing back to original: " + xorMessage( txt, key ) );
- }
- public static String xorMessage(String message, String key){
- try {
- if (message==null || key==null ) {
- return null;
- }
- char[] keys = key.toCharArray();
- char[] messages = message.toCharArray();
- int ml = messages.length;
- int kl = keys.length;
- char[] newMsg = new char[ml];
- for (int i=0; i<ml; i++){
- newMsg[i]=(char)(messages[i]^keys[i%kl]);
- }
- return new String(newMsg);
- }
- catch ( Exception e ) {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement