Created
April 18, 2012 13:51
-
-
Save r4um/2413699 to your computer and use it in GitHub Desktop.
System::Security::Cryptography file enc/dec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'System' | |
require 'pp' | |
include System | |
include System::Text | |
include System::Security::Cryptography | |
class CredentialStore | |
def initialize(file) | |
@file = file | |
@rijndaelAlg = Rijndael.create | |
pp @rijndaelAlg.Key | |
pp @rijndaelAlg.IV | |
end | |
def write(data) | |
#Create or open the specified file. | |
fStream = System::IO::File.open(@file, System::IO::FileMode.open_or_create) | |
#Create a new Rijndael object. | |
rijndaelAlg = Rijndael.create | |
# Create a CryptoStream using the FileStream | |
# and the passed key and initialization vector (IV). | |
cStream = CryptoStream.new(fStream, | |
rijndaelAlg.CreateEncryptor(@rijndaelAlg.Key, @rijndaelAlg.IV), | |
CryptoStreamMode.Write) | |
#Create a StreamWriter using the CryptoStream. | |
sWriter = System::IO::StreamWriter.new(cStream) | |
sWriter.write_line(data) | |
sWriter.Close() | |
cStream.Close() | |
fStream.Close() | |
end | |
def read() | |
#Create or open the specified file. | |
fStream = System::IO::File.open(@file, System::IO::FileMode.open) | |
#Create a new Rijndael object. | |
rijndaelAlg = Rijndael.create | |
# Create a CryptoStream using the FileStream | |
# and the passed key and initialization vector (IV). | |
cStream = CryptoStream.new(fStream, | |
rijndaelAlg.CreateDecryptor(@rijndaelAlg.Key, @rijndaelAlg.IV), | |
CryptoStreamMode.Read) | |
#Create a StreamWriter using the CryptoStream. | |
sReader = System::IO::StreamReader.new(cStream) | |
d = sReader.read_line() | |
sReader.Close() | |
cStream.Close() | |
fStream.Close() | |
return d | |
end | |
end | |
cs = CredentialStore.new("foo.dat") | |
cs.write("encrypted") | |
puts cs.read |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment