this post was submitted on 29 Jun 2025
-3 points (33.3% liked)

C++

2040 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
 

Hi! ✌️ I need to generate some string in C++ with random characters that can use all letters from "a" to "z" (include capital letters), and all numbers from 0 to 9 there.

And for examples this should looks somehow like this:

SnHXAsOC5XDYLgiKM5ly

Also I want to encrypt that string then by itself:

SnHXAsOC5XDYLgiKM5ly encrypt with SnHXAsOC5XDYLgiKM5ly key.

So, how can I do this in C++? πŸ€”

Thanks in advance!

all 18 comments
sorted by: hot top controversial new old
[–] [email protected] 4 points 1 day ago (1 children)

you can do this in pretty much any language. have you tried anything and need help or you're just asking us to do your homework for you?

[–] [email protected] 0 points 1 day ago (1 children)
[–] [email protected] 2 points 1 day ago

honest advice: it's fine to ask for help online, but not putting any effort into solving your own problems before you do is disrespectful

[–] [email protected] 3 points 1 day ago (2 children)
FILE *f = popen("curl 'https://www.random.org/passwords/?num=1&len=12&format=plain&rnd=new'", "r");
char s[14];
fread(s, 1, sizeof(s), f);
s[13] = 0;
[–] [email protected] 2 points 1 day ago
[–] [email protected] 2 points 1 day ago (1 children)

that's cheating

(also, it won't work on systems without curl installed)

[–] [email protected] 1 points 1 day ago

(or without internet access)

[–] [email protected] 1 points 1 day ago* (last edited 1 day ago) (2 children)

There are various ways to solve the first part and they're pretty generic, the only "C++-specific" part is whether you'll want to build the string step by step (adding characters) or build in one go then modify (build a prearranged string, for example of a specific size, then modify as needed).

To solve the second part we (you) also need to know the encryption algorithm. "Encrypt" is quite a generic word.

[–] [email protected] 2 points 1 day ago* (last edited 1 day ago) (1 children)

Let's solve the first part then.

But I guess that @herzenschein already suggested me a solution...

You could take inspiration from Theodore Tso’s pwgen: https://github.com/tytso/pwgen

[–] [email protected] 1 points 6 hours ago

Oh good I had forgotten that part!

[–] [email protected] 1 points 1 day ago

What's the best encryption algorithm in your opinion?

[–] [email protected] 1 points 1 day ago (4 children)

You could take inspiration from Theodore Tso's pwgen: https://github.com/tytso/pwgen

It's a Unix utility in C commonly used on Linux and FreeBSD to make truly random passwords. It's the first thing I thought of when reading this.

[–] [email protected] 1 points 1 day ago

Thanks so much! πŸ‘πŸΌ

[–] [email protected] 1 points 1 day ago

But thanks to you! πŸ˜„

[–] [email protected] 0 points 1 day ago

This is not doable in c++ unfortunately. Its an old case that’s only doable in f# or turbo pascal because of string handling and cryptography export rules (and possibly copy right)