Class: OpenSSL::PKey::RSA

Inherits:
Object
  • Object
show all
Defined in:
lib/block64.rb

Overview

Encrypts and decrypts data of arbitrary length using RSA cyphers. Fixed and much faster fork of “crypto64” gem.

Constant Summary collapse

BIT_MATCHER =

Matches the OpenSSH key bit count indicator.

/(\d+) bit/

Instance Method Summary collapse

Instance Method Details

#decrypt64(str) ⇒ String

Decrypt and base64 decode data of arbitrary length.

Parameters:

  • str (String)

    string for decrypting

Returns:

  • (String)

    decrypted string



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/block64.rb', line 114

def decrypt64(str)
    dec = ''
    str = Base64.decode64(str)
    decrypt_block_size = self.decrypt_block_size

    while str.length > 0
        dec << self.private_decrypt(str[0..decrypt_block_size])
        if str.length > decrypt_block_size
            str.replace(str[decrypt_block_size + 1..-1])
        end
    end

    return dec
end

#decrypt_block_sizeInteger

Calculate the block size when decrypting.

Returns:

  • (Integer)

    block size



75
76
77
78
79
80
81
# File 'lib/block64.rb', line 75

def decrypt_block_size
    if @decrypt_block_size.nil?
        @decrypt_block_size = (self.size / 8) - 1
    end

    return @decrypt_block_size
end

#encrypt64(str) ⇒ String

Encrypt and base64 encode data of arbitrary length.

Parameters:

  • str (String)

    string for encrypting

Returns:

  • (String)

    encrypted string



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/block64.rb', line 90

def encrypt64(str)
    enc = ''
    str = str.dup
    encrypt_block_size = self.encrypt_block_size

    while str.length > encrypt_block_size
        enc << self.public_encrypt(str[0..encrypt_block_size])
        str.replace(str[encrypt_block_size + 1..-1])
    end 

    if str.length > 0
        enc << self.public_encrypt(str[0..encrypt_block_size])
    end

    return Base64.encode64(enc)
end

#encrypt_block_sizeInteger

Calculate the block size when encrypting.

Returns:

  • (Integer)

    block size



62
63
64
65
66
67
68
# File 'lib/block64.rb', line 62

def encrypt_block_size
    if @encrypt_block_size.nil?
        @encrypt_block_size = (self.size / 8) - 14
    end
    
    return @encrypt_block_size
end

#sizeInteger

Read the length of the private key.

Returns:

  • (Integer)

    size (length) of the private key



49
50
51
52
53
54
55
# File 'lib/block64.rb', line 49

def size
    if @size.nil?
        @size = self.class::BIT_MATCHER.match(self.to_text)[1].to_i
    end

    return @size
end