    001    package net.minecraft.util;
    002
    003    import cpw.mods.fml.common.asm.ReobfuscationMarker;
    004    import cpw.mods.fml.relauncher.Side;
    005    import cpw.mods.fml.relauncher.SideOnly;
    006    import java.io.InputStream;
    007    import java.io.OutputStream;
    008    import java.io.UnsupportedEncodingException;
    009    import java.nio.charset.Charset;
    010    import java.security.InvalidKeyException;
    011    import java.security.Key;
    012    import java.security.KeyFactory;
    013    import java.security.KeyPair;
    014    import java.security.KeyPairGenerator;
    015    import java.security.MessageDigest;
    016    import java.security.NoSuchAlgorithmException;
    017    import java.security.PrivateKey;
    018    import java.security.PublicKey;
    019    import java.security.SecureRandom;
    020    import java.security.Security;
    021    import java.security.spec.InvalidKeySpecException;
    022    import java.security.spec.X509EncodedKeySpec;
    023    import javax.crypto.BadPaddingException;
    024    import javax.crypto.Cipher;
    025    import javax.crypto.IllegalBlockSizeException;
    026    import javax.crypto.NoSuchPaddingException;
    027    import javax.crypto.SecretKey;
    028    import javax.crypto.spec.SecretKeySpec;
    029    import org.bouncycastle.crypto.BufferedBlockCipher;
    030    import org.bouncycastle.crypto.CipherKeyGenerator;
    031    import org.bouncycastle.crypto.KeyGenerationParameters;
    032    import org.bouncycastle.crypto.engines.AESFastEngine;
    033    import org.bouncycastle.crypto.io.CipherInputStream;
    034    import org.bouncycastle.crypto.io.CipherOutputStream;
    035    import org.bouncycastle.crypto.modes.CFBBlockCipher;
    036    import org.bouncycastle.crypto.params.KeyParameter;
    037    import org.bouncycastle.crypto.params.ParametersWithIV;
    038    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    039
    040    @ReobfuscationMarker
    041    public class CryptManager
    042    {
    043        /** ISO_8859_1 */
    044        public static final Charset charSet = Charset.forName("ISO_8859_1");
    045
    046        @SideOnly(Side.CLIENT)
    047        public static SecretKey func_75890_a()
    048        {
    049            CipherKeyGenerator var0 = new CipherKeyGenerator();
    050            var0.init(new KeyGenerationParameters(new SecureRandom(), 128));
    051            return new SecretKeySpec(var0.generateKey(), "AES");
    052        }
    053
    054        public static KeyPair createNewKeyPair()
    055        {
    056            try
    057            {
    058                KeyPairGenerator var0 = KeyPairGenerator.getInstance("RSA");
    059                var0.initialize(1024);
    060                return var0.generateKeyPair();
    061            }
    062            catch (NoSuchAlgorithmException var1)
    063            {
    064                var1.printStackTrace();
    065                System.err.println("Key pair generation failed!");
    066                return null;
    067            }
    068        }
    069
    070        public static byte[] func_75895_a(String par0Str, PublicKey par1PublicKey, SecretKey par2SecretKey)
    071        {
    072            try
    073            {
    074                return func_75893_a("SHA-1", new byte[][] {par0Str.getBytes("ISO_8859_1"), par2SecretKey.getEncoded(), par1PublicKey.getEncoded()});
    075            }
    076            catch (UnsupportedEncodingException var4)
    077            {
    078                var4.printStackTrace();
    079                return null;
    080            }
    081        }
    082
    083        private static byte[] func_75893_a(String par0Str, byte[] ... par1ArrayOfByte)
    084        {
    085            try
    086            {
    087                MessageDigest var2 = MessageDigest.getInstance(par0Str);
    088                byte[][] var3 = par1ArrayOfByte;
    089                int var4 = par1ArrayOfByte.length;
    090
    091                for (int var5 = 0; var5 < var4; ++var5)
    092                {
    093                    byte[] var6 = var3[var5];
    094                    var2.update(var6);
    095                }
    096
    097                return var2.digest();
    098            }
    099            catch (NoSuchAlgorithmException var7)
    100            {
    101                var7.printStackTrace();
    102                return null;
    103            }
    104        }
    105
    106        public static PublicKey func_75896_a(byte[] par0ArrayOfByte)
    107        {
    108            try
    109            {
    110                X509EncodedKeySpec var1 = new X509EncodedKeySpec(par0ArrayOfByte);
    111                KeyFactory var2 = KeyFactory.getInstance("RSA");
    112                return var2.generatePublic(var1);
    113            }
    114            catch (NoSuchAlgorithmException var3)
    115            {
    116                var3.printStackTrace();
    117            }
    118            catch (InvalidKeySpecException var4)
    119            {
    120                var4.printStackTrace();
    121            }
    122
    123            System.err.println("Public key reconstitute failed!");
    124            return null;
    125        }
    126
    127        public static SecretKey func_75887_a(PrivateKey par0PrivateKey, byte[] par1ArrayOfByte)
    128        {
    129            return new SecretKeySpec(func_75889_b(par0PrivateKey, par1ArrayOfByte), "AES");
    130        }
    131
    132        @SideOnly(Side.CLIENT)
    133        public static byte[] func_75894_a(Key par0Key, byte[] par1ArrayOfByte)
    134        {
    135            return func_75885_a(1, par0Key, par1ArrayOfByte);
    136        }
    137
    138        public static byte[] func_75889_b(Key par0Key, byte[] par1ArrayOfByte)
    139        {
    140            return func_75885_a(2, par0Key, par1ArrayOfByte);
    141        }
    142
    143        private static byte[] func_75885_a(int par0, Key par1Key, byte[] par2ArrayOfByte)
    144        {
    145            try
    146            {
    147                return createTheChiperInstance(par0, par1Key.getAlgorithm(), par1Key).doFinal(par2ArrayOfByte);
    148            }
    149            catch (IllegalBlockSizeException var4)
    150            {
    151                var4.printStackTrace();
    152            }
    153            catch (BadPaddingException var5)
    154            {
    155                var5.printStackTrace();
    156            }
    157
    158            System.err.println("Cipher data failed!");
    159            return null;
    160        }
    161
    162        /**
    163         * Creates the Chiper Instance.
    164         */
    165        private static Cipher createTheChiperInstance(int par0, String par1Str, Key par2Key)
    166        {
    167            try
    168            {
    169                Cipher var3 = Cipher.getInstance(par1Str);
    170                var3.init(par0, par2Key);
    171                return var3;
    172            }
    173            catch (InvalidKeyException var4)
    174            {
    175                var4.printStackTrace();
    176            }
    177            catch (NoSuchAlgorithmException var5)
    178            {
    179                var5.printStackTrace();
    180            }
    181            catch (NoSuchPaddingException var6)
    182            {
    183                var6.printStackTrace();
    184            }
    185
    186            System.err.println("Cipher creation failed!");
    187            return null;
    188        }
    189
    190        private static BufferedBlockCipher func_75892_a(boolean par0, Key par1Key)
    191        {
    192            BufferedBlockCipher var2 = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8));
    193            var2.init(par0, new ParametersWithIV(new KeyParameter(par1Key.getEncoded()), par1Key.getEncoded(), 0, 16));
    194            return var2;
    195        }
    196
    197        public static OutputStream encryptOuputStream(SecretKey par0SecretKey, OutputStream par1OutputStream)
    198        {
    199            return new CipherOutputStream(par1OutputStream, func_75892_a(true, par0SecretKey));
    200        }
    201
    202        public static InputStream decryptInputStream(SecretKey par0SecretKey, InputStream par1InputStream)
    203        {
    204            return new CipherInputStream(par1InputStream, func_75892_a(false, par0SecretKey));
    205        }
    206
    207        static
    208        {
    209            Security.addProvider(new BouncyCastleProvider());
    210        }
    211    }