Notifications
Clear all

[Closed] Encrypt and decrypt string with DotNet

Hi everyone!
In order to avoid “necro-posting” I prefer to open a new topic here about encryption and decryption of strings.
I’d like to encrypt a text password that should be saved in a INI file.
After looking around I’ve found this old post
Cross platform encryption/decryption (text) with maxscript
That seems to be similar to another article here
YOU CAN DO IT! VFX

I’ve taken the Encrypt function and it works: since I’m not very into security stuff and never really used DotNet, I tried to compare the script with this C# example to understand the code and be able to use and rewrite it:
System_Security_Cryptography_Rijndael_Create

So I tried to write my decrypt function but I’m stuck… I see that the first part could be almost the same lines but I get an error when trying to read from the stream. The object exists, but I get this Runtime error: .NET runtime exception: Object reference not set to an instance of an object. when evaluating ms.
I’m probably missing something very easy, but I admit I’m in trouble
Maybe some of you could point me in the right direction…
My function is this:


fn decryptText cipherText tKey: tIV: =
(
	plaintext = ""

	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
	
	cipherBytes = dnUTF8.GetBytes cipherText
	
	memStream = dotnetObject "System.IO.MemoryStream"
	cStreamMode = (dotnetClass "System.Security.Cryptography.CryptoStreamMode")
	
	cStreamTransform = algorithm.CreateDecryptor algorithm.key algorithm.IV

	cStream = dotnetObject "System.Security.Cryptography.CryptoStream" memStream cStreamTransform cStreamMode.Read
	
	print cStream --Just for debugging... It exists!
	
	/* ERROR HERE!!! --> */ cStream.Read cipherBytes 0 cipherBytes.count

	plaintext = memStream.ToString()			
	memStream.Close()
	cStream.Close()
	dnConvert.ToBase64String plaintext
)

Many, many thanks!!!

3 Replies

fn decryptText cipherText tKey: tIV: =
(
–plaintext = “”

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
cipherBytes = dnConvert.FromBase64String cipherText
--cipherBytes = dnUTF8.GetBytes cipherText

memStream = dotnetObject "System.IO.MemoryStream"
cStreamMode = (dotnetClass "System.Security.Cryptography.CryptoStreamMode")

cStreamTransform = algorithm.CreateDecryptor algorithm.key algorithm.IV

cStream = dotnetObject "System.Security.Cryptography.CryptoStream" memStream cStreamTransform cStreamMode.Write--Read
/* ERROR HERE!!! --> */ 
--cStream.Read cipherBytes 0 cipherBytes.count
cStream.Write cipherBytes 0 cipherBytes.count
cStream.FlushFinalBlock()
plaintext = memStream.ToArray()            
memStream.Close()
cStream.Close()
--dnConvert.ToBase64String plaintext
dnUTF8.GetString plaintext

)

decryptText “JSkhLYfOlYEFQ/QnYrr8DQ==” tKey:“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa” tIV:“bbbbbbbbbbbbbbbb”

… deleted

Thanks bud!!
This worked like a charm!!
I had lost hope in some answer after such a long time!