mirror of
https://github.com/opensim/opensim.git
synced 2026-08-09 18:55:58 +08:00
Merge pull request #16 from AdilElFarissi/master
Basic implementation of AES encryption/decryption and respective OSSL functions
This commit is contained in:
@@ -1367,6 +1367,117 @@ namespace OpenSim.Framework
|
|||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static string AESEncrypt(string secret, string plainText)
|
||||||
|
{
|
||||||
|
return AESEncryptString(secret, plainText, string.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static string AESEncryptTo(string secret, string plainText, string ivString)
|
||||||
|
{
|
||||||
|
return AESEncryptString(secret, plainText, ivString);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// AES Encrypt a string using a password and a random or custom Initialization
|
||||||
|
/// Vector
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="secret">The secret encryption password or key.</param>
|
||||||
|
/// <param name="plainText">The string or text to encrypt.</param>
|
||||||
|
/// <param name="ivString">(optional) A string used to generate the Initialization Vector eg; an avatarID, a SecureSessionID, an object or script
|
||||||
|
/// 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)
|
||||||
|
{
|
||||||
|
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(plainText))
|
||||||
|
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[] aesKey = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(secret));
|
||||||
|
byte[] encryptedText;
|
||||||
|
|
||||||
|
using (Aes aes = Aes.Create())
|
||||||
|
{
|
||||||
|
aes.Key = aesKey;
|
||||||
|
aes.IV = iv;
|
||||||
|
aes.Mode = CipherMode.CBC;
|
||||||
|
aes.Padding = PaddingMode.PKCS7;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
encryptedText = memoryStream.ToArray();
|
||||||
|
memoryStream.Dispose();
|
||||||
|
cryptoStream.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.Format("{0}:{1}", Convert.ToHexString(iv), Convert.ToHexString(encryptedText)).ToLower();
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static string AESDecrypt(string secret, string encryptedText)
|
||||||
|
{
|
||||||
|
return AESDecryptString(secret, encryptedText, string.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static string AESDecryptFrom(string secret, string encryptedText, string ivString)
|
||||||
|
{
|
||||||
|
return AESDecryptString(secret, encryptedText, ivString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AES Decrypt the string encrypted by AESEncryptString with the same password
|
||||||
|
/// and ivString used in the encryption.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="secret">The secret decryption password or key.</param>
|
||||||
|
/// <param name="encryptedText">The encrypted string or text.</param>
|
||||||
|
/// <param name="ivString">The string used to generate the Initialization Vector
|
||||||
|
/// 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)
|
||||||
|
{
|
||||||
|
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(encryptedText))
|
||||||
|
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
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] aesKey = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(secret));
|
||||||
|
|
||||||
|
using Aes aes = Aes.Create();
|
||||||
|
aes.Key = aesKey;
|
||||||
|
aes.IV = iv;
|
||||||
|
aes.Mode = CipherMode.CBC;
|
||||||
|
aes.Padding = PaddingMode.PKCS7;
|
||||||
|
|
||||||
|
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
|
||||||
|
using MemoryStream memoryStream = new(buffer);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
public static int fast_distance2d(int x, int y)
|
public static int fast_distance2d(int x, int y)
|
||||||
{
|
{
|
||||||
x = Math.Abs(x);
|
x = Math.Abs(x);
|
||||||
|
|||||||
@@ -6374,5 +6374,65 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
|
|||||||
|
|
||||||
return World.TryGetSceneObjectPart(id, out SceneObjectPart part) ? part.ParentGroup.GetSittingAvatarsCount() : 0;
|
return World.TryGetSceneObjectPart(id, out SceneObjectPart part) ? part.ParentGroup.GetSittingAvatarsCount() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LSL_String osAESEncrypt(string secret, string plainText)
|
||||||
|
{
|
||||||
|
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(plainText))
|
||||||
|
return LSL_String.Empty;
|
||||||
|
|
||||||
|
LSL_String ret = Util.AESEncrypt(secret, plainText);
|
||||||
|
if(string.IsNullOrEmpty(ret)){
|
||||||
|
OSSLShoutError("osAESEncrypt: Failed to encrypt!");
|
||||||
|
return LSL_String.Empty;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public LSL_String osAESDecrypt(string secret, string encryptedText)
|
||||||
|
{
|
||||||
|
if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(encryptedText))
|
||||||
|
return LSL_String.Empty;
|
||||||
|
|
||||||
|
LSL_String ret = Util.AESDecrypt(secret, encryptedText);
|
||||||
|
if(string.IsNullOrEmpty(ret)){
|
||||||
|
OSSLShoutError("osAESDecrypt: Failed to Decrypt!");
|
||||||
|
return LSL_String.Empty;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public LSL_String osAESEncryptTo(string secret, string plainText, string ivString)
|
||||||
|
{
|
||||||
|
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)){
|
||||||
|
OSSLShoutError("osAESEncryptTo: Failed to encrypt!");
|
||||||
|
return LSL_String.Empty;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public LSL_String osAESDecryptFrom(string secret, string encryptedText, string ivString)
|
||||||
|
{
|
||||||
|
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)){
|
||||||
|
OSSLShoutError("osAESDecryptFrom: Failed to decrypt!");
|
||||||
|
return LSL_String.Empty;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -617,5 +617,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Interfaces
|
|||||||
LSL_Integer osGetPrimCount(LSL_Key object_id);
|
LSL_Integer osGetPrimCount(LSL_Key object_id);
|
||||||
LSL_Integer osGetSittingAvatarsCount();
|
LSL_Integer osGetSittingAvatarsCount();
|
||||||
LSL_Integer osGetSittingAvatarsCount(LSL_Key object_id);
|
LSL_Integer osGetSittingAvatarsCount(LSL_Key object_id);
|
||||||
|
LSL_String osAESEncrypt(string secret, string plainText);
|
||||||
|
LSL_String osAESEncryptTo(string secret, string plainText, string ivString);
|
||||||
|
LSL_String osAESDecrypt(string secret, string encryptedText);
|
||||||
|
LSL_String osAESDecryptFrom(string secret, string encryptedText, string ivString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1750,5 +1750,29 @@ namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
|
|||||||
return m_OSSL_Functions.osGetSittingAvatarsCount(object_id);
|
return m_OSSL_Functions.osGetSittingAvatarsCount(object_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public LSL_String osAESEncrypt(string secret, string plainText)
|
||||||
|
{
|
||||||
|
return m_OSSL_Functions.osAESEncrypt(secret, plainText);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public LSL_String osAESEncryptTo(string secret, string plainText, string ivString)
|
||||||
|
{
|
||||||
|
return m_OSSL_Functions.osAESEncryptTo(secret, plainText, ivString);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public LSL_String osAESDecrypt(string secret, string encryptedText)
|
||||||
|
{
|
||||||
|
return m_OSSL_Functions.osAESDecrypt(secret, encryptedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public LSL_String osAESDecryptFrom(string secret, string encryptedText, string ivString)
|
||||||
|
{
|
||||||
|
return m_OSSL_Functions.osAESDecryptFrom(secret, encryptedText, ivString);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7347,6 +7347,13 @@ fc2639fd-5d16-66bf-d8ab-2e4d754c816d
|
|||||||
<map><key>itemId</key><map><key>type</key><string>key</string></map></map>
|
<map><key>itemId</key><map><key>type</key><string>key</string></map></map>
|
||||||
</array>
|
</array>
|
||||||
</map>
|
</map>
|
||||||
|
<key>osGetInventoryNames</key>
|
||||||
|
<map>
|
||||||
|
<key>return</key><string>list</string>
|
||||||
|
<key>arguments</key><array>
|
||||||
|
<map><key>type</key><map><key>type</key><string>integer</string></map></map>
|
||||||
|
</array>
|
||||||
|
</map>
|
||||||
<key>osGetLinkInventoryName</key>
|
<key>osGetLinkInventoryName</key>
|
||||||
<map>
|
<map>
|
||||||
<key>return</key><string>string</string>
|
<key>return</key><string>string</string>
|
||||||
@@ -8757,5 +8764,39 @@ fc2639fd-5d16-66bf-d8ab-2e4d754c816d
|
|||||||
<key>return</key><string>string</string>
|
<key>return</key><string>string</string>
|
||||||
<key>arguments</key><undef/>
|
<key>arguments</key><undef/>
|
||||||
</map>
|
</map>
|
||||||
|
<key>osAESEncrypt</key>
|
||||||
|
<map>
|
||||||
|
<key>return</key><string>string</string>
|
||||||
|
<key>arguments</key><array>
|
||||||
|
<map><key>secret</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
<map><key>plainText</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
</array>
|
||||||
|
</map>
|
||||||
|
<key>osAESEncryptTo</key>
|
||||||
|
<map>
|
||||||
|
<key>return</key><string>string</string>
|
||||||
|
<key>arguments</key><array>
|
||||||
|
<map><key>secret</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
<map><key>plainText</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
<map><key>ivString</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
</array>
|
||||||
|
</map>
|
||||||
|
<key>osAESDecrypt</key>
|
||||||
|
<map>
|
||||||
|
<key>return</key><string>string</string>
|
||||||
|
<key>arguments</key><array>
|
||||||
|
<map><key>secret</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
<map><key>encryptedText</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
</array>
|
||||||
|
</map>
|
||||||
|
<key>osAESDecryptFrom</key>
|
||||||
|
<map>
|
||||||
|
<key>return</key><string>string</string>
|
||||||
|
<key>arguments</key><array>
|
||||||
|
<map><key>secret</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
<map><key>encryptedText</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
<map><key>ivString</key><map><key>type</key><string>string</string></map></map>
|
||||||
|
</array>
|
||||||
|
</map>
|
||||||
</map>
|
</map>
|
||||||
</map></llsd>
|
</map></llsd>
|
||||||
Reference in New Issue
Block a user