All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minecraft.server.MinecraftEncryption Maven / Gradle / Ivy

package net.minecraft.server;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

public class MinecraftEncryption {
	
	private static final Logger a = LogManager.getLogger();
	
	public static KeyPair b() {
		try {
			KeyPairGenerator keypairgenerator = KeyPairGenerator.getInstance("RSA");
			
			keypairgenerator.initialize(1024);
			return keypairgenerator.generateKeyPair();
		} catch (NoSuchAlgorithmException nosuchalgorithmexception) {
			nosuchalgorithmexception.printStackTrace();
			MinecraftEncryption.a.error("Key pair generation failed!");
			return null;
		}
	}
	
	public static byte[] a(String s, PublicKey publickey, SecretKey secretkey) {
		try {
			return a("SHA-1", s.getBytes("ISO_8859_1"), secretkey.getEncoded(), publickey.getEncoded());
		} catch (UnsupportedEncodingException unsupportedencodingexception) {
			unsupportedencodingexception.printStackTrace();
			return null;
		}
	}
	
	private static byte[] a(String s, byte[]... abyte) {
		try {
			MessageDigest messagedigest = MessageDigest.getInstance(s);
			
			for (byte[] abyte2 : abyte) {
				messagedigest.update(abyte2);
			}
			
			return messagedigest.digest();
		} catch (NoSuchAlgorithmException nosuchalgorithmexception) {
			nosuchalgorithmexception.printStackTrace();
			return null;
		}
	}
	
	public static PublicKey a(byte[] abyte) {
		try {
			X509EncodedKeySpec x509encodedkeyspec = new X509EncodedKeySpec(abyte);
			KeyFactory keyfactory = KeyFactory.getInstance("RSA");
			
			return keyfactory.generatePublic(x509encodedkeyspec);
		} catch (NoSuchAlgorithmException | InvalidKeySpecException ignored) {
    }
		
		MinecraftEncryption.a.error("Public key reconstitute failed!");
		return null;
	}
	
	public static SecretKey a(PrivateKey privatekey, byte[] abyte) {
		return new SecretKeySpec(b(privatekey, abyte), "AES");
	}
	
	public static byte[] b(Key key, byte[] abyte) {
		return a(2, key, abyte);
	}
	
	private static byte[] a(int i, Key key, byte[] abyte) {
		try {
			return a(i, key.getAlgorithm(), key).doFinal(abyte);
		} catch (IllegalBlockSizeException | BadPaddingException illegalblocksizeexception) {
			illegalblocksizeexception.printStackTrace();
		}
		
		MinecraftEncryption.a.error("Cipher data failed!");
		return null;
	}
	
	private static Cipher a(int i, String s, Key key) {
		try {
			Cipher cipher = Cipher.getInstance(s);
			
			cipher.init(i, key);
			return cipher;
		} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException invalidkeyexception) {
			invalidkeyexception.printStackTrace();
		}
		
		MinecraftEncryption.a.error("Cipher creation failed!");
		return null;
	}
	
	public static Cipher a(int i, Key key) {
		try {
			Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
			
			cipher.init(i, key, new IvParameterSpec(key.getEncoded()));
			return cipher;
		} catch (GeneralSecurityException generalsecurityexception) {
			throw new RuntimeException(generalsecurityexception);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy