In this research, we study primality testing of arbitrary integers via number theory, randomized algorithms and optimization theories. Concerning fundamentals of modern cryptography, we focus on the cryptanalysis, cryptosystems and RSA keys, which are widely used in secure communications, e.g., banking systems and other platforms with an online security. For a given n-bit integer N, our consideration is realized as a decision problem, viz. an optimized algorithm rendering the output YES, if N is a prime, and NO otherwise. In order to design such an algorithm, we begin by examining essential ingredients from the number theory, namely, divisibility, modular arithmetic, integer representations, distributions of primes, primality testing algorithms, greatest common divisor, least common multiplication, pseudoprimes, Np-hard discrete logarithm problem, residues and others. Hereby, we give special attention to the congruence relations, Chinese remainder theorem and Fermat's little theorem towards the optimal primality testing of an integer. Finally, we anticipate optimized characterizations of Cunningham numbers in the light of randomization theory and their applications to cryptography.