本文介绍了在DES算法中加密数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 朋友,我首先是以字符串形式提供文本数据&然后我将它转移到加密功能,使用DES算法加密字符串它通过incrypting给我正确的字符串,但在下一步我写的字符串到波形文件意味着我在波形文件中隐藏字符串但在隐藏字符串时wave文件它不是隐藏整个字符串它只是隐藏了前11个字符,加密字符串中有16个字符。所以为什么它不是隐藏整个字符串。 但是如果我隐藏了一件事通过硬编码方式相同的加密字符串意味着代替调用加密函数我直接将一些值分配给字符串可验证,在这种情况下它工作正常&隐藏所有字符串没有任何问题。我已经附加了我的两个功能 ENCRYPT功能&隐藏数据功能请查看它&帮帮我谢谢 private void btnHide_Click( object sender,EventArgs e) { BinaryWriter messageWriter = new BinaryWriter( new MemoryStream()); 流sourceStream = null ; FileStream destinationStream = null ; WaveStream audioStream = null ; // 创建一个包含消息的流,前面是其长度 messageWriter.Write(txtMessage.Text.Length); string encryptDatas = txtMessage.Text; // 加密(txtMessage.Text); messageWriter.Write(Encoding.ASCII.GetBytes (encryptDatas)); messageWriter.Seek( 0 ,SeekOrigin.Begin); 流messageStream = messageWriter.BaseStream; // 打开密钥文件 流keyStream = new FileStream(txtKeyFile.Text,FileMode.Open); 尝试 { // 我们需要的样本如何? long countSamplesRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length); if (countSamplesRequired > Int32 .MaxValue) { throw new 例外( 消息太长或坏密钥!此消息/密钥组合需要 + countSamplesRequired + samples,仅 + Int32 .MaxValue + 允许采样。); } // 使用.wav文件作为载体 sourceStream = new FileStream(txtSrcFile.Text,FileMode.Open); this .Cursor = Cursors.WaitCursor; // 为载波创建一个空文件 destinationStream = new FileStream(txtDstFile.Text,FileMode.Create); // 复制承运人档案的标题 audioStream = new WaveStream(sourceStream,destinationStream); if (audioStream.Length < = 0 ) { throw new 异常( 无效的WAV文件); } // 载波中是否有足够的样本? if (countSamplesRequired > audioStream.CountSamples) { 字符串 errorReport = 运营商文件太小对于此消息和密钥!\\\ n \\ n + 可提供的样本: + audioStream.CountSamples + \\\\ nn + 需要的样本: + countSamplesRequired; throw new 异常(errorReport); } // 隐藏消息 WaveUtility utility = new WaveUtility(audioStream,destinationStream); utility.Hide(messageStream,keyStream); MessageBox.Show( 数据被隐藏); } catch (例外情况) { this .Cursor = Cursors.Default; MessageBox.Show(ex.Message); } 最后 { 如果(keyStream != null ){keyStream.Close(); } if (messageStream!= null ){messageStream.Close(); } if (audioStream!= null ){audioStream.Close(); } if (sourceStream!= null ){sourceStream.Close(); } if (destinationStream!= null ){destinationStream.Close(); } this .Cursor = Cursors.Default; } } public string Encrypt( string originalString) { byte [] bytes = ASCIIEncoding .ASCII.GetBytes( ZeroCool); if ( String .IsNullOrEmpty(originalString)) { throw new ArgumentNullException ( 需要加密的字符串不能为空。); } DESCryptoServiceProvider cryptoProvid = new DESCryptoServiceProvider(); MemoryStream memoryStre = new MemoryStream(); CryptoStream cryptoStre = new CryptoStream(memoryStre, cryptoProvid.CreateEncryptor(bytes,bytes),CryptoStreamMode.Write); StreamWriter writer = new StreamWriter(cryptoStre); writer.Write(originalString); writer.Flush(); cryptoStre.FlushFinalBlock(); writer.Flush(); byte [] bb = memoryStre.GetBuffer(); int l =( int )memoryStre.Length; writer.Close(); writer.Dispose(); cryptoProvid.Clear(); cryptoProvid.Dispose(); memoryStre.Close(); memoryStre.Dispose(); cryptoStre.Clear(); cryptoStre.Close(); cryptoStre.Dispose(); writer.Close(); writer.Dispose(); return Convert.ToBase64String(bb, 0 ,l); } 没有解密我只是显示什么字符串被加密,我还有解密功能但是只有当我给出加密字符串时才会出现问题通过硬编码相同的加密字符串它给了我正确的输出但是当我调用加密方法那些它发送正确的字符串加密但是当写入它时不写波形文件中的整个刺。所以请帮助我。谢谢。 解决方案 我认为这是因为你从txtMessage文本框计算长度: messageWriter.Write (txtMessage.Text.Length) 你应该在调用后得到的字符串中计算它: 加密(txtMessage.Text) Hi friend I first I am taking an text data in string veriable & then i am transfering it to Encrypt function which encrypts string using DES algo it is giving me correct string by incrypting but in next step i am writing that string to wave file means i am hiding string in wave file but at the time of hiding string in wave file it si not hiding whole string it is just hiding first 11 charactes those there are 16 charcter in the encrypted string.so why it is not hiding whole string.but one thing is there if i hide the same encrypted string by hardcoded way means instead of calling to encrypt function i am directly assigning some value to string veriable in that case it is working fine & hiding all the string without any problem.i have attached my both functionENCRYPT function & Hide data function here please review it & help me thank you private void btnHide_Click(object sender, EventArgs e) { BinaryWriter messageWriter = new BinaryWriter(new MemoryStream()); Stream sourceStream = null; FileStream destinationStream = null; WaveStream audioStream = null; //create a stream that contains the message, preceeded by its length messageWriter.Write(txtMessage.Text.Length); string encryptDatas =txtMessage.Text; //Encrypt(txtMessage.Text); messageWriter.Write(Encoding.ASCII.GetBytes(encryptDatas)); messageWriter.Seek(0, SeekOrigin.Begin); Stream messageStream = messageWriter.BaseStream; //open the key file Stream keyStream = new FileStream(txtKeyFile.Text, FileMode.Open); try { //how man samples do we need? long countSamplesRequired = WaveUtility.CheckKeyForMessage(keyStream, messageStream.Length); if (countSamplesRequired > Int32.MaxValue) { throw new Exception("Message too long, or bad key! This message/key combination requires " + countSamplesRequired + " samples, only " + Int32.MaxValue + " samples are allowed."); } //use a .wav file as the carrier sourceStream = new FileStream(txtSrcFile.Text, FileMode.Open); this.Cursor = Cursors.WaitCursor; //create an empty file for the carrier wave destinationStream = new FileStream(txtDstFile.Text, FileMode.Create); //copy the carrier file's header audioStream = new WaveStream(sourceStream, destinationStream); if (audioStream.Length <= 0) { throw new Exception("Invalid WAV file"); } //are there enough samples in the carrier wave? if (countSamplesRequired > audioStream.CountSamples) { String errorReport = "The carrier file is too small for this message and key!\r\n" + "Samples available: " + audioStream.CountSamples + "\r\n" + "Samples needed: " + countSamplesRequired; throw new Exception(errorReport); } //hide the message WaveUtility utility = new WaveUtility(audioStream, destinationStream); utility.Hide(messageStream, keyStream); MessageBox.Show("data is hided"); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message); } finally { if (keyStream != null) { keyStream.Close(); } if (messageStream != null) { messageStream.Close(); } if (audioStream != null) { audioStream.Close(); } if (sourceStream != null) { sourceStream.Close(); } if (destinationStream != null) { destinationStream.Close(); } this.Cursor = Cursors.Default; } }public string Encrypt(string originalString) { byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool"); if (String.IsNullOrEmpty(originalString)) { throw new ArgumentNullException ("The string which needs to be encrypted can not be null."); } DESCryptoServiceProvider cryptoProvid = new DESCryptoServiceProvider(); MemoryStream memoryStre = new MemoryStream(); CryptoStream cryptoStre = new CryptoStream(memoryStre, cryptoProvid.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write); StreamWriter writer = new StreamWriter(cryptoStre); writer.Write(originalString); writer.Flush(); cryptoStre.FlushFinalBlock(); writer.Flush(); byte[] bb = memoryStre.GetBuffer(); int l=(int)memoryStre.Length; writer.Close(); writer.Dispose(); cryptoProvid.Clear(); cryptoProvid.Dispose(); memoryStre.Close(); memoryStre.Dispose(); cryptoStre.Clear(); cryptoStre.Close(); cryptoStre.Dispose(); writer.Close(); writer.Dispose(); return Convert.ToBase64String(bb, 0,l ); }without decrypting i am just showing what string is get encrypted , I have function to decrypt also but there is problem in writeing encrypted string only when i give same encrypted string by hard coding it gives me correct out put but when i call to encrypt method those it send correct string encrypting but also when goes to write it does not write whole sting in wave file.so please help me on it .Thanks. 解决方案 I think it is because you calculate the length from the txtMessage textbox:messageWriter.Write(txtMessage.Text.Length)You should calculate it from the string you get after calling the:Encrypt(txtMessage.Text) 这篇关于在DES算法中加密数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 15:33