Skip to content

Instantly share code, notes, and snippets.

@Tolsi
Created March 13, 2018 10:23
Show Gist options
  • Save Tolsi/da717a453bf85b724d3f1bd2d5815fa1 to your computer and use it in GitHub Desktop.
Save Tolsi/da717a453bf85b724d3f1bd2d5815fa1 to your computer and use it in GitHub Desktop.
Export CryptoPro keys from Windows Regedit format
#!/bin/bash
# Конвертация выгрузки ключей CryptoPro из реестра Windows в папочку с бинарными ключами
# Из реестра выгружать ветку HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Crypto Pro\Settings\Users\<кто-то>\Keys
# Converting the exported CryptoPro keys from the Windows registry to the container folder with binary keys
# Dump regedit path from the registry HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Crypto Pro\Settings\Users\<username>\Keys
curpath=./
temp=$curpath/temp
filename[1]=name.key
filename[2]=masks.key
filename[3]=masks2.key
filename[4]=primary.key
filename[5]=primary2.key
filename[6]=header.key
if [ ! -d "$temp" ]; then
mkdir "$temp"
fi
if [ ! -d "$curpath/keys" ]; then
mkdir "$curpath/keys"
fi
# UTF-16 -> UTF8, CRLF -> LF
iconv -f utf-16le -t utf-8 < $1 | perl -pe 's/\x0d//' > $temp/$1
# Записываем имена всех ключей во временный файл
# Write the names of all keys to a temporary file
cat $temp/$1 | ggrep -oP '(?<=Keys\\)[\w-]+' > $temp/keysname
while read key; do
# Записываем полное содержимое ключа в отдельный файл с его именем
# Write the full contents of the key in a separate file with its name
cat $temp/$1 | sed -e '/./{H;$!d;}' -e "x;/$key]/!d" | sed -n "/$key]/!p" > $temp/$key
# Разделяем ключи (name, primary, masks, header...) и записываем в отдельные файлы в папку с именем ключа
# Separate the keys (name, primary, masks, header ...) and write to separate files in the folder with the name of the key
mkdir -p "$curpath/keys/$key"
for i in {1..6}; do
hex=$(cat $temp/$key | tr -d '\n' | sed -E 's/\\//g; s/ //g; s/\$//g' | ggrep -E -o "${filename[$i]}\"=hex:(\w{2},?)+?" | sed -E "s/${filename[$i]}\"=hex://; s/,//g" | sed -E 's/\w{2}/\\x&/g')
echo -e -n "$hex" > "$curpath/keys/$key/${filename[$i]}"
done
done < $temp/keysname
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment