imports system.io
imports system.security.cryptography
数据加/解密 类
public class crydata
加密密钥,初始化向量
public readonly crykey as byte() = {9, 4, 2, 8, 5, 1, 4, 9, 7, 6, 9, 5, 1, 13, 7, 5, 14, 9, 10, 15, 0, 1, 14, 5, 9, 4, 3, 8, 2, 10}
public readonly cryiv as byte() = {7, 1, 8, 8, 2, 8, 7, 1, 4, 5, 6, 3, 5, 6, 7}
文件加密
public sub encryptdata(byval inname as string, byval outname as string, _
optional byval rijnkey() as byte = nothing, _
optional byval rijniv() as byte = nothing)
if rijnkey is nothing then
rijnkey = crykey
end if
if rijniv is nothing then
rijniv = cryiv
end if
redim preserve rijnkey(31)
redim preserve rijniv(15)
create the file streams to handle the input and output files.
dim fin as new filestream(inname, filemode.open, fileaccess.read)
dim fout as new filestream(outname, filemode.openorcreate, fileaccess.readwrite)
fout.setlength(0)
create variables to help with read and write.
dim bin(1024) as byte this is intermediate storage for the encryption.
dim rdlen as long = 0 this is the total number of bytes written.
dim totlen as long = fin.length total length of the input file.
dim len as integer this is the number of bytes to be written at a time.
creates the default implementation, which is rijndaelmanaged.
dim rijn as symmetricalgorithm = symmetricalgorithm.create()
dim encstream as new cryptostream(fout, _
rijn.createencryptor(rijnkey, rijniv), cryptostreammode.write)
console.writeline("encrypting…")
read from the input file, then encrypt and write to the output file.
while rdlen < totlen
len = fin.read(bin, 0, 1024)
encstream.write(bin, 0, len)
rdlen = convert.toint32(rdlen + len)
console.writeline("{0} bytes processed", rdlen)
end while
fout.seek(0, seekorigin.begin)
dim returnvalue as string
returnvalue = new streamreader(fout).readtoend()
encstream.close()
fout.close()
fin.close()
end sub
文件解密
public sub decryptdata(byval inname as string, byval outname as string, _
optional byval rijnkey() as byte = nothing, _
optional byval rijniv() as byte = nothing)
if rijnkey is nothing then
rijnkey = crykey
end if
if rijniv is nothing then
rijniv = cryiv
end if
redim preserve rijnkey(31)
redim preserve rijniv(15)
create the file streams to handle the input and output files.
dim fin as new filestream(inname, filemode.open, fileaccess.read)
dim fout as new filestream(outname, filemode.openorcreate, _
fileaccess.readwrite)
fout.setlength(0)
create variables to help with read and write.
dim bin(1024) as byte this is intermediate storage for the encryption.
dim rdlen as long = 0 this is the total number of bytes written.
dim totlen as long = fin.length total length of the input file.
dim len as integer this is the number of bytes to be written at a time.
creates the default implementation, which is rijndaelmanaged.
dim rijn as symmetricalgorithm = symmetricalgorithm.create()
dim encstream as new cryptostream(fout, _
rijn.createdecryptor(rijnkey, rijniv), cryptostreammode.write)
console.writeline("encrypting…")
read from the input file, then encrypt and write to the output file.
while rdlen < totlen
len = fin.read(bin, 0, 1024)
encstream.write(bin, 0, len)
rdlen = convert.toint32(rdlen + len)
console.writeline("{0} bytes processed", rdlen)
end while
encstream.close()
fout.close()
fin.close()
end sub
文件解密
public function decryptdata(byval inname as string, _
optional byval rijnkey() as byte = nothing, _
optional byval rijniv() as byte = nothing) as string
if rijnkey is nothing then
rijnkey = crykey
end if
if rijniv is nothing then
rijniv = cryiv
end if
redim preserve rijnkey(31)
redim preserve rijniv(15)
create the file streams to handle the input and output files.
dim fin as new filestream(inname, filemode.open, fileaccess.read)
dim fout as new filestream(outname, filemode.openorcreate, _
fileaccess.readwrite)
存储流,
dim outstream as new memorystream() (arrinbyte, true) (arrinbyte, true)
create variables to help with read and write.
dim bin(1024) as byte this is intermediate storage for the encryption.
dim rdlen as long = 0 this is the total number of bytes written.
dim totlen as long = fin.length total length of the input file.
dim len as integer this is the number of bytes to be written at a time.
creates the default implementation, which is rijndaelmanaged.
while rdlen < totlen
len = fin.read(bin, 0, 1024)
outstream.write(bin, 0, len)
rdlen = convert.toint32(rdlen + len)
console.writeline("{0} bytes processed", rdlen)
end while
outstream.seek(0, seekorigin.begin)
dim rijn as symmetricalgorithm = symmetricalgorithm.create()
dim encstream as new cryptostream(outstream, _
rijn.createdecryptor(rijnkey, rijniv), cryptostreammode.read)
console.writeline("encrypting…")
read from the input file, then encrypt and write to the output file.
dim returnvalue as string
returnvalue = new streamreader(encstream, new system.text.unicodeencoding()).readtoend()
encstream.close()
outstream.close()
fin.close()
return returnvalue
end function
加密
in: intext 待加密原始文本
in: rijnkey 32位密钥
in: rijniv 16位初始化向量
out: return 加密后的文本(为空则加密失败)
public function crypt(byval intext as string, _
optional byval rijnkey() as byte = nothing, _
optional byval rijniv() as byte = nothing) as string
create the file streams to handle the input and output files.
dim fin as new filestream(inname, filemode.open, fileaccess.read)
dim arrinbyte as byte() = (new system.text.unicodeencoding()).getbytes(intext)
dim len as long = arrinbyte.length
if (len mod 32) > 0 then
dim oldlen as long = len
len = oldlen + 32 – oldlen mod 32
redim preserve arrinbyte(len – 1)
end if
if rijnkey is nothing then
rijnkey = crykey
end if
if rijniv is nothing then
rijniv = cryiv
end if
redim preserve rijnkey(31)
redim preserve rijniv(15)
dim outstream as new memorystream() (arrinbyte, true) (arrinbyte, true)
dim rij as symmetricalgorithm = symmetricalgorithm.create()
dim encstream as new cryptostream(outstream, _
rij.createencryptor(rijnkey, rijniv), _
cryptostreammode.write)
encstream.write(arrinbyte, 0, len)
outstream.seek(0, seekorigin.begin)
dim returnvalue as string
returnvalue = new streamreader(outstream, new system.text.unicodeencoding()).readtoend()
encstream.close()
outstream.close()
return returnvalue
end function
——————————————————————————————————–
解密
in: intext 待解密密文
in: rijnkey 32位解密密钥(须跟加密时的相同)
in: rijniv 16位解密初始化向量(须跟加密时的相同)
out: return 解密后的文本(为空则解密失败)
解密 过程:把密文换成字节写到内存流,再跟据rijnkey和rijniv创建解密流
从解密流中读取所有的数据
——————————————————————————————————–
public function decrypt(byval intext as string, _
optional byval rijnkey() as byte = nothing, _
optional byval rijniv() as byte = nothing) as string
密文转换成字节
dim arrinbyte as byte() = (new system.text.unicodeencoding()).getbytes(intext)
存储流,
dim outstream as new memorystream() (arrinbyte, true) (arrinbyte, true)
dim len as long = arrinbyte.length
if rijnkey is nothing then
rijnkey = crykey
end if
if rijniv is nothing then
rijniv = cryiv
end if
redim preserve rijnkey(31)
redim preserve rijniv(15)
dim rij as symmetricalgorithm = symmetricalgorithm.create()
outstream.write(arrinbyte, 0, len)
outstream.seek(0, seekorigin.begin)
dim encstream as new cryptostream(outstream, _
rij.createdecryptor(rijnkey, rijniv), _
cryptostreammode.read)
dim returnvalue as string
returnvalue = new streamreader(encstream, new system.text.unicodeencoding()).readtoend()
encstream.close()
outstream.close()
return returnvalue
end function
end class
加密
http://community.csdn.net/expert/topic/3199/3199494.xml?temp=.982464
public shared function encrypttext(byval strtext as string) as string
return encrypt(strtext, "&%#@?./)")
end function
解密
public shared function decrypttext(byval strtext as string) as string
return decrypt(strtext, "&%#@?./)")
end function
加密函数
private shared function encrypt(byval strtext as string, byval strencrkey as string) as string
dim bykey() as byte = {}
dim iv() as byte = {&h12, &h34, &h56, &h78, &h90, &hab, &hcd, &hef}
try
bykey = system.text.encoding.utf8.getbytes(left(strencrkey, 8))
bykey = system.text.encoding.utf8.getbytes(strencrkey.substring(0, 8))
dim des as new system.security.cryptography.descryptoserviceprovider
dim inputbytearray() as byte = system.text.encoding.utf8.getbytes(strtext)
dim ms as new memorystream
dim cs as new cryptostream(ms, des.createencryptor(bykey, iv), cryptostreammode.write)
cs.write(inputbytearray, 0, inputbytearray.length)
cs.flushfinalblock()
return convert.tobase64string(ms.toarray())
catch ex as exception
return ex.message
end try
end function
解密函数
private shared function decrypt(byval strtext as string, byval sdecrkey as string) as string
dim bykey() as byte = {}
dim iv() as byte = {&h12, &h34, &h56, &h78, &h90, &hab, &hcd, &hef}
dim inputbytearray(strtext.length) as byte
try
bykey = system.text.encoding.utf8.getbytes(left(sdecrkey, 8))
bykey = system.text.encoding.utf8.getbytes(sdecrkey.substring(0, 8))
dim des as new descryptoserviceprovider
inputbytearray = convert.frombase64string(strtext)
dim ms as new memorystream
dim cs as new cryptostream(ms, des.createdecryptor(bykey, iv), cryptostreammode.write)
cs.write(inputbytearray, 0, inputbytearray.length)
cs.flushfinalblock()
dim encoding as system.text.encoding = system.text.encoding.utf8
return encoding.getstring(ms.toarray())
catch ex as exception
return ex.message
end try
end function
