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