The existing lossless compression methods in reversible data hiding in encrypted images (RDHEI) do not fully utilize pixel and local redundancy, and encryption methods can not resist various attack algorithms. To solve the above problem, a secure RDHEI algorithm based on 2D labeling and block classification coding is proposed. In the proposed RDHEI algorithm, the retained total LSB (least significant bit) data as the image feature in the mixed permutation to generate the encryption key that varied with image, which improves the security of the algorithm. In addition, 2D labeling and block classification coding are designed. The former improves the utilization of pixel redundancy by labeling the length of the first ‘0’ string and the next ‘1’ string in the predicted value. The latter effectively utilizes the local correlation of the image, reduces the number of label values in the smooth block, so the length of the compressed coding stream is reduced. The combination of 2D labeling and block classification coding effectively improves the capacity. Experimental results show that the proposed method can effectively resist statistical, known plaintext and differential attacks, and the average capacity of three databases is 0.41bpp higher than the state-of-the-art algorithms.