解密问题 [英] Decrypt problem
本文介绍了解密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我遇到了加密和解密的问题,当我加密密码时它做得很好但是当我尝试解密时我得到错误信息" ;要解密的数据长度无效"
这是加密密码的函数:
公共函数加密(str As String )As String
Dim EncrptKey As String =" 2013; [pnuLIT] WebCodeExpert"
Dim byKey As Byte()= {}
Dim IV As Byte()= {18,52,86,120,144,171,_
205,239}
byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0,8))
Dim des As New DESCryptoServiceProvider()
Dim inputByteArray As Byte()= Encoding.UTF8.GetBytes(str )
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms,des.CreateEncryptor(byKey,IV),CryptoStreamMode.Write)
cs.Write(inputByteArray,0,inputByteArray .Length)
cs.FlushFinalBlock()
返回Convert.ToBase64String(ms.ToArray())
结束函数
这是我用来解密的代码,但是当我得到上面提到的错误时。
公共函数Decrypt(str As String)As String
str = str.Replace(""," +")
Dim DecryptKey As String =" 2013; [pnuLIT)WebCodeExpert"
Dim byKey As Byte()= {}
Dim IV As Byte()= {18,52,86,120,144,171,_
205,239}
Dim inputByteArray As Byte()= New Byte(str.Length - 1){}
byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0,8))
Dim des As New DESCryptoServiceProvider()
inputByteArray = Convert.FromBase64String(str.Replace(""," +"))
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms,des.CreateDecryptor(byKey,IV),CryptoStreamMode.Write)
cs.Write(inputByteArray,0,inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
返回encoding.GetString(ms.ToArray())
结束函数
有没有人知道到底发生了什么?任何帮助将非常感谢。
Ivannovish
解决方案
Ivan,
这是一个更好的选择:
Hello all,
I have an issue with encyption and decryption, when I encrypt a password it does it very well but when I try to decrypt I get error message "Length of the data to decrypt is invalid"
This is the function to encrypt the password: Public Function Encrypt(str As String) As String Dim EncrptKey As String = "2013;[pnuLIT)WebCodeExpert" Dim byKey As Byte() = {} Dim IV As Byte() = {18, 52, 86, 120, 144, 171, _ 205, 239} byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8)) Dim des As New DESCryptoServiceProvider() Dim inputByteArray As Byte() = Encoding.UTF8.GetBytes(str) Dim ms As New MemoryStream() Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock() Return Convert.ToBase64String(ms.ToArray()) End Function
This is the code that I´m using to decrypt but it is when I get the error mentioned above. Public Function Decrypt(str As String) As String str = str.Replace(" ", "+") Dim DecryptKey As String = "2013;[pnuLIT)WebCodeExpert" Dim byKey As Byte() = {} Dim IV As Byte() = {18, 52, 86, 120, 144, 171, _ 205, 239} Dim inputByteArray As Byte() = New Byte(str.Length - 1) {} byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8)) Dim des As New DESCryptoServiceProvider() inputByteArray = Convert.FromBase64String(str.Replace(" ", "+")) Dim ms As New MemoryStream() Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write) cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock() Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8 Return encoding.GetString(ms.ToArray()) End Function
Does anybody have any idea what exactly is going on? Any help will be very much appreciated.
Ivannovish
解决方案
Ivan,
This is a better alternative:
这篇关于解密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文