大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用加密方式存储即可实现别人无法查看内容,加密的方式有很多,适用你这里使用的是可逆的算法,推荐你使用DES加密
创新互联成立与2013年,先为夏县等服务建站,夏县等地企业,进行企业商务咨询服务。为夏县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
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")
"采用DES算法"这个说法不明确,首先是使用多少位的DES进行加密,通常是128位或192位,其次是,要先把主密钥转化成散列,才能供DES进行加密,转化的方法是什么没有明确,通常是md5,所以有的银行卡说是128位md5 3DS就是指用md5转换主密钥散列,用DES进行加密,但是DES本身是64位(包含校验码),2DES是128位,3DES是192位,但是没有2DES的叫法,所以128位、192位统称3DES
要完整的md5+3DS实例,需要100分以上,要不到我的空间中查找相关的文章
我觉得你的并不是RSA加密解密算法。
在点虐 的有一个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);
}
Function ByteToBin(m As Byte) As String ' 将字节型数据转换成八位二进制字符串
Dim c$
c$ = ""
Do While m 0
r = m Mod 2
m = m \ 2
c$ = r c$
Loop
c$ = Right("00000000" c$, 8)
ByteToBin = c$
End Function
Function Reverse(m As String) As String ' 将八位二进制字符串颠倒顺序
Dim i%, x$
x = ""
For i = 1 To 8
x = Mid(m, i, 1) x
Next i
Reverse = x
End Function
Function BinToByte(m As String) As Byte ' 将八位二进制串转换成十进制
Dim x As String * 1, y%, z%
z = 0
For i = 1 To 8
x = Mid(m, i, 1)
y = x * 2 ^ (8 - i)
z = z + y
Next i
BinToByte = z
End Function
Private Sub Command1_Click()
Dim x As Byte, i%, fname$
fname = InputBox("请输入要加密的文件名!注意加上路径名:")
If Dir(fname) = "" Then
MsgBox "文件不存在!"
Exit Sub
End If
Open fname For Binary As #1 ' 以二进制访问模式打开待加密文件
For i = 1 To LOF(1) ' LOF函数是求文件长度的内部函数
Get #1, i, x ' 取出第i个字节
x = BinToByte(Reverse(ByteToBin(x))) ' 这里调用了三个自定义函数
Put #1, i, x ' 将加密后的这个字节写回到文件原位置
Next i
Close
MsgBox "完成!"
End Sub
退出:Unload Me
网上帮你找的,如果不符合你的要求,你可以再去网上在搜搜,有很多代码的。