加密密钥和算法

时间:2012-02-02 01:56:19

标签: vb.net

以下是一些完美的代码:

   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()

End Sub

是否可以更改密钥大小,甚至可以在此代码中选择MD5和SHA1加密?如果没有,有人能指出我正确的方向找到一些吗?

感谢

西蒙

1 个答案:

答案 0 :(得分:1)

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

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

只要查看TripleDES和Aes类的文档,您就应该能够替换该行:

Dim DES As New DESCryptoServiceProvider()

使用提供CreateEncryptor函数的任何其他CryptoServiceProvider类。它们还支持您可以设置的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

我没有尝试编译上面的示例,但是我希望你能开始。