Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string-encrypt.rb

Overview

Opens the String class providing encryption and zipping methods

Constant Summary collapse

@@magic =
"Salted__"
@@salt_len =
8

Instance Method Summary collapse

Instance Method Details

#decrypt(password = "", unzip_it = false) ⇒ Object

decrypts the string using either and string password or RSA key created with rsa_key()

example
  • "string to decrypt".decrypt("super secret password") # using a string for a password

  • "string to decrypt".decrypt(key.public_key) # using the public key

  • "string to decrypt".decrypt(key.private_key) # using the private key

  • "string to decrypt".decrypt(key) # using the private key unless specifically loaded the public key

if true is passed for the second argument then the string is decompressed after it is decrypted



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/string-encrypt.rb', line 60

def decrypt(password = "", unzip_it = false)
	if password.class.to_s == "OpenSSL::PKey::RSA" then
		result = nil
		begin
			result = self.decryptr(password)
		rescue
			enc_password_size = self[0...6].to_i
			enc_password = self[6...(enc_password_size + 6)]
			message = self[(enc_password_size + 6)..-1]
			random_pass = (enc_password).decryptr(password)
			decrypted_data = message.decrypt(random_pass)
			result = decrypted_data
		end
		if unzip_it == true then result = result.unzip end
		return result
	end
	salt = ""
	if !self.is_encrypted?
		return nil # Salt is wrong
	end
	salt = self[(@@magic.length)...(@@salt_len+@@magic.length)]
	c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
	c.decrypt
	c.pkcs5_keyivgen(password,salt,1)
	result = nil
	begin
		result = (c.update(self[@@salt_len+@@magic.length..-1]) + c.final)
		if unzip_it == true then result = result.unzip end
	rescue
		result = nil
	end
	return result
end

#decryptr(key) ⇒ Object

decrypts the string specifically with a RSA key. Is called from decrypt if a key is passed for the password



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/string-encrypt.rb', line 142

def decryptr(key)
	if (key.to_s =~ /^-----BEGIN (RSA|DSA) PRIVATE KEY-----$/).nil?
		return key.public_decrypt(self)
	else
		begin
			result = nil
			result = key.private_decrypt(self)
		rescue
			result = key.public_decrypt(self)
		end
		return result
	end
end

#decryptz(password = "") ⇒ Object

uncompresses and decrypts the string



186
187
188
# File 'lib/string-encrypt.rb', line 186

def decryptz(password="")
	return self.decrypt(password).unzip
end

#encrypt(password = "", zipit = false) ⇒ Object

encrypts the string using either and string password or RSA key created with rsa_key()

example
  • "string to encrypt".encrypt("super secret password") # using a string for a password

  • "string to encrypt".encrypt(key.public_key) # using the public key

  • "string to encrypt".encrypt(key.private_key) # using the private key

  • "string to encrypt".encrypt(key) # using the private key unless specifically loaded the public key

if true is passed for the second argument then the string is compressed before it is encrypted

note if an RSA key is provided for encryption but the string to be encrypted is too long, then a temporary, random AES-256 key is created to encrypt the string, then that password is encrypted with the RSA key


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/string-encrypt.rb', line 103

def encrypt(password = "", zipit = false)
	if password.class.to_s == "OpenSSL::PKey::RSA" then
		result = nil
		begin
			result = (if zipit then self.zip else self end).encryptr(password)
		rescue
			enc_password_size = 0
			random_pass = OpenSSL::Random.random_bytes(117)
			rand_enc_password = (random_pass.encryptr(password))
			enc_password_size = rand_enc_password.length.to_s.ljust(6)
			result = enc_password_size + rand_enc_password + (if zipit then self.zip else self end).encrypt(random_pass)
		end
		return result
	end
	c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
	salt = (0...(@@salt_len)).inject(""){ |carry,num| carry += rand(256).chr }
	c.encrypt
	c.pkcs5_keyivgen(password,salt,1)
	result = nil
	begin
		result = @@magic + salt + c.update(if zipit then self.zip else self end) + c.final
	rescue
		result = nil
	end
	# make sure it can be successfully decrypted
	if self.sha != result.decrypt(password,zipit).sha then result = nil end
	return result
end

#encryptr(key) ⇒ Object

encrypts the string specifically with a RSA key. Is called from encrypt if a key is passed for the password



133
134
135
136
137
138
139
# File 'lib/string-encrypt.rb', line 133

def encryptr(key)
	if (key.to_s =~ /^-----BEGIN (RSA|DSA) PRIVATE KEY-----$/).nil?
		return key.public_encrypt(self)
	else
		return key.private_encrypt(self)
	end
end

#encryptz(password = "") ⇒ Object

compresses and encrypts the string



181
182
183
# File 'lib/string-encrypt.rb', line 181

def encryptz(password="")
	return self.zip.encrypt(password)
end

#is_encrypted?Boolean

checks for the salt string as the magic string at the beginning of the string. If present then it must be encrypted.

Returns:

  • (Boolean)


162
163
164
165
166
167
168
# File 'lib/string-encrypt.rb', line 162

def is_encrypted?
	if self[0...(@@magic.length)] == @@magic
		return true
	else
		return false
	end
end

#shaObject

Returns the sha1 hash of the string as a character string



157
158
159
# File 'lib/string-encrypt.rb', line 157

def sha
	return OpenSSL::Digest::Digest.new('sha1',self).to_s
end

#unzipObject

decompresses the string that was previously compressed with zip



176
177
178
# File 'lib/string-encrypt.rb', line 176

def unzip
	return Zlib::Inflate.inflate(self)	
end

#zipObject

compresses the string using zlib



171
172
173
# File 'lib/string-encrypt.rb', line 171

def zip
	return Zlib::Deflate.deflate(self,Zlib::BEST_COMPRESSION)
end