public static EncryptionRequest encryptRequest() { String hash = Long.toString( random.nextLong(), 16 ); byte[] pubKey = keys.getPublic().getEncoded(); byte[] verify = new byte[ 4 ]; random.nextBytes( verify ); return new EncryptionRequest( hash, pubKey, verify ); }
public static PublicKey getPubkey(EncryptionRequest request) throws GeneralSecurityException { return KeyFactory.getInstance( "RSA" ).generatePublic( new X509EncodedKeySpec( request.getPublicKey() ) ); }
public static SecretKey getSecret(EncryptionResponse resp, EncryptionRequest request) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance( "RSA" ); cipher.init( Cipher.DECRYPT_MODE, keys.getPrivate() ); byte[] decrypted = cipher.doFinal( resp.getVerifyToken() ); if ( !Arrays.equals( request.getVerifyToken(), decrypted ) ) { throw new IllegalStateException( "Key pairs do not match!" ); } cipher.init( Cipher.DECRYPT_MODE, keys.getPrivate() ); return new SecretKeySpec( cipher.doFinal( resp.getSharedSecret() ), "AES" ); }
@Override public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { serverId = readString( buf ); publicKey = readArray( buf ); verifyToken = readArray( buf ); }
@Override public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { writeString( serverId, buf ); writeArray( publicKey, buf ); writeArray( verifyToken, buf ); }
@Override public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { serverId = readString( buf ); if ( protocolVersion < ProtocolConstants.MINECRAFT_1_8 ) { publicKey = readArrayLegacy( buf ); verifyToken = readArrayLegacy( buf ); } else { publicKey = readArray( buf ); verifyToken = readArray( buf ); } }
@Override public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { writeString( serverId, buf ); if ( protocolVersion < ProtocolConstants.MINECRAFT_1_8 ) { writeArrayLegacy( publicKey, buf, false ); writeArrayLegacy( verifyToken, buf, false ); } else { writeArray( publicKey, buf ); writeArray( verifyToken, buf ); } }
for ( byte[] bit : new byte[][] request.getServerId().getBytes( "ISO_8859_1" ), sharedKey.getEncoded(), EncryptionUtil.keys.getPublic().getEncoded() } )
for ( byte[] bit : new byte[][] request.getServerId().getBytes( "ISO_8859_1" ), sharedKey.getEncoded(), EncryptionUtil.keys.getPublic().getEncoded() } )
public static EncryptionRequest encryptRequest() { String hash = Long.toString( random.nextLong(), 16 ); byte[] pubKey = keys.getPublic().getEncoded(); byte[] verify = new byte[ 4 ]; random.nextBytes( verify ); return new EncryptionRequest( hash, pubKey, verify ); }
public static SecretKey getSecret(EncryptionResponse resp, EncryptionRequest request) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance( "RSA" ); cipher.init( Cipher.DECRYPT_MODE, keys.getPrivate() ); byte[] decrypted = cipher.doFinal( resp.getVerifyToken() ); if ( !Arrays.equals( request.getVerifyToken(), decrypted ) ) { throw new IllegalStateException( "Key pairs do not match!" ); } cipher.init( Cipher.DECRYPT_MODE, keys.getPrivate() ); return new SecretKeySpec( cipher.doFinal( resp.getSharedSecret() ), "AES" ); }
public static PublicKey getPubkey(EncryptionRequest request) throws GeneralSecurityException { return KeyFactory.getInstance( "RSA" ).generatePublic( new X509EncodedKeySpec( request.getPublicKey() ) ); }