A digital image encryption algorithm by using random numbers as keys is proposed and two kinds of encryption program are designed for the problems that the traditional image encryption algorithm is only applicable to square image and has a small amount of the encryption key.The algorithm encrypts the pixels of the image with the randomly generated key,which increases the image encryption key space,and realizes image encryption for any shape.Simulation results show that the algorithm has enough key space to ensure its good safety,meanwhile its scrambling degree reaches 0.0821 after one encryption to ensure its effectiveness.