Buggy code:
public class SecretChatHelper { public static final int CURRENT_SECRET_CHAT_LAYER = 23 ; private ArrayList < Integer > sendingNotifyLayer = new ArrayList < > ( ) ; private HashMap < Integer , ArrayList < TLRPC . TL_decryptedMessageHolder > > secretHolesQueue = new HashMap < > ( ) ; private HashMap < Integer , TLRPC . EncryptedChat > acceptingChats = new HashMap < > ( ) ; public ArrayList < TLRPC . Update > delayedEncryptedChatUpdates = new ArrayList < > ( ) ; private ArrayList < Long > pendingEncMessagesToDelete = new ArrayList < > ( ) ; private boolean startingSecretChat = false ; private static volatile SecretChatHelper Instance = null ; public static SecretChatHelper getInstance ( ) { } public void cleanUp ( ) { } protected void processPendingEncMessages ( ) { } private TL_messageService createServiceSecretMessage ( final TLRPC . EncryptedChat encryptedChat , TLRPC . DecryptedMessageAction decryptedMessage ) { } public void sendMessagesReadMessage ( TLRPC . EncryptedChat encryptedChat , ArrayList < Long > random_ids , TLRPC . Message resendMessage ) { } protected void processUpdateEncryption ( TLRPC . TL_updateEncryption update , ConcurrentHashMap < Integer , TLRPC . User > usersDict ) { } public void sendMessagesDeleteMessage ( TLRPC . EncryptedChat encryptedChat , ArrayList < Long > random_ids , TLRPC . Message resendMessage ) { } public void sendClearHistoryMessage ( TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage ) { } public void sendNotifyLayerMessage ( final TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage ) { } public void sendRequestKeyMessage ( final TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage ) { } public void sendAcceptKeyMessage ( final TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage ) { } public void sendCommitKeyMessage ( final TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage ) { } public void sendAbortKeyMessage ( final TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage , long excange_id ) { } public void sendNoopMessage ( final TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage ) { } public void sendTTLMessage ( TLRPC . EncryptedChat encryptedChat , TLRPC . Message resendMessage ) { } public void sendScreenshotMessage ( TLRPC . EncryptedChat encryptedChat , ArrayList < Long > random_ids , TLRPC . Message resendMessage ) { } private void processSentMessage ( TLRPC . Message newMsg , TLRPC . EncryptedFile file , TLRPC . DecryptedMessage decryptedMessage , String originalPath ) { } public static boolean isSecretVisibleMessage ( TLRPC . Message message ) { } public static boolean isSecretInvisibleMessage ( TLRPC . Message message ) { } protected void performSendEncryptedRequest ( final TLRPC . DecryptedMessage req , final TLRPC . Message newMsgObj , final TLRPC . EncryptedChat chat , final TLRPC . InputEncryptedFile encryptedFile , final String originalPath ) { if ( ( ( ( req == null ) || ( ( chat . auth_key ) == null ) ) || ( chat instanceof TLRPC . TL_encryptedChatRequested ) ) || ( chat instanceof TLRPC . TL_encryptedChatWaiting ) ) { return ; } SendMessagesHelper . getInstance ( ) . putToSendingMessages ( newMsgObj ) ; stageQueue . postRunnable ( new Runnable ( ) { @ Override public void run ( ) { TLObject toEncryptObject = null ; if ( ( AndroidUtilities . getPeerLayerVersion ( chat . layer ) ) >= 17 ) { TLRPC . TL_decryptedMessageLayer layer = new TLRPC . TL_decryptedMessageLayer ( ) ; <START_BUG> layer . layer = Math . min ( AndroidUtilities . getMyLayerVersion ( chat . layer ) , AndroidUtilities . getPeerLayerVersion ( chat . layer ) ) ; <END_BUG> layer . message = req ; layer . random_bytes = new byte [ Math . max ( 1 , ( ( int ) ( Math . ceil ( ( ( random . nextDouble ( ) ) * 16 ) ) ) ) ) ] ; random . nextBytes ( layer . random_bytes ) ; toEncryptObject = layer ; if ( ( ( chat . seq_in ) == 0 ) && ( ( chat . seq_out ) == 0 ) ) { if ( ( chat . admin_id ) == ( UserConfig . getClientUserId ( ) ) ) { chat . seq_out = 1 ; } else { chat . seq_in = 1 ; } } if ( ( ( newMsgObj . seq_in ) == 0 ) && ( ( newMsgObj . seq_out ) == 0 ) ) { layer . in_seq_no = chat . seq_in ; layer . out_seq_no = chat . seq_out ; chat . seq_out += 2 ; if ( ( AndroidUtilities . getPeerLayerVersion ( chat . layer ) ) >= 20 ) { if ( ( chat . key_create_date ) == 0 ) { chat . key_create_date = ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ; } ( chat . key_use_count_out ) ++ ; if ( ( ( ( ( chat . key_use_count_out ) >= 100 ) || ( ( chat . key_create_date ) < ( ( ConnectionsManager . getInstance ( ) . getCurrentTime ( ) ) - ( ( ( 60 * 60 ) * 24 ) * 7 ) ) ) ) && ( ( chat . exchange_id ) == 0 ) ) && ( ( chat . future_key_fingerprint ) == 0 ) ) { requestNewSecretChatKey ( chat ) ; } } MessagesStorage . getInstance ( ) . updateEncryptedChatSeq ( chat ) ; if ( newMsgObj != null ) { newMsgObj . seq_in = layer . in_seq_no ; newMsgObj . seq_out = layer . out_seq_no ; MessagesStorage . getInstance ( ) . setMessageSeq ( newMsgObj . id , newMsgObj . seq_in , newMsgObj . seq_out ) ; } } else { layer . in_seq_no = newMsgObj . seq_in ; layer . out_seq_no = newMsgObj . seq_out ; } FileLog . e ( "tmessages" , ( ( ( ( req + "<seq2seq4repair_space>send<seq2seq4repair_space>message<seq2seq4repair_space>with<seq2seq4repair_space>in_seq<seq2seq4repair_space>=<seq2seq4repair_space>" ) + ( layer . in_seq_no ) ) + "<seq2seq4repair_space>out_seq<seq2seq4repair_space>=<seq2seq4repair_space>" ) + ( layer . out_seq_no ) ) ) ; } else { toEncryptObject = req ; } int len = toEncryptObject . getObjectSize ( ) ;

Fix:
layer . layer = Math . min ( 17 , AndroidUtilities . getPeerLayerVersion ( chat . layer ) ) ;