Notifications
Clear all

[Closed] Corss platform encryption/decryption (text) with maxscript

Hi everyone,
I think there’s nobody talked about this,I want to encrypt text for my project,from php(server) to maxscript,encrypt in php,then ,decrypt in maxscript,just text,or a long string,maybe need a little secure,after I searched google,I found there are a lot of examples using C# language,but I really don’t know how to translate them into maxscript,and I don’t know whether it is possible support in maxscript either.What I want is translate below codes to maxscript,I tired several days myself,but unlucky:

origin url:
http://stackoverflow.com/questions/4329260/cross-platform-php-to-c-sharp-net-encryption-decryption-with-rijndael
Below is C# side codes:

public string Decode(string str)
{
    byte[] decbuff = Convert.FromBase64String(str);
    return System.Text.Encoding.UTF8.GetString(decbuff);
}

static public String DecryptRJ256(string cypher, string KeyString, string IVString)
{

    string sRet = "";
    RijndaelManaged rj = new RijndaelManaged();
    UTF8Encoding encoding = new UTF8Encoding();


    try
    {
        //byte[] message = Convert.FromBase64String(cypher);
        byte[] message = encoding.GetBytes(cypher);

        byte[] Key = encoding.GetBytes(KeyString);
        byte[] IV = encoding.GetBytes(IVString);

        rj.Padding = PaddingMode.Zeros;
        rj.Mode = CipherMode.CBC;
        rj.KeySize = 256;
        rj.BlockSize = 256;
        rj.Key = Key;
        rj.IV = IV;
        MemoryStream ms = new MemoryStream(message);

        using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
        {
            using (StreamReader sr = new StreamReader(cs))
            {
                sRet = sr.ReadToEnd();
            }
        }

    }
    finally
    {
        rj.Clear();
    }

    return sRet;
}

string temp = DecryptRJ256(Server.UrlDecode(Decode(cypher)), keyString, ivString);

Or does anyone has a better suggestion or solution of encryption/decryption between php and maxscript,thanks.

5 Replies

You can easily access the .Net api in maxsciprt using some keywords : max doc about dotNet

so C#

System.Text.Encoding.UTF8.GetString(decbuff);

would become in Maxscript

(DotnetClass "System.Text.UTF8Encoding").UTF8.GetString(decbuff)
1 Reply
(@momo2012)
Joined: 11 months ago

Posts: 0

Thanks,actually there has a similar maxscript for encryption and decrytion :
http://forums.cgsfolio.org/showthread.php?t=1234542

fn encryptText plainText tKey: tIV: =
	(
		dnConvert = dotnetClass "System.Convert"
		dnUTF8 = dotNetObject "System.Text.UTF8Encoding"
		algorithm = (dotnetClass "System.Security.Cryptography.SymmetricAlgorithm").Create()
		
		encText = dnUTF8.GetBytes plainText
		memStream = dotnetObject "System.IO.MemoryStream"
		cStreamMode = (dotnetClass "System.Security.Cryptography.CryptoStreamMode")
		cStreamTransform = algorithm.CreateEncryptor tKey tIV
		cStream = dotnetObject "System.Security.Cryptography.CryptoStream" memStream cStreamTransform cStreamMode.Write
		cStream.Write encText 0 encText.Count
		cStream.FlushFinalBlock()
		cipherBytes = memStream.ToArray()			
		memStream.Close()
		cStream.Close()

		dnConvert.ToBase64String cipherBytes
	)

As I’ve said ,I tried too many times ,but failed.
:banghead:

Are you sure that your key and IV are bytes array and not strings ?

Because appart from that it works If I use the default key and IV from the algorithm creation :

cStreamTransform = algorithm.CreateEncryptor algorithm.key algorithm.IV

edit :
If you need to convert strings to bytes array :

algorithm.key = (DotnetObject "System.Text.UTF8Encoding").GetBytes "32chars" 	
algorithm.IV = (DotnetObject "System.Text.UTF8Encoding").GetBytes "16chars"

final code :

fn encryptText plainText tKey: tIV: =
(
	dnConvert = dotnetClass "System.Convert"
	dnUTF8 = dotNetObject "System.Text.UTF8Encoding"
	algorithm = (dotnetClass "System.Security.Cryptography.SymmetricAlgorithm").Create()
	algorithm.key = (DotnetObject "System.Text.UTF8Encoding").GetBytes tKey
	algorithm.IV = (DotnetObject "System.Text.UTF8Encoding").GetBytes tIV
	
	encText = dnUTF8.GetBytes plainText
	memStream = dotnetObject "System.IO.MemoryStream"
	cStreamMode = (dotnetClass "System.Security.Cryptography.CryptoStreamMode")
	cStreamTransform = algorithm.CreateEncryptor algorithm.key algorithm.IV
	cStream = dotnetObject "System.Security.Cryptography.CryptoStream" memStream cStreamTransform cStreamMode.Write
	cStream.Write encText 0 encText.Count
	cStream.FlushFinalBlock()
	cipherBytes = memStream.ToArray()			
	memStream.Close()
	cStream.Close()

	dnConvert.ToBase64String cipherBytes
)

encryptText "test" tKey:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" tIV:"bbbbbbbbbbbbbbbb"

1 Reply
(@momo2012)
Joined: 11 months ago

Posts: 0

Perfect!Thanks a lot paul,with your fixed codes,it’s works like a charm!

Hello. This is the tool you are looking for: http://www.scriptspot.com/3ds-max/scripts/maxscript-protector
It can encrypt maxscript and it works on most versions of 3D Max.
It’s interesting topic…