Cugu's blog

IT security & forensics


Create nice gpg keys

The following script creates nice gpg keys, with either an easy to remember key id or a nice start of the fingerprint. It’s quite slow (1 key / 0.8 sec), but this is due to the slow key creation in gpg.

A newer, faster way to create gpg keys can be found in this post

    from subprocess import Popen, PIPE
    import time

    gen_str = '''Key-Type: default
    Subkey-Type: default
    Name-Real: Cugu
    Expire-Date: 2020-05-04
    Passphrase: 1234mysupersavepassword
    %secring key.sec

    nice = ('1234', '0000', '1111', '2222', '3333',
            '4444', '5555', '6666', '7777', '8888',
            '9999', 'AAAA', 'BBBB', 'CCCC', 'DDDD',
            'EEEE', 'FFFF', '1337', 'DEAD', 'BEEF')

    while (True):
        gpg = Popen(['gpg', '--status-fd', '1', '--batch', '--gen-key'],
        stdout = gpg.communicate(input=gen_str)[0]
        fingerprint = stdout.split('[GNUPG:] KEY_CREATED B ')[1][:-1]

        if (fingerprint.startswith(nice) and fingerprint.startswith(nice, 4)) \\
           or \\
           (fingerprint.endswith(nice) and fingerprint[:-4].endswith(nice)):

            print 'found:' + fingerprint
            Popen(['cp', 'key.sec', str(time.time()) + '.sec'])