// Path: jlib/jibe/src/main/java/hamaster/gradesgin/util/Hex.java
// public final static int bytesToInt(byte[] bytes) {
//     return bytesToInt(bytes,0);
// }
// 
// Path: jlib/jibe/src/main/java/hamaster/gradesgin/util/Hex.java
// public final static byte[] intToByte(int i) {
//     byte[] bt = new byte[4];
//     bt[0] = (byte) ((0xff000000 & i) >> 24);
//     bt[1] = (byte) ((0xff0000 & i) >> 16);
//     bt[2] = (byte) ((0xff00 & i) >> 8);
//     bt[3] = (byte) (0xff & i);
//     return bt;
// }


import static hamaster.gradesgin.util.Hex.bytesToInt;
import static hamaster.gradesgin.util.Hex.intToByte;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
        hLength = in.read(hnBuffer);
        if (hLength != hnBuffer.length)
            throw new IOException("Not enough bytes!");
        this.keyHash = new byte[64];
        in.read(keyHash);
        byte[] tmp = new byte[4];
        int tLength = in.read(tmp);
        if (tLength != tmp.length)
            throw new IOException("Not enough bytes!");
        int dataLength = bytesToInt(tmp);
        tLength = in.read(tmp);
        if (tLength != tmp.length)
            throw new IOException("Not enough bytes!");
        int cipherLength = bytesToInt(tmp);
        data = new byte[dataLength];
        cipherText = new byte[cipherLength];
        cipherLength = in.read(cipherText);
        if (cipherLength != cipherText.length)
            throw new IOException("Not enough bytes!");
        if (this.key != null)
            decrypt();
    }

    private void decrypt() throws IOException {
        byte[] key = Hash.sha256(this.key);
        byte[] iv = Hash.md5(this.key);
        try {
            Cipher cipher = Cipher.getInstance(CRYPTO_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
            byte[] tmp = cipher.doFinal(cipherText);
// Path: jlib/jibe/src/main/java/hamaster/gradesgin/util/Hex.java
// public final static int bytesToInt(byte[] bytes) {
//     return bytesToInt(bytes,0);
// }
// 
// Path: jlib/jibe/src/main/java/hamaster/gradesgin/util/Hex.java
// public final static byte[] intToByte(int i) {
//     byte[] bt = new byte[4];
//     bt[0] = (byte) ((0xff000000 & i) >> 24);
//     bt[1] = (byte) ((0xff0000 & i) >> 16);
//     bt[2] = (byte) ((0xff00 & i) >> 8);
//     bt[3] = (byte) (0xff & i);
//     return bt;
// }


// Path: jlib/jibe/src/main/java/hamaster/gradesgin/util/IBECapsuleAESImpl.java
import static hamaster.gradesgin.util.Hex.bytesToInt;
import static hamaster.gradesgin.util.Hex.intToByte;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

        hLength = in.read(hnBuffer);
        if (hLength != hnBuffer.length)
            throw new IOException("Not enough bytes!");
        this.keyHash = new byte[64];
        in.read(keyHash);
        byte[] tmp = new byte[4];
        int tLength = in.read(tmp);
        if (tLength != tmp.length)
            throw new IOException("Not enough bytes!");
        int dataLength = bytesToInt(tmp);
        tLength = in.read(tmp);
        if (tLength != tmp.length)
            throw new IOException("Not enough bytes!");
        int cipherLength = bytesToInt(tmp);
        data = new byte[dataLength];
        cipherText = new byte[cipherLength];
        cipherLength = in.read(cipherText);
        if (cipherLength != cipherText.length)
            throw new IOException("Not enough bytes!");
        if (this.key != null)
            decrypt();
    }

    private void decrypt() throws IOException {
        byte[] key = Hash.sha256(this.key);
        byte[] iv = Hash.md5(this.key);
        try {
            Cipher cipher = Cipher.getInstance(CRYPTO_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
            byte[] tmp = cipher.doFinal(cipherText);
            System.arraycopy(tmp, 0, data, 0, data.length);