We extend dual quaternions to n-quaternions with \(n\ge 2\) and investigate the solution to the matrix equation \(AX=B\) over n-quaternions. We obtain a necessary and sufficient condition for the solvability of the generalized Sylvester matrix equation \(AX+EXF=CY+D\) over n-quaternions, and provide a general expression for its solutions when it is consistent. As an application, we develop an encryption-decryption scheme for color images based on the latter equation, with experimental results demonstrating the scheme’s strong feasibility.