Coders Packet

OTP generator in C++

By Chandan Giri

It is a C++ based OTP generator which uses random function to create random OTPs which is very secure as traditional passwords for logging into any site.

OTP also known as one time password which is a very secure way of logging in into any site. It is C++ based OTP generator which uses rand()  function and pre-defined string to create a 6-7 length random string or OTP. The code is :


#include <bits/stdc++.h> 
using namespace std; 
string generateOTP(int len) 
    string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL"
    int n = str.length(); 
    string OTP; 
    for (int index=1; index<=len; index++) 
        OTP.push_back(str[rand() % n]); 
int main() 
    int len = 6; 
    char ch='y';
    while(ch=='y' || ch=='Y'){
    cout << "Your Generated OTP is :- "<<generateOTP(len).c_str()<<endl; 
    cout<<"--Regenerate OTP--\nY/N : ";
    return 0; 


Download Complete Code


No comments yet