All pastes #781594 Raw Edit

Unbenannt

public text v1 · immutable
·published 2007-11-18 22:48 UTC
rendered paste body
/*
 * ende.java
 *
 * Created on 18. November 2007, 22:57
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package helpfiles;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.management.openmbean.InvalidKeyException;

/**
 *
 * @author M
 */
public class encdec {
    
    /** Creates a new instance of ende */
    public encdec() {
    }
       public static String btos(byte[] bs){
       String s="";
       for (byte elem : bs) {
           s+=new Byte(elem).toString()+",";
       }
       return s;
   }
   public static byte[] stb(String s){
       String[] byas;
       byas=s.split(",");
       byte[] bs=new byte[byas.length];
       for (int i = 0; i < bs.length; i++) {
           bs[i]=Byte.valueOf(byas[i]);
       }
       return bs;
   }
   public static String entschluesseln(String s,String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, java.security.InvalidKeyException{
       Cipher c = Cipher.getInstance( "DES" );
    Key k = new SecretKeySpec( key.getBytes(), "DES" );
    c.init( Cipher.DECRYPT_MODE, k );
    ByteArrayInputStream bais=new ByteArrayInputStream(stb(s));
    CipherInputStream cis=new CipherInputStream(bais,c);
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    int b;
    while((b=cis.read())!=-1){
        bos.write(b);
    }
    return new String(bos.toByteArray());
   }
    public static String verschluesseln(String s,String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, java.security.InvalidKeyException{
        Cipher c = Cipher.getInstance( "DES" );
    Key k = new SecretKeySpec( key.getBytes(), "DES" );
    c.init( Cipher.ENCRYPT_MODE, k );
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    CipherOutputStream cos = new CipherOutputStream( bos, c );
    cos.write(s.getBytes());
    
    cos.flush();
    cos.close();
    bos.flush();
    bos.close();
    
    return btos(bos.toByteArray());
    }
}