2012-02-02 124 views
0

下面是一些代码,我有一个完美的作品:加密密钥大小和算法

Sub EncryptFile(ByVal sInputFilename As String, _ 
       ByVal sOutputFilename As String, _ 
       ByVal sKey As String) 

    Dim fsInput As New FileStream(sInputFilename, _ 
           FileMode.Open, FileAccess.Read) 
    Dim fsEncrypted As New FileStream(sOutputFilename, _ 
           FileMode.Create, FileAccess.Write) 

    Dim DES As New DESCryptoServiceProvider() 

    'Set secret key for DES algorithm. 
    'A 64-bit key and an IV are required for this provider. 
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey) 

    'Set the initialization vector. 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 

    'Create the DES encryptor from this instance. 
    Dim desencrypt As ICryptoTransform = DES.CreateEncryptor() 
    'Create the crypto stream that transforms the file stream by using DES encryption. 
    Dim cryptostream As New CryptoStream(fsEncrypted, _ 
             desencrypt, _ 
             CryptoStreamMode.Write) 

    'Read the file text to the byte array. 
    Dim bytearrayinput(fsInput.Length - 1) As Byte 
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length) 
    'Write out the DES encrypted file. 
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Close() 

末次

是否有可能改变密钥长度,甚至MD5和SHA1加密之间选择使用此代码?如果没有,有人能指出我正确的方向来找到一些这样做吗?

感谢

西蒙

回答

1

DES是一种加密算法。如果你想使用别的东西,你应该看看TripleDESCryptoServiceProvider或者AesCryptoServiceProvider(在System.Security.Cryptography命名空间中)。

MD5和SHA1实际上是哈希算法。实际上,它们是特殊情况下的单向加密算法,不能解密(所以我不认为它们就是你要找的)。

只看了TripleDes的和AES类的文档,它看起来像你应该能够取代线:任何提供了一个CreateEncryptor功能的其他CryptoServiceProvider类

Dim DES As New DESCryptoServiceProvider() 

。它们还支持您可以设置的KeySize属性。你可以尝试这样的:

Sub EncryptFile(ByVal sInputFilename As String, _ 
      ByVal sOutputFilename As String, _ 
      ByVal sKey As String, _ 
      ByVal keysize as integer, _ 
      ByVal algorithm as String) 

    Dim fsInput As New FileStream(sInputFilename, _ 
          FileMode.Open, FileAccess.Read) 
    Dim fsEncrypted As New FileStream(sOutputFilename, _ 
          FileMode.Create, FileAccess.Write) 

    Dim algorithm As SymmetricAlgorithm 
    Select Case algorithm 
     Case "DES": algorithm = New DESCryptoServiceProvider() 
     Case "3DES": algorithm = New TripleDESCryptoServiceProvider() 
     Case "AES": algorithm = New AESCryptoServiceProvider() 
    End Select 
    algorithm.KeySize = keysize 

    'Set secret key for the algorithm. 
    'A 64-bit key and an IV are required for this provider. 
    algorithm.Key = ASCIIEncoding.ASCII.GetBytes(sKey) 

    'Set the initialization vector. 
    algorithm.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 

    'Create the encryptor from this instance. 
    Dim desencrypt As ICryptoTransform = algorithm.CreateEncryptor() 
    'Create the crypto stream that transforms the file stream by using encryption. 
    Dim cryptostream As New CryptoStream(fsEncrypted, _ 
            desencrypt, _ 
            CryptoStreamMode.Write) 

    'Read the file text to the byte array. 
    Dim bytearrayinput(fsInput.Length - 1) As Byte 
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length) 
    'Write out the DES encrypted file. 
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Close() 
End Sub 

我没试过编译上面的示例,但你让你开始希望。