a few changes to last PR (aes methods) - all still untested

This commit is contained in:
UbitUmarov
2024-02-24 14:50:28 +00:00
parent 662eac32a2
commit 121f270414
2 changed files with 42 additions and 43 deletions

View File

@@ -1388,12 +1388,14 @@ namespace OpenSim.Framework
/// ID...</param>
/// <returns>A string composed by the Initialization Vector bytes and the
/// encrypted text bytes converted to lower case HexString and separated by " : " </returns>
private static string AESEncryptString(string secret, string plainText, string ivString= null)
private static string AESEncryptString(string secret, string plainText, string ivString = null)
{
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(plainText))
return string.Empty;
return string.Empty;
byte[] iv = string.IsNullOrEmpty(ivString) ? MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(UUID.Random().ToString())) : MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(ivString));
byte[] iv = string.IsNullOrEmpty(ivString) ?
MD5.Create().ComputeHash(UUID.Random().GetBytes()) :
MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(ivString));
byte[] aesKey = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(secret));
byte[] encryptedText;
@@ -1407,18 +1409,13 @@ namespace OpenSim.Framework
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using MemoryStream memoryStream = new();
using CryptoStream cryptoStream = new(memoryStream, encryptor, CryptoStreamMode.Write);
using (StreamWriter streamWriter = new(cryptoStream))
{
streamWriter.Write(plainText);
streamWriter.Close();
}
using StreamWriter streamWriter = new(cryptoStream);
streamWriter.Write(plainText);
encryptedText = memoryStream.ToArray();
memoryStream.Dispose();
cryptoStream.Dispose();
}
return string.Format("{0}:{1}", Convert.ToHexString(iv), Convert.ToHexString(encryptedText)).ToLower();
return $"{Convert.ToHexString(iv)}:{Convert.ToHexString(encryptedText).ToLower()}";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -1443,17 +1440,26 @@ namespace OpenSim.Framework
/// if used in the encription. eg; an avatarID, a SecureSessionID, an object or
/// script ID...</param>
/// <returns>The decrypted string.</returns>
private static string AESDecryptString(string secret, string encryptedText, string ivString= null)
private static string AESDecryptString(string secret, string encryptedText, string ivString = null)
{
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(encryptedText))
return string.Empty;
return string.Empty;
string[] encodedParts = encryptedText.Split(":");
if(encodedParts.Length < 2 || encodedParts[1].Length == 0)
return string.Empty;
byte[] iv;
byte[] buffer;
try{
iv = string.IsNullOrEmpty(ivString) ? Convert.FromHexString(encryptedText.Split(":")[0].ToUpper()) : MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(ivString));
buffer = Convert.FromHexString(encryptedText.Split(":")[1].ToUpper());
}catch
try
{
iv = string.IsNullOrEmpty(ivString) ?
Convert.FromHexString(encodedParts[0]) :
MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(ivString));
buffer = Convert.FromHexString(encodedParts[1]);
}
catch
{
return string.Empty;
}
@@ -1471,11 +1477,8 @@ namespace OpenSim.Framework
using CryptoStream cryptoStream = new(memoryStream, decryptor, CryptoStreamMode.Read);
using StreamReader streamReader = new(cryptoStream);
string ret = streamReader.ReadToEnd();
memoryStream.Dispose();
cryptoStream.Dispose();
streamReader.Close();
return ret;
//string ret = streamReader.ReadToEnd();
return streamReader.ReadToEnd();
}
public static int fast_distance2d(int x, int y)

View File

@@ -6380,14 +6380,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(plainText))
return LSL_String.Empty;
LSL_String ret = Util.AESEncrypt(secret, plainText);
if(string.IsNullOrEmpty(ret)){
string ret = Util.AESEncrypt(secret, plainText);
if(string.IsNullOrEmpty(ret))
{
OSSLShoutError("osAESEncrypt: Failed to encrypt!");
return LSL_String.Empty;
}else
{
return ret;
}
return ret;
}
public LSL_String osAESDecrypt(string secret, string encryptedText)
@@ -6395,14 +6394,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(encryptedText))
return LSL_String.Empty;
LSL_String ret = Util.AESDecrypt(secret, encryptedText);
if(string.IsNullOrEmpty(ret)){
string ret = Util.AESDecrypt(secret, encryptedText);
if(string.IsNullOrEmpty(ret))
{
OSSLShoutError("osAESDecrypt: Failed to Decrypt!");
return LSL_String.Empty;
}else
{
return ret;
}
return ret;
}
public LSL_String osAESEncryptTo(string secret, string plainText, string ivString)
@@ -6410,14 +6408,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(plainText) || string.IsNullOrEmpty(ivString))
return LSL_String.Empty;
LSL_String ret = Util.AESEncryptTo(secret, plainText, ivString);
if(string.IsNullOrEmpty(ret)){
string ret = Util.AESEncryptTo(secret, plainText, ivString);
if(string.IsNullOrEmpty(ret))
{
OSSLShoutError("osAESEncryptTo: Failed to encrypt!");
return LSL_String.Empty;
}else
{
return ret;
}
return ret;
}
public LSL_String osAESDecryptFrom(string secret, string encryptedText, string ivString)
@@ -6425,14 +6422,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(encryptedText) || string.IsNullOrEmpty(ivString))
return LSL_String.Empty;
LSL_String ret = Util.AESDecryptFrom(secret, encryptedText, ivString);
if(string.IsNullOrEmpty(ret)){
string ret = Util.AESDecryptFrom(secret, encryptedText, ivString);
if(string.IsNullOrEmpty(ret))
{
OSSLShoutError("osAESDecryptFrom: Failed to decrypt!");
return LSL_String.Empty;
}else
{
return ret;
}
return ret;
}
}
}