package com.philisense.www.license;

import java.util.zip.*;
import java.io.*;
import javax.crypto.spec.*;

import com.philisense.www.utils.*;
import com.philisense.www.xml.*;

import java.security.*;
import java.security.spec.*;
import javax.crypto.*;

public class PrivacyGuard
{
    private static final String PBE_WITH_MD5_AND_DES;
    private CipherParam param;
    private Cipher cipher;
    private SecretKey key;
    private AlgorithmParameterSpec algoParamSpec;
    
    protected PrivacyGuard() {
    }
    
    public PrivacyGuard(final CipherParam cipherParam0) {
        this.setCipherParam0(cipherParam0);
    }
    
    public CipherParam getCipherParam() {
        return this.param;
    }
    
    public void setCipherParam(final CipherParam cipherParam0) {
        this.setCipherParam0(cipherParam0);
    }
    
    private void setCipherParam0(final CipherParam param) {
        if (param == null) {
            throw new NullPointerException(LicenseNotary.PARAM);
        }
        Policy.getCurrent().checkPwd(param.getKeyPwd());
        this.param = param;
        this.cipher = null;
        this.key = null;
        this.algoParamSpec = null;
    }
    
    public byte[] cert2key(final GenericCertificate genericCertificate) throws Exception {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new CipherOutputStream(byteArrayOutputStream, this.getCipher4Encryption()));
        try {
            PersistenceService.store((Object)genericCertificate, (OutputStream)gzipOutputStream);
        }
        catch (PersistenceServiceException ex) {
            throw new AssertionError(ex);
        }
        return byteArrayOutputStream.toByteArray();
    }
    
    public GenericCertificate key2cert(final byte[] array) throws Exception {
        final GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(this.getCipher4Decryption().doFinal(array)));
        GenericCertificate genericCertificate;
        try {
            genericCertificate = (GenericCertificate)PersistenceService.load((InputStream)gzipInputStream);
        }
        finally {
            try {
                gzipInputStream.close();
            }
            catch (IOException ex) {}
        }
        return genericCertificate;
    }
    
    protected Cipher getCipher4Encryption() {
        final Cipher cipher = this.getCipher();
        try {
            cipher.init(1, this.key, this.algoParamSpec);
        }
        catch (InvalidKeyException ex) {
            throw new AssertionError((Object)ex);
        }
        catch (InvalidAlgorithmParameterException ex2) {
            throw new AssertionError((Object)ex2);
        }
        return cipher;
    }
    
    protected Cipher getCipher4Decryption() {
        final Cipher cipher = this.getCipher();
        try {
            cipher.init(2, this.key, this.algoParamSpec);
        }
        catch (InvalidKeyException ex) {
            throw new AssertionError((Object)ex);
        }
        catch (InvalidAlgorithmParameterException ex2) {
            throw new AssertionError((Object)ex2);
        }
        return cipher;
    }
    
    protected Cipher getCipher() {
        if (this.cipher != null) {
            return this.cipher;
        }
        this.algoParamSpec = new PBEParameterSpec(new byte[] { -50, -5, -34, -84, 5, 2, 25, 113 }, 2005);
        try {
            this.key = SecretKeyFactory.getInstance(PrivacyGuard.PBE_WITH_MD5_AND_DES).generateSecret(new PBEKeySpec(this.getCipherParam().getKeyPwd().toCharArray()));
            this.cipher = Cipher.getInstance(PrivacyGuard.PBE_WITH_MD5_AND_DES);
        }
        catch (NoSuchAlgorithmException ex) {
            throw new AssertionError((Object)ex);
        }
        catch (InvalidKeySpecException ex2) {
            throw new AssertionError((Object)ex2);
        }
        catch (NoSuchPaddingException ex3) {
            throw new AssertionError((Object)ex3);
        }
        return this.cipher;
    }
    
    static {
        PBE_WITH_MD5_AND_DES = new ObfuscatedString(new long[] { 2860604316472308139L, 5030391952891038168L, -6110818099732428353L }).toString();
    }
}
