本文介绍了TripleDES加密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试加密某些文本,并能够在稍后的时间内解密它。我有两种方法可以做到这一点: public static Byte [] EncryptText(string textToEncrypt,string encryptionHash) { Byte [] bytearrayinput = StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt); // DES实例 系统。 Security.Cryptography.TripleDESCryptoServic eProvider des = new TripleDESCryptoServiceProvider(); //使用默认的SHA-1哈希算法 string pws = encryptionHash; System.Security.Cryptography.PasswordDeriveBytes db = new System.Security.Cryptography.PasswordDeriveBytes(p ws,new byte [0]); byte [] prndKey = db.GetBytes(16); byte [] IV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10, 0x11,0x12,0x13,0x14,0x15,0x16}; //来自MS中的示例 文档。 System.IO.MemoryStream ms = new System.IO.MemoryStream(); / /创建使用Triple DES转换文本流的加密流 加密 CryptoStream cryptostream = new CryptoStream(ms,des.CreateEncryptor(prndKey, IV),Cr yptoStreamMode.Write); cryptostream.Write(bytearrayinput,0,bytearrayinput .Length); System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptostream); sw.Write(bytearrayinput); Byte [] mBytes = new Byte [ms.Length-1]; ms.Position = 0; ms。读取(mBytes,0,mBytes.Length); cryptostream.Close(); ms.Close(); 返回mBytes; } 公共静态字符串DeCryptText(Byte [] textToDecrypt,string encryptionHash) { Byte [] bytearrayinput = textToDecrypt; // DES实例 System.Security.Cryptograph y.TripleDESCryptoServic eProvider des = new TripleDESCryptoServiceProvider(); string pws = encryptionHash; System.Security.Cryptography.PasswordDeriveBytes db = new System.Security.Cryptography.PasswordDeriveBytes(p ws,new byte [0]); byte [] prndKey = db.GetBytes(16); byte [] IV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10, 0x11,0x12,0x13,0x14,0x15,0x16} ; System.IO.MemoryStream ms = new System.IO.MemoryStream(bytearrayinput); //创建使用Triple DES转换文本流的加密流 加密 CryptoStream cryptostream = new CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read); System.IO.StreamReader SR = new System.IO.StreamReader(cryptostream,System.Text.En coding.ASCII); 返回SR.ReadToEnd() ; } 我已经尝试了大约五百种机智同样的结果: 它窒息了 SR.ReadToEnd();当触及解密由加密的数据时 EncryptText(..) 要解密的数据长度无效。 描述:在执行 当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取更多信息 有关错误及其源自代码的位置。 异常详细信息:System.Security.Cryptography.CryptographicExceptio n : 要解密的数据长度无效。 有人可以解释发生了什么以及我做错了什么。在查看 以了解这一点时,我看到了很多新闻组帖子,其中人们有同样的问题,但没有答案。 谢谢, CMDI am attempting to encrypt some text and be able to decrypt it at a latertime. I have two methods to do this:public static Byte[] EncryptText(string textToEncrypt, stringencryptionHash){Byte[] bytearrayinput =StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt);//DES instanceSystem.Security.Cryptography.TripleDESCryptoServic eProvider des = newTripleDESCryptoServiceProvider();// use the default SHA-1 hash algorithmstring pws = encryptionHash;System.Security.Cryptography.PasswordDeriveBytes db = newSystem.Security.Cryptography.PasswordDeriveBytes(p ws,new byte[0]);byte[] prndKey= db.GetBytes(16);byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in MSdocumentation.System.IO.MemoryStream ms = new System.IO.MemoryStream();//Create Crypto Stream that transforms text stream using Triple DESencryptionCryptoStream cryptostream = newCryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write);cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptostream);sw.Write(bytearrayinput);Byte[] mBytes = new Byte[ms.Length-1];ms.Position = 0;ms.Read(mBytes,0,mBytes.Length);cryptostream.Close();ms.Close();return mBytes;}public static string DeCryptText(Byte[] textToDecrypt, stringencryptionHash){Byte[] bytearrayinput = textToDecrypt;//DES instanceSystem.Security.Cryptography.TripleDESCryptoServic eProvider des = newTripleDESCryptoServiceProvider();string pws = encryptionHash;System.Security.Cryptography.PasswordDeriveBytes db = newSystem.Security.Cryptography.PasswordDeriveBytes(p ws,new byte[0]);byte[] prndKey= db.GetBytes(16);byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,0x11, 0x12, 0x13, 0x14, 0x15, 0x16};System.IO.MemoryStream ms = new System.IO.MemoryStream(bytearrayinput);//Create Crypto Stream that transforms text stream using Triple DESencryptionCryptoStream cryptostream = newCryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read);System.IO.StreamReader SR = newSystem.IO.StreamReader(cryptostream,System.Text.En coding.ASCII);return SR.ReadToEnd();}I have tried this about half a hundred ways with the same results :It chokes onSR.ReadToEnd(); when atttepting to Decrypt the data that was encrypted byEncryptText(..)Length of the data to decrypt is invalid.Description: An unhandled exception occurred during the execution of thecurrent web request. Please review the stack trace for more informationabout the error and where it originated in the code.Exception Details: System.Security.Cryptography.CryptographicExceptio n:Length of the data to decrypt is invalid.Can someone explain what is going on and what I am doing wrong. In lookingfor insight into this I have seen allot of newsgroup posts where people hadthe same problem but no answers.Thanks,CMD推荐答案 0x10, 0x10, 0x10, 0x10, System.IO 。我moryStream(bytearrayinput); System.IO.MemoryStream(bytearrayinput); 中查看 有 UnicodeEncoding UnicodeEncoding 洞察 C#中所有 C# all 工作 有点。 System.Text.ASCIIEncoding.ASCII.GetString(encrypte d); System.Text.ASCIIEncoding.ASCII.GetString(encrypte d); System.Text。 ASCIIEncoding.ASCII.GetBytes(加密的Str); System.Text.ASCIIEncoding.ASCII.GetBytes(encrypted Str); NFS.Architecture.Security.TextEncryption.DesDecryp t(reencrypted," passw ord"); NFS.Architecture.Security.TextEncryption.DesDecryp t(reencrypted,"passw ord");将 at a 时间。我有两种方法可以做到这一点:> > public static Byte [] EncryptText(string textToEncrypt,string > encryptionHash)> {> Byte [] bytearrayinput = > StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt); > // DES实例> System.Security.Cryptography.TripleDESCryptoServic eProvider des = StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt); > //DES instance > System.Security.Cryptography.TripleDESCryptoServic eProvider des = byte [0]); > byte [] prndKey = db.GetBytes(16); > byte [] IV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,> 0x11,0x12,0x13,0x14,0x15,0x16}; //来自MS的例子>文档。> System.IO.MemoryStream ms = new System.IO.MemoryStream(); > //创建使用Triple DES转换文本流的加密流>加密> CryptoStream cryptostream = new > CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write ); > cryptostream.Write(bytearrayinput,0,bytearrayinput .Length); > > System.IO.StreamWriter sw = new byte[0]); > byte[] prndKey= db.GetBytes(16); > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in MS > documentation. > System.IO.MemoryStream ms = new System.IO.MemoryStream(); > //Create Crypto Stream that transforms text stream using Triple DES > encryption > CryptoStream cryptostream = new > CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write ); > cryptostream.Write(bytearrayinput,0,bytearrayinput .Length); > > System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptostream); System.IO.StreamWriter(cryptostream); byte [0]); > byte [] prndKey = db.GetBytes(16); > byte [] IV = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,> 0x11,0x12,0x13,0x14,0x15,0x16}; > System.IO.MemoryStream ms = new System.IO.MemoryStream(bytearrayinput); > //创建使用Triple DES转换文本流的加密流>加密> CryptoStream cryptostream = new > CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read); > System.IO.StreamReader SR = new > System.IO.StreamReader(cryptostream,System.Text.En coding.ASCII); >返回SR.ReadToEnd(); > } > >我已经尝试了大约五百种具有相同结果的方法:>它在>上窒息SR.ReadToEnd();当通过 byte[0]); > byte[] prndKey= db.GetBytes(16); > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; > System.IO.MemoryStream ms = new System.IO.MemoryStream(bytearrayinput); > //Create Crypto Stream that transforms text stream using Triple DES > encryption > CryptoStream cryptostream = new > CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read) ; > System.IO.StreamReader SR = new > System.IO.StreamReader(cryptostream,System.Text.En coding.ASCII); > return SR.ReadToEnd(); > } > > I have tried this about half a hundred ways with the same results : > It chokes on > SR.ReadToEnd(); when atttepting to Decrypt the data that was encrypted 执行>期间发生未处理的异常当前的网络请求。请查看堆栈跟踪以获取更多信息>关于错误以及它在代码中的起源。> >例外细节:System.Security.Cryptography.CryptographicExceptio n:>要解密的数据长度无效。> > >有人可以解释发生了什么以及我做错了什么。在寻找>为了深入了解这一点,我已经看到人们有新闻组帖子分享了>同样的问题,但没有答案。> >谢谢,> CMD > > execution of the > current web request. Please review the stack trace for more information > about the error and where it originated in the code. > > Exception Details: System.Security.Cryptography.CryptographicExceptio n: > Length of the data to decrypt is invalid. > > > Can someone explain what is going on and what I am doing wrong. In looking > for insight into this I have seen allot of newsgroup posts where people had > the same problem but no answers. > > Thanks, > CMD > > 这篇关于TripleDES加密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 01:55