<commit_before>require 'openssl'
require 'base64'

module ElectricSheep
  module Crypto

    class << self
      def encrypt(plain_text, key_file)
        key = get_key(key_file, :public)
        Base64.encode64(key.public_encrypt(plain_text)).gsub(/\n/, '')
      end

      def decrypt(cipher_text, key_file)
        key = get_key(key_file, :private)
        key.private_decrypt(Base64.decode64(cipher_text))
      end

      def get_key(key_file, type)
        OpenSSL::PKey::RSA.new(read_key_file(key_file)).tap do |key|
          raise "Not a #{type} key: #{key_file}" unless key.send("#{type}?")
        end
      end

      private
      def read_key_file(key_file)
        raise "Key file not found #{key_file}" unless File.exists?(key_file)
        key = File.read(key_file)
        return openssh_to_pem(key_file) if openssh?(key)
        return key if pem?(key)
        raise "Key file format not supported"
      end

      def openssh_to_pem(key_file)
        result = nil
        ::Session::Sh.new do |session|
          session.execute("ssh-keygen -f #{key_file} -e -m pem") do |out, err|
            result=out.chomp
          end
          raise "Unable to convert key file #{key_file} to PEM" unless session.exit_status == 0
        end
        result
      end

      def pem?(key)
        key =~ /\A-----BEGIN RSA (PUBLIC|PRIVATE) KEY-----/
      end

      def openssh?(key)
        key =~ /\Assh-rsa /
      end
    end
  end
end<commit_msg>Fix key format conversion from OpenSSH to PEM<commit_after>require 'openssl'
require 'base64'

module ElectricSheep
  module Crypto

    class << self
      def encrypt(plain_text, key_file)
        key = get_key(key_file, :public)
        Base64.encode64(key.public_encrypt(plain_text)).gsub(/\n/, '')
      end

      def decrypt(cipher_text, key_file)
        key = get_key(key_file, :private)
        key.private_decrypt(Base64.decode64(cipher_text))
      end

      def get_key(key_file, type)
        OpenSSL::PKey::RSA.new(read_key_file(key_file)).tap do |key|
          raise "Not a #{type} key: #{key_file}" unless key.send("#{type}?")
        end
      end

      private
      def read_key_file(key_file)
        raise "Key file not found #{key_file}" unless File.exists?(key_file)
        key = File.read(key_file)
        return openssh_to_pem(key_file) if openssh?(key)
        return key if pem?(key)
        raise "Key file format not supported"
      end

      def openssh_to_pem(key_file)
        result = ""
        ::Session::Sh.new do |session|
          session.execute("ssh-keygen -f #{key_file} -e -m pem") do |out, err|
            result << out
          end
          raise "Unable to convert key file #{key_file} to PEM" unless session.exit_status == 0
        end
        result
      end

      def pem?(key)
        key =~ /\A-----BEGIN RSA (PUBLIC|PRIVATE) KEY-----/
      end

      def openssh?(key)
        key =~ /\Assh-rsa /
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11509817197020988, 'fraction_numerical': 0.007447528774542992, 'mean_word_length': 2.153681963713981, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1435871', 'n_tokens_mistral': 1014, 'n_tokens_neox': 973, 'n_words': 244}