狐爸
下面是我在微软的Libary 中看到的关于AES类库的示例文件:
我在项目里已经加了System.Security.Cryptography 的命名空间,现在我想把它的加密过程写到项目里,我是直接COPY的,代码如下:
Public Function EncryptStringToBytes_Aes(ByVal plainText As
String, ByVal Key() As
Byte, ByVal IV() As
Byte) As
Byte()
' Check arguments.
If plainText Is
Nothing
OrElse plainText.Length <= 0 Then
Throw
New ArgumentNullException("plainText")
End
If
If Key Is
Nothing
OrElse Key.Length <= 0 Then
Throw
New ArgumentNullException("Key")
End
If
If IV Is
Nothing
OrElse IV.Length <= 0 Then
Throw
New ArgumentNullException("IV")
End
If
Dim encrypted() As
Byte
' Create an Aes object
' with the specified key and IV.
Using aesAlg As Aes = Aes.Create()
aesAlg.Key = Key
aesAlg.IV = IV
' Create a decrytor to perform the stream transform.
Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
' Create the streams used for encryption.
Using msEncrypt As
New MemoryStream()
Using csEncrypt As
New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
Using swEncrypt As
New StreamWriter(csEncrypt)
'Write all data to the stream.
swEncrypt.Write(plainText)
End
Using
encrypted = msEncrypt.ToArray()
End
Using
End
Using
End
Using
' Return the encrypted bytes from the memory stream.
Return encrypted
End
Function
'EncryptStringToBytes_Aes
但是会提示出错,说没有定义AES的类?这种情况要怎么处理?