vb.net加密字符串 vbnet字符串函数
求VB.NET生成TET文件的加密方法
使用加密方式存储即可实现别人无法查看内容,加密的方式有很多,适用你这里使用的是可逆的算法,推荐你使用DES加密
作为一家“创意+整合+营销”的成都网站建设机构,我们在业内良好的客户口碑。成都创新互联公司提供从前期的网站品牌分析策划、网站设计、成都网站制作、成都网站建设、外贸营销网站建设、创意表现、网页制作、系统开发以及后续网站营销运营等一系列服务,帮助企业打造创新的互联网品牌经营模式与有效的网络营销方法,创造更大的价值。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Namespace ZU14
NotInheritable Public Class DES
Private iv As String = "1234的yzo"
Private key As String = "123在yzo"
'/ summary
'/ DES加密偏移量,必须是=8位长的字符串
'/ /summary
Public Property IV() As String
Get
Return iv
End Get
Set
iv = value
End Set
End Property
'/ summary
'/ DES加密的私钥,必须是8位长的字符串
'/ /summary
Public Property Key() As String
Get
Return key
End Get
Set
key = value
End Set
End Property
'/ summary
'/ 对字符串进行DES加密
'/ /summary
'/ param name="sourceString"待加密的字符串/param
'/ returns加密后的BASE64编码的字符串/returns
Public Function Encrypt(sourceString As String) As String
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim ms As New MemoryStream()
Try
Dim inData As Byte() = Encoding.Default.GetBytes(sourceString)
Try
Dim cs As New CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(inData, 0, inData.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Return Convert.ToBase64String(ms.ToArray())
Catch
End Try
Finally
ms.Dispose()
End Try
End Function 'Encrypt
'/ summary
'/ 对DES加密后的字符串进行解密
'/ /summary
'/ param name="encryptedString"待解密的字符串/param
'/ returns解密后的字符串/returns
Public Function Decrypt(encryptedString As String) As String
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim ms As New MemoryStream()
Try
Dim inData As Byte() = Convert.FromBase64String(encryptedString)
Try
Dim cs As New CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(inData, 0, inData.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Return Encoding.Default.GetString(ms.ToArray())
Catch
End Try
Finally
ms.Dispose()
End Try
End Function 'Decrypt
'/ summary
'/ 对文件内容进行DES加密
'/ /summary
'/ param name="sourceFile"待加密的文件绝对路径/param
'/ param name="destFile"加密后的文件保存的绝对路径/param
Overloads Public Sub EncryptFile(sourceFile As String, destFile As String)
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)
End If
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim btFile As Byte() = File.ReadAllBytes(sourceFile)
Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)
Try
Try
Dim cs As New CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(btFile, 0, btFile.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Catch
Finally
fs.Close()
End Try
Finally
fs.Dispose()
End Try
End Sub 'EncryptFile
'/ summary
'/ 对文件内容进行DES加密,加密后覆盖掉原来的文件
'/ /summary
'/ param name="sourceFile"待加密的文件的绝对路径/param
Overloads Public Sub EncryptFile(sourceFile As String)
EncryptFile(sourceFile, sourceFile)
End Sub 'EncryptFile
'/ summary
'/ 对文件内容进行DES解密
'/ /summary
'/ param name="sourceFile"待解密的文件绝对路径/param
'/ param name="destFile"解密后的文件保存的绝对路径/param
Overloads Public Sub DecryptFile(sourceFile As String, destFile As String)
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)
End If
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim btFile As Byte() = File.ReadAllBytes(sourceFile)
Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)
Try
Try
Dim cs As New CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(btFile, 0, btFile.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Catch
Finally
fs.Close()
End Try
Finally
fs.Dispose()
End Try
End Sub 'DecryptFile
'/ summary
'/ 对文件内容进行DES解密,加密后覆盖掉原来的文件
'/ /summary
'/ param name="sourceFile"待解密的文件的绝对路径/param
Overloads Public Sub DecryptFile(sourceFile As String)
DecryptFile(sourceFile, sourceFile)
End Sub 'DecryptFile
End Class 'DES
End Namespace 'ZU14
对文本文件加密
Dim des As New ZU14.DES()
des.IV = "abcd哈哈笑"
des.Key = "必须八位"
'加密
des.EncryptFile("d:\a.txt", "d:\b.txt")
'解密
des.DecryptFile("d:\b.txt")
vb.net比较字符串
vb.net规定如果要比较字符串,不能用“=”,strcomp(str1,str2,n)或者string.compare(str1,str2)
例如:
Dim a As String = "c"
Dim b As String = "c"
Dim n As Integer = String.Compare(a, b)
If n = 0 Then
MsgBox("=")
Else
MsgBox("")
End If
vb.net中实现rsa加密解密 急!急!
我觉得你的并不是RSA加密解密算法。
在.net的有一个System.Security.Cryptography的命名空间,里面有一RSACryptoServiceProvider的类用来对byte进行RSA加密解密。
具体例子如下:
using System;
using System.Security.Cryptography;
using System.Text;
class RSACSPSample
{
static void Main()
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();
//Create byte arrays to hold original, encrypted, and decrypted data.
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false), false);
//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);
//Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
}
catch(ArgumentNullException)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine("Encryption failed.");
}
}
static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Import the RSA Key information. This only needs
//toinclude the public key information.
RSA.ImportParameters(RSAKeyInfo);
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch(CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Import the RSA Key information. This needs
//to include the private key information.
RSA.ImportParameters(RSAKeyInfo);
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch(CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
}
[Visual Basic]
Try
'Create a new RSACryptoServiceProvider object.
Dim RSA As New RSACryptoServiceProvider()
'Export the key information to an RSAParameters object.
'Pass false to export the public key information or pass
'true to export public and private key information.
Dim RSAParams As RSAParameters = RSA.ExportParameters(False)
Catch e As CryptographicException
'Catch this exception in case the encryption did
'not succeed.
Console.WriteLine(e.Message)
End Try
[C#]
try
{
//Create a new RSACryptoServiceProvider object.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Export the key information to an RSAParameters object.
//Pass false to export the public key information or pass
//true to export public and private key information.
RSAParameters RSAParams = RSA.ExportParameters(false);
}
catch(CryptographicException e)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine(e.Message);
}
VB.NET UNICODE码 显示
Public Class Form1
Dim b() As Byte
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
b = System.Text.Encoding.Default.GetBytes(TextBox1.Text)
For i = 0 To UBound(b)
TextBox2.AppendText(i.ToString " ")
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = System.Text.Encoding.Default.GetString(b)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "Google free online translation service instantly translates text and web pages。 该翻译器支持: 中文(简体), 中文(繁体), shqip, 日本语, русский, langue française ..."
End Sub
End ClassVB.Net中用String类型表示字符串,内部采用Unicode编码。当需要在网络或串口中收发字符串时,就需要在String和Byte数组之间进行转换,这项功能可以通过System.Text.Encoding类实现。
Private zeroChars() As Char = {ChrW(0)}
Dim descBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(mDescription)
Dim description As String = System.Text.Encoding.Unicode.GetString(rBuffer, offset, length).TrimEnd(zeroChars)
说明:C语言中用'\0'表示字符串结束,而String类型中0是有效字符,显示时是空白字符,会占用显示宽度,可以用TrimEnd方法将字符串末尾的零字符去掉。
主要叙说一下StrConv 函数conversion参数最后两个值的含义和用途,并举例说明。
1、语法
StrConv(string, conversion, LCID)
StrConv 函数的语法有下面的命名参数:
部分 说明
string 必要参数。要转换的字符串表达式。
conversion 必要参数。Integer。其值的和决定转换的类型。
LCID 可选的。如果与系统LocaleID不同,则为LocaleID(系统LocaleID为缺省值。)
设置值
conversion 参数的设置值为:
常数 值 说明
vbUpperCase 1 将字符串文字转成大写。
vbLowerCase 2 将字符串文字转成小写。
vbProperCase 3 将字符串中每个字的开头字母转成大写。
vbWide* 4* 将字符串中单字节字符转成双字节字符。
vbNarrow* 8* 将字符串中双字节字符转成单字节字符。
vbKatakana** 16** 将字符串中平假名字符转成片假名字符。
vbHiragana** 32** 将字符串中片假名字符转成平假名字符。
vbUnicode 64 根据系统的缺省码页将字符串转成 Unicode。
vbFromUnicode 128 将字符串由 Unicode 转成系统的缺省码页。
*应用到远东国别。
**仅应用到日本。
说明:前面3个参数比较简单,后面4个我们用不上,就不说了,主要说说后面两个。
2、ANSI 格式
语法中说的缺省码页就是ANSI模式,英文环境下 的ANSI 格式其实也就是ASCII码,其它环境就不一样了,比如中文环境,就是ASCII,一个字节表示一个字符,GB2312,2个字节表示一个汉字,所以中文环境下的ANSI格式就是ASCII码+GB2312,早期的DOS系统中纯文本就是这种格式,这种格式下,通过最高位来判断是中文字符(最高位是1)还是ASCII字符(最高位是0)。中文环境下保存文本文件时一般都采用ANSI格式,不过也有其他格式,比如UTF-8。
3、Unicode编码
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。Unicode用两个字节表示一个字符,涵盖了世界上所有字符,和以前的字符集都不兼容,VB内部字符串就是采用Unicode编码,所以当我们打开一个文本文件读入数据的时候,其内存中的内容和文本文件的内容是不一样的,经过了转换,除非你采用二进制方式读入。
4、vbUnicode和vbFromUnicode含义
有了上面叙说,这两个参数的含义就好理解了,就是Unicode编码和ANSI编码的互换,例如:
textline= StrConv(plaintext,vbUnicode)
这儿textline是以字符串变量,plaintext是以字节变量保存着ANSI模式的字符内容,例如,“2”这个字符,一个字节,值是50,16进制是32,“皖”这个汉字,两个字节,值是205和238,同样是这两个字符,作为字符串在内存中都是两个字节,例如,“2”这个字符在内存中16进制值是0032。当VB打开一个文件读取文本内容是,实际上自动进行了上述转换。
plaintext = StrConv(textline, vbFromUnicode)
这儿进行相反的转换,就是将Unicode字符串转换成ANSI模式,转换结果必须以字节方式保存。
5、vbUnicode和vbFromUnicode用途
由于字符在内存中的内容和文件中的内容不一致,所以必须要用到这种转换,特别是系统间进行数据交换、数据加密和解密,如果不做转换可能导致得不到正确的结果。
比如,我们对一个文本文件进行加密,这个文件是ANSI格式存储的,当从文件内容读入一行到内存的时候,自动将内容转换成了Unicode格式,如果这时候对其做加密运算,其结果和文件中字符串加密结果是不一样的,这样的结果如果让别人解密将无法得到正确的结果。如果对读入内存的内容先做个转换(textline是读入内容):
plaintext = StrConv(textline, vbFromUnicode)
再对plaintext做加密,其结果就一样了。
举例(按行做加密和解密运算,算法是AES+Base64):
[vb] view plain copy
Status = "Encrypting File"
Open FileName For Input As #1 ' 打开输入文件。
Open FileName2 For Output As #2 ' 打开输出文件。
Do While Not EOF(1)
Line Input #1, TextLine
plaintext = StrConv(TextLine, vbFromUnicode)
Status = "Encrypting Data"
m_Rijndael.SetCipherKey pass, KeyBits
m_Rijndael.ArrayEncrypt plaintext, ciphertext, 0
Status = "Converting Text to Base64"
TextLine = Base64Encode(ciphertext)
Status = ""
Print #2, TextLine ' 将字符串写入文件。
Loop
Close
[vb] view plain copy
Status = "Decrypting File"
Open FileName For Input As #1 ' 打开输入文件。
Open FileName2 For Output As #2 ' 打开输出文件。
Do While Not EOF(1)
Line Input #1, TextLine
Status = "Converting Base64 to Text"
ciphertext = Base64Decode(TextLine)
Status = "Decrypting Data"
m_Rijndael.SetCipherKey pass, KeyBits
If m_Rijndael.ArrayDecrypt(plaintext, ciphertext, 0) 0 Then
Status = ""
Exit Sub
End If
TextLine = StrConv(plaintext, vbUnicode)
For i = 0 To UBound(plaintext)
Debug.Print plaintext(i)
Next i
k = InStr(1, TextLine, Chr(0), vbBinaryCompare)
If k 0 Then TextLine = Left(TextLine, k - 1) '截掉加密时补的0
MsgBox TextLine "end"
Status = ""
Print #2, TextLine ' 将字符串写入文件。
Loop
Close
标题名称:vb.net加密字符串 vbnet字符串函数
URL分享:http://scyanting.com/article/hisdpd.html