string-encrypt 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,16 @@
1
+ The string_encryption gem was started with the intention of being
2
+ compatible with the RSA and AES algorithms used in a javascript
3
+ library on http://www.pidder.com/pidcrypt . Usage and testing
4
+ against the pidcrypt library hasn't been done yet, but is scheduled
5
+ for the future.
6
+ The intent of this library is to make encryption and decryption of a
7
+ string as straight forward as capitalizing or reversing is.
8
+
9
+ To encrypt a string:
10
+ encrypted_secret = "Super Secret Text".encrypt("Super Secret Password")
11
+ To decrypt a string:
12
+ decrypted_secret = encrypted_secret.encrypt("Super Secret Password")
13
+
14
+
15
+
16
+ Branden Giacoletto
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+ require 'string-encrypt'
3
+ require 'optparse'
4
+
5
+ options = {}
6
+ if ARGV.length == 1
7
+ options["infile"] = options["outfile"] = ARGV[0]
8
+ else
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: decrypt.rb -i input.file -o output.file"
11
+ opts.on("-i", "--infile INPUT", "File to be encrypted") do |input|
12
+ options["infile"] = input.gsub("\"","")
13
+ end
14
+
15
+ opts.on("-o", "--outfile OUTPUT", "File after it\'s encrypted") do |output|
16
+ options["outfile"] = output.gsub("\"","")
17
+ end
18
+ opts.on("-p", "--password PASSWORD", "") do |pass|
19
+ options["password"] = pass
20
+ end
21
+ opts.on("-k","--key-file KEYFILE", "") do |keyfile|
22
+ options["key"] = keyfile
23
+ end
24
+ opts.on("-z", "--zip", "") do
25
+ options["zip"] = true
26
+ end
27
+ end.parse!
28
+ end
29
+ password = ""
30
+
31
+ if options["key"] != nil then
32
+ if options["password"] == nil then password_provided = false end
33
+ begin
34
+ options["password"] = rsa_key(options["key"], options["password"])
35
+ rescue
36
+ if options["infile"] != nil then
37
+ STDERR.print "What\'s the password?: "
38
+ password = STDIN.gets.chomp
39
+ options["password"] = rsa_key(options["key"],password)
40
+ else
41
+ STDERR.print "key file may be encrypted. Enter the password either with\
42
+ the -p option or read the file to be decrypted with the -i option"
43
+ # Considered reading the file to be decrypted in, but maybe getting a broken
44
+ # pipe error is more desirable
45
+ exit(1)
46
+ end
47
+ end
48
+ end
49
+
50
+ if options["infile"] == nil || options["infile"] == "-" then
51
+ file = ARGF
52
+ if options["password"] != nil then
53
+ password = options["password"]
54
+ end
55
+ else
56
+ file = File.open(options["infile"],"rb")
57
+ if options["password"] != nil then
58
+ password = options["password"]
59
+ else
60
+ STDERR.print "What\'s the password?: "
61
+ password = STDIN.gets.chomp
62
+ end
63
+ end
64
+ string = file.read
65
+ decrypted = string.decrypt(password,options["zip"])
66
+ file.close
67
+ if decrypted == nil then raise 'decryption failed!'; exit 3 end
68
+
69
+ if options["outfile"] == nil || options["outfile"] == "-" then
70
+ print decrypted
71
+ #puts "no file to output, wrote to screen"
72
+ else
73
+ File.open(options["outfile"],"wb") {|f| f << decrypted }
74
+ #puts "opened file #{options["outfile"]} for output"
75
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ print "What\'s the password?: "
4
+ password = gets
5
+ files = Dir::entries('.').collect {|entry| if File.file? entry then entry else nil end }.compact
6
+
7
+ files.each { |file| `decrypt -i "#{file}" -o "#{file}" -p "#{password}" -z` }
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # encrypt.rb is a program for encrypting an individual file or piped input
4
+ # using either a password or rsa key. If the key is beyond the allowed size
5
+ # for the contents being encrypted it is silently symmetrically encrypted
6
+ # with a random password and that random password is instead encrypted with
7
+ # the rsa key
8
+ #
9
+ # USAGE:: encrypt.rb ([-i input/filename] [-o output/filename] [(-k key/filename) || -p password || -p "pass phrase"]) || filename_changed_in_place
10
+ #
11
+ # -i:: Input file name
12
+ # -o:: Output file name
13
+ # -k:: RSA public or private key to encrypt with
14
+ # -p:: a password or pass phrase to encrypt with
15
+ #
16
+ # If no input is specified then input is taken from STDIN so a password or key
17
+ # must be provided at the time of invocation with the -k or -p options
18
+ # If not then an empty string is used for the password (NOT ADVISABLE)
19
+ #
20
+ # If no output is specified then the output is directed to STDOUT. if no
21
+ # password or key is provided you will be prompted for a password.
22
+ #
23
+ # If the input and output file are the same you can optionally pass the
24
+ # filename as the only parameter and then you will be prompted for a password
25
+
26
+ require 'string-encrypt'
27
+ require 'optparse'
28
+
29
+ options = {}
30
+ if ARGV.length == 1 then
31
+ options["outfile"] = options["infile"] = ARGV[0]
32
+ else
33
+ OptionParser.new do |opts|
34
+ opts.banner = "Usage: encrypt.rb -i input.file -o output.file"
35
+ opts.on("-i", "--infile INPUT", "File to be encrypted") do |input|
36
+ options["infile"] = input.gsub("\"","")
37
+ end
38
+
39
+ opts.on("-o", "--outfile OUTPUT", "File after it\'s encrypted") do |output|
40
+ options["outfile"] = output.gsub("\"","")
41
+ end
42
+ opts.on("-p", "--password PASSWORD", "") do |password|
43
+ options["password"] = password
44
+ end
45
+ opts.on("-k", "--key-file KEYFILE", "") do |keyfile|
46
+ options["key"] = keyfile
47
+ end
48
+ opts.on("-z","--zip","") do
49
+ options["zip"] = true
50
+ end
51
+ end.parse!
52
+ end
53
+
54
+ password = ""
55
+
56
+ if options["key"] != nil then
57
+ if options["password"] == nil then password_provided = false end
58
+ begin
59
+ options["password"] = rsa_key(options["key"], options["password"])
60
+ rescue
61
+ if options["infile"] != nil then
62
+ STDERR.print "What\'s the password?: "
63
+ password = STDIN.gets.chomp
64
+ options["password"] = rsa_key(options["key"],password)
65
+ else
66
+ STDERR.puts "key file may be encrypted, provide the password with -p option\
67
+ read the file with -i instead of through a pipe."
68
+ #raise "key file may be encrypted"
69
+ exit(1)
70
+ end
71
+ end
72
+ end
73
+
74
+ if options["infile"] == nil || options["infile"] == "-" then
75
+ f = ARGF
76
+ if options["password"] != nil then
77
+ password = options["password"]
78
+ end
79
+ #puts "no file, reading from prompt"
80
+ else
81
+ f = File.open(options["infile"],"rb")
82
+ if options["password"] != nil then
83
+ password = options["password"]
84
+ else
85
+ STDERR.print "What\'s the password?: "
86
+ password = STDIN.gets.chomp
87
+ end
88
+ #puts "opened file #{options["infile"]} for reading"
89
+ #puts.flush
90
+ end
91
+ string = f.read
92
+ f.close
93
+ if string.length == 0 then raise 'can not encrypt nothing'; exit 2 end
94
+
95
+ encrypted = string.encrypt(password,options["zip"])
96
+ # if it fails once, try again
97
+ if encrypted == nil then
98
+ encrypted = string.encrypt(password,options["zip"])
99
+ end
100
+ # if it fails twice, quit with an error
101
+ if encrypted == nil then raise 'Could not encrypt successfully'; exit 1 end
102
+
103
+ if options["outfile"] == nil || options["outfile"] == "-" then
104
+ print encrypted
105
+ #puts "no file to output, wrote to screen"
106
+ else
107
+ File.open(options["outfile"],"wb") {|f| f << encrypted }
108
+ #puts "opened file #{options["outfile"]} for output"
109
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ print "What\'s the password?: "
4
+ password = gets
5
+ files = Dir::entries('.').collect {|entry| if File.file? entry then entry else nil end }.compact
6
+
7
+ files.each { |file| if file != "decrypt.rb" && file != "decrypt_all.rb" then `encrypt -i "#{file}" -o "#{file}" -p "#{password}" -z` end }
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # journal.rb is a stand-alone program that
4
+ # 1. decrypts the journal file using the private key that's been symetrically encrypted with a password.
5
+ # 2. opens the decrypted journal file with vim
6
+ # 3. then encrypts the edited file back with the public key
7
+
8
+ if ARGV.length == 0 then
9
+ filename = "journal.txt"
10
+ else
11
+ filename = ARGV[0]
12
+ end
13
+ if File.exists? filename then
14
+ system("decrypt -i rsa.priv -o tmp_priv.pem")
15
+ `decrypt -i #{filename} -o #{filename}.tmp -z -k tmp_priv.pem`
16
+ system "clear"
17
+ `rm tmp_priv.pem`
18
+ if File.exists? "#{filename}.tmp" then
19
+ `echo $(date) >> #{filename}.tmp`
20
+ system("vim #{filename}.tmp")
21
+ `encrypt -i #{filename}.tmp -o #{filename} -z -k rsa.pub`
22
+ `rm #{filename}.tmp`
23
+ else
24
+ puts "decryption failed"
25
+ end
26
+ else
27
+ puts "file does not exists"
28
+ end
@@ -0,0 +1,189 @@
1
+ #!/usr/bin/env ruby
2
+ # == Synopsis
3
+ # The encryption.rb library simplifies the creation and loading of RSA keys
4
+ # along with encrypting/decrypting strings using the RSA keys or AES-256 password
5
+ # with salt. Encryption/Decryption is simplified by opening the String class with
6
+ # a encrypt and decrypt method and one for rsa keys (encryptr/decryptr) which are
7
+ # called automatically from the former methods if the password argument is an RSA
8
+ # key. To reduce the size of the resulting encrypted file it can optionally be
9
+ # compressed before encryption using encryptz/decryptz methods.
10
+ # == Usage
11
+ # * encrypted_string = "string to encrypt with AES-256".encrypt("super secret password")
12
+ # * key = rsa_key(1024);
13
+ # encrypted_string = "string to encrypt with rsa key".encryptr(key.public_key)
14
+ # or
15
+ # encrypted_string = "string to encrypt with rsa key".encrypt(key.public_key)
16
+ # * encrypted_string = "string to compress then encrypt".encryptz("super secret password")
17
+ # [Note] decryption is done with the corresponding decrypt/decryptr/decryptz methods.
18
+ # [Note] if you encrypted with a public key like above, you decrypt with the private (master) key
19
+ # (either key or key.private_key). If you encrypted with the private (master) key you can
20
+ # decrypt with either the private or the public key (preferably the public key, but it's your choice)
21
+ # [Note] if the RSA key to use is in a file use: key = rsa_key("public_key_file.pub") or key = rsa_key("private_key_file.pub")
22
+ #
23
+ # == Author
24
+ # Branden Giacoletto, Carbondata Digital Services
25
+ #
26
+ # == Copyright
27
+ # Copyright (c) 2009 Carbondata Digital Services
28
+ # Licensed under the same terms as Ruby
29
+
30
+ require 'openssl'
31
+ require 'zlib'
32
+ require 'yaml'
33
+ # require 'base64'
34
+
35
+ # rsa key will either create a new RSA key if a bit size is supplied (multiples of 1024, 15 is supposed to be equivalent to AES-256) or
36
+ # the filename of an already created key.
37
+ # If a password is given then the rsa file will be decrypted.
38
+ def rsa_key(bitsize_or_filename,password = nil)
39
+ if bitsize_or_filename.class.to_s == "String" && password == nil
40
+ return OpenSSL::PKey::RSA.new(File.read(bitsize_or_filename))
41
+ elsif bitsize_or_filename.class.to_s == "String" && password != nil
42
+ return OpenSSL::PKey::RSA.new(File.read(bitsize_or_filename).decrypt(password))
43
+ elsif bitsize_or_filename.class.to_s != "String" && password == nil then
44
+ return OpenSSL::PKey::RSA.new(bitsize_or_filename)
45
+ end
46
+ end
47
+
48
+ # Opens the String class providing encryption and zipping methods
49
+ class String
50
+ @@magic = "Salted__"
51
+ @@salt_len = 8
52
+ # decrypts the string using either and string password or RSA key created with rsa_key()
53
+ # =====example
54
+ # * <tt> "string to decrypt".decrypt("super secret password")</tt> # using a string for a password
55
+ # * <tt> "string to decrypt".decrypt(key.public_key)</tt> # using the public key
56
+ # * <tt> "string to decrypt".decrypt(key.private_key)</tt> # using the private key
57
+ # * <tt> "string to decrypt".decrypt(key)</tt> # using the private key unless specifically loaded the public key
58
+ #
59
+ # if true is passed for the second argument then the string is decompressed after it is decrypted
60
+ def decrypt(password = "", unzip_it = false)
61
+ if password.class.to_s == "OpenSSL::PKey::RSA" then
62
+ result = nil
63
+ begin
64
+ result = self.decryptr(password)
65
+ rescue
66
+ enc_password_size = self[0...6].to_i
67
+ enc_password = self[6...(enc_password_size + 6)]
68
+ message = self[(enc_password_size + 6)..-1]
69
+ random_pass = (enc_password).decryptr(password)
70
+ decrypted_data = message.decrypt(random_pass)
71
+ result = decrypted_data
72
+ end
73
+ if unzip_it == true then result = result.unzip end
74
+ return result
75
+ end
76
+ salt = ""
77
+ if !self.is_encrypted?
78
+ return nil # Salt is wrong
79
+ end
80
+ salt = self[(@@magic.length)...(@@salt_len+@@magic.length)]
81
+ c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
82
+ c.decrypt
83
+ c.pkcs5_keyivgen(password,salt,1)
84
+ result = nil
85
+ begin
86
+ result = (c.update(self[@@salt_len+@@magic.length..-1]) + c.final)
87
+ if unzip_it == true then result = result.unzip end
88
+ rescue
89
+ result = nil
90
+ end
91
+ return result
92
+ end
93
+
94
+ # encrypts the string using either and string password or RSA key created with rsa_key()
95
+ # =====example
96
+ # * <tt> "string to encrypt".encrypt("super secret password")</tt> # using a string for a password
97
+ # * <tt> "string to encrypt".encrypt(key.public_key)</tt> # using the public key
98
+ # * <tt> "string to encrypt".encrypt(key.private_key)</tt> # using the private key
99
+ # * <tt> "string to encrypt".encrypt(key)</tt> # using the private key unless specifically loaded the public key
100
+ #
101
+ # if true is passed for the second argument then the string is compressed before it is encrypted
102
+ # =====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
+ def encrypt(password = "", zipit = false)
104
+ if password.class.to_s == "OpenSSL::PKey::RSA" then
105
+ result = nil
106
+ begin
107
+ result = (if zipit then self.zip else self end).encryptr(password)
108
+ rescue
109
+ enc_password_size = 0
110
+ random_pass = OpenSSL::Random.random_bytes(117)
111
+ rand_enc_password = (random_pass.encryptr(password))
112
+ enc_password_size = rand_enc_password.length.to_s.ljust(6)
113
+ result = enc_password_size + rand_enc_password + (if zipit then self.zip else self end).encrypt(random_pass)
114
+ end
115
+ return result
116
+ end
117
+ c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
118
+ salt = (0...(@@salt_len)).inject(""){ |carry,num| carry += rand(256).chr }
119
+ c.encrypt
120
+ c.pkcs5_keyivgen(password,salt,1)
121
+ result = nil
122
+ begin
123
+ result = @@magic + salt + c.update(if zipit then self.zip else self end) + c.final
124
+ rescue
125
+ result = nil
126
+ end
127
+ # make sure it can be successfully decrypted
128
+ if self.sha != result.decrypt(password,zipit).sha then result = nil end
129
+ return result
130
+ end
131
+
132
+ # encrypts the string specifically with a RSA key. Is called from encrypt if a key is passed for the password
133
+ def encryptr(key)
134
+ if (key.to_s =~ /^-----BEGIN (RSA|DSA) PRIVATE KEY-----$/).nil?
135
+ return key.public_encrypt(self)
136
+ else
137
+ return key.private_encrypt(self)
138
+ end
139
+ end
140
+
141
+ # decrypts the string specifically with a RSA key. Is called from decrypt if a key is passed for the password
142
+ def decryptr(key)
143
+ if (key.to_s =~ /^-----BEGIN (RSA|DSA) PRIVATE KEY-----$/).nil?
144
+ return key.public_decrypt(self)
145
+ else
146
+ begin
147
+ result = nil
148
+ result = key.private_decrypt(self)
149
+ rescue
150
+ result = key.public_decrypt(self)
151
+ end
152
+ return result
153
+ end
154
+ end
155
+
156
+ # Returns the sha1 hash of the string as a character string
157
+ def sha
158
+ return OpenSSL::Digest::Digest.new('sha1',self).to_s
159
+ end
160
+
161
+ # checks for the salt string as the magic string at the beginning of the string. If present then it must be encrypted.
162
+ def is_encrypted?
163
+ if self[0...(@@magic.length)] == @@magic
164
+ return true
165
+ else
166
+ return false
167
+ end
168
+ end
169
+
170
+ # compresses the string using zlib
171
+ def zip
172
+ return Zlib::Deflate.deflate(self,Zlib::BEST_COMPRESSION)
173
+ end
174
+
175
+ # decompresses the string that was previously compressed with zip
176
+ def unzip
177
+ return Zlib::Inflate.inflate(self)
178
+ end
179
+
180
+ # compresses and encrypts the string
181
+ def encryptz(password="")
182
+ return self.zip.encrypt(password)
183
+ end
184
+
185
+ # uncompresses and decrypts the string
186
+ def decryptz(password="")
187
+ return self.decrypt(password).unzip
188
+ end
189
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: string-encrypt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Branden Giacoletto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-12 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: branden@rubyforge.com
18
+ executables:
19
+ - journal.rb
20
+ - encrypt.rb
21
+ - encrypt_all.rb
22
+ - decrypt.rb
23
+ - decrypt_all.rb
24
+ extensions: []
25
+
26
+ extra_rdoc_files:
27
+ - bin/journal.rb
28
+ - bin/encrypt.rb
29
+ - bin/encrypt_all.rb
30
+ - bin/decrypt.rb
31
+ - bin/decrypt_all.rb
32
+ - README
33
+ - lib/string-encrypt.rb
34
+ files:
35
+ - bin/journal.rb
36
+ - bin/encrypt.rb
37
+ - bin/encrypt_all.rb
38
+ - bin/decrypt.rb
39
+ - bin/decrypt_all.rb
40
+ - lib/string-encrypt.rb
41
+ - README
42
+ has_rdoc: true
43
+ homepage: http://string-encrypt.rubyforge.com
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project: string-encrypt
64
+ rubygems_version: 1.3.1
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: String open classed with AES-256 and RSA encryption and zipping methods for easy, secure, encryption of strings
68
+ test_files: []
69
+