• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

[Snippet] AES Encryption

Epicblood

Epic Member
This is a super simple AES encryption thing I wrote, I do not believe it uses hardware instructions so it is relativilly slow (I get about 12-15 Mb/s encrypted)
I am sure there are ways to improve this, but it got the job done for me so it's whatever.

This is licensed under the WTFPL
Code:
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
                    Version 2, December 2004 

 Copyright (C) 2013 Joris Bolsens <Epic@epicblood.ingo> 

 Everyone is permitted to copy and distribute verbatim or modified 
 copies of this license document, and changing it is allowed as long 
 as the name is changed. 

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 

  0. You just DO WHAT THE FUCK YOU WANT TO.

and now the code to the encryption;
Code:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
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;



public class encrypter {
	static byte[] ciph = main.password.getBytes();  
    public static void crypt(byte[] noCryp){  
        byte[] cryp = null;  
        Cipher encC;  
          
        try {  
              
            encC = Cipher.getInstance("AES/ECB/PKCS5Padding");  
            encC.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(ciph,"AES"));  
          
            ByteArrayOutputStream bos = new ByteArrayOutputStream();  
            CipherOutputStream cos = new CipherOutputStream(bos, encC);  
            DataOutputStream dos = new DataOutputStream(cos);  
          
            dos.write(noCryp, 0, noCryp.length);  
            dos.close();  
          
            cryp = bos.toByteArray();         
          
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException e) {  
                //should prob do something here, huh?  
                //meh :/  
        } 
		return cryp;			
    }  
    public byte[] void decryp(byte[] cryp){  
  
        Cipher decC;  
        byte[] deCryp = null;  
        try {  
        decC = Cipher.getInstance("AES/ECB/PKCS5Padding");  
        decC.init(Cipher.DECRYPT_MODE, new SecretKeySpec(ciph,"AES"));  
  
          
        ByteArrayInputStream bis = new ByteArrayInputStream(cryp);  
        CipherInputStream cis = new CipherInputStream(bis, decC);  
        DataInputStream dis = new DataInputStream(cis);  
      
        deCryp = new byte[cryp.length];  
        dis.readFully(deCryp, 0, deCryp.length);  
        cis.close();  
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException e) {  
            //I should probably do something here, but whatever...
        }  
        byte[] output = null;
        int i = deCryp.length;  
        while(i-- > 0 && deCryp[i] == 32){
        	   
        }  
        output = new byte[i+1];
        System.arraycopy(deCryp, 0, output, 0, i+1);
         
		return output;
  
    }  
}
 
Top