gpt4 book ai didi

c# - Rijndael : same string, 不同的结果

转载 作者:太空狗 更新时间:2023-10-30 00:56:44 25 4
gpt4 key购买 nike

我们有一个小型桌面应用程序,现在需要作为网络功能 (.Net) 提供。此应用程序包含一些加密代码,并使用 .Net 框架中的 Rijndael 类。该代码接受一个输入字符串,对其进行加密并将结果写入文件。由于所有代码都包含在一个类中,我只是将该类复制到我的 Web 服务应用程序中。当我在原始应用程序和新应用程序中使用相同的 key 加密相同的字符串时,结果是不同的。原始应用程序给出的结果字符串是我的网络服务给出的结果字符串的子集。后者在加密字符串的末尾有额外的字符。

下面是我使用的代码。请注意,我没有开发此代码,也没有完全理解它。关于行为差异的任何想法?请帮忙!!

这是获取用户输入并调用加密器的代码。

    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 : same string, 不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com