c# – Rijndael:相同的字符串,不同的结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Rijndael:相同的字符串,不同的结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3124字,纯文字阅读大概需要5分钟。
内容图文
![c# – Rijndael:相同的字符串,不同的结果](/upload/InfoBanner/zyjiaocheng/815/8cd9f335c1df45e3a3c578ba59426abc.jpg)
我们有一个小型桌面应用程序,现在需要作为Web功能提供(.Net).这个应用程序包含一些加密代码,并使用.Net框架中的Rijndael类.代码接受输入字符串,对其进行加密并将结果写入文件.由于所有代码都包含在一个类中,我只是将该类复制到我的Web服务应用程序中.当我在原始应用程序和新应用程序中使用相同的密钥加密相同的字符串时,结果会有所不同.
原始应用程序给出的结果字符串是我的Web服务给出的结果字符串的子集.后者在加密字符串的末尾有其他字符.
以下是我正在使用的代码.请注意,我没有开发此代码,也不完全理解它.有关行为差异的任何想法?请帮忙!!
以下是获取用户输入并调用加密器的代码.
public void EncryptDomain(string EncryptValue, string outputDomainFile)
{
if (EncryptValue.Length > 0)
{
if ((outputDomainFile != null) && (outputDomainFile.Length > 0))
{
_outputDomainFile = outputDomainFile;
}
byte[] input = Encoding.UTF8.GetBytes(EncryptValue);
Transform(input, TransformType.ENCRYPT);
}
这是加密器代码:
private byte[] Transform(byte[] input, TransformType transformType)
{
CryptoStream cryptoStream = null; // Stream used to encrypt
RijndaelManaged rijndael = null; // Rijndael provider
ICryptoTransform rijndaelTransform = null;// Encrypting object
FileStream fsIn = null; //input file
FileStream fsOut = null; //output file
MemoryStream memStream = null; // Stream to contain data
try
{
// Create the crypto objects
rijndael = new RijndaelManaged();
rijndael.Key = this._Key;
rijndael.IV = this._IV;
rijndael.Padding = PaddingMode.Zeros;
if (transformType == TransformType.ENCRYPT)
{
rijndaelTransform = rijndael.CreateEncryptor();
}
else
{
rijndaelTransform = rijndael.CreateDecryptor();
}
if ((input != null) && (input.Length > 0))
{
//memStream = new MemoryStream();
//string outputDomainFile =
FileStream fsOutDomain = new FileStream(_outputDomainFile,
FileMode.OpenOrCreate, FileAccess.Write);
cryptoStream = new CryptoStream(
fsOutDomain, rijndaelTransform, CryptoStreamMode.Write);
cryptoStream.Write(input, 0, input.Length);
cryptoStream.FlushFinalBlock();
//return memStream.ToArray();
return null;
}
return null;
}
catch (CryptographicException)
{
throw new CryptographicException("Password is invalid. Please verify once again.");
}
finally
{
if (rijndael != null) rijndael.Clear();
if (rijndaelTransform != null) rijndaelTransform.Dispose();
if (cryptoStream != null) cryptoStream.Close();
if (memStream != null) memStream.Close();
if (fsOut != null) fsOut.Close();
if (fsIn != null) fsIn.Close();
}
}
设置IV值的代码:
private void GenerateKey(string SecretPhrase)
{
// Initialize internal values
this._Key = new byte[24];
this._IV = new byte[16];
// Perform a hash operation using the phrase. This will
// generate a unique 32 character value to be used as the key.
byte[] bytePhrase = Encoding.ASCII.GetBytes(SecretPhrase);
SHA384Managed sha384 = new SHA384Managed();
sha384.ComputeHash(bytePhrase);
byte[] result = sha384.Hash;
// Transfer the first 24 characters of the hashed value to the key
// and the remaining 8 characters to the intialization vector.
for (int loop = 0; loop < 24; loop++) this._Key[loop] = result[loop];
for (int loop = 24; loop < 40; loop++) this._IV[loop - 24] = result[loop];
}
解决方法:
我猜这是因为IV (Initialisation Vector)
内容总结
以上是互联网集市为您收集整理的c# – Rijndael:相同的字符串,不同的结果全部内容,希望文章能够帮你解决c# – Rijndael:相同的字符串,不同的结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。