# [SOLVED]Help with matrix "division"(inversion)

## Recommended Posts

It has been a couple years since I have worked with matricies, I am writing an encryption algorithm, however I am having a problem with the decryption, I cannot seem to remember exactly how to "divide" matricies, I can't seem to figure out how to properly use the inverse to "undo" the multiplication. maybe someone could give me a hand and show me how please?

here is my code

```func _crypt(\$text, \$key, \$binary = 0)
if \$binary = 0 Then
\$text = StringSplit(\$text, '')
\$number = ''
for \$i=1 to \$text[0]
\$number &= _Base(AscW(\$text[\$i]), \$base104)
Next
Else
\$number = \$Text
EndIf
;compress before encrypt, use binarytostring() chrw() and ascw()
Dim \$Original = Binary(\$number)
Dim \$Compressed = _QuickLZ_Compress(\$Original, 2)
;run through encryption matrix, then compress once more (send in binary safe mode, or use binarytostring).
;convert string to ascw() into a 13xn matrix
\$compressed = BinaryToString(\$Compressed)
\$compressed = StringSplit(\$Compressed, "")
for \$i=1 to \$Compressed[0]
\$Compressed[\$i] = AscW(\$Compressed[\$i])
Next
\$h = \$Compressed[0]/13
\$height = round(\$Compressed[0]/13, 0)
;in order for matrix multiplication to work columns == rows
;so since the keys are 13x13 and we are mutliplying text*key
;the text must have 13 columns 13xN * 13x13 (Columns by Rows [width by heigh])
if \$height < \$h Then
\$height += 1
EndIf
local \$ar[\$height][13]
\$chr = 0
for \$i = 0 to \$height-1
for \$i2= 0 to 12
if \$chr < \$Compressed[0] Then
\$ar[\$i][\$i2] = \$compressed[\$Chr]
\$Chr += 1
Else
\$ar[\$i][\$i2] = 0
EndIf
next
next

if \$binary = 0 Then
;if not binary choose 15 random keys
local \$ran[15]
for \$i=0 to 14
\$ran[\$i] = random(0,12,1)
Next
Else
;else only choose 2
local \$ran[2]
for \$i=0 to 1
\$ran[\$i] = random(0,12,1)
Next
EndIf
;record chosen keys

\$keysx = "{"&_ArrayToString(\$ran,';')&"}"
_ArrayDisplay(\$ar)

\$key = _mMul(\$keys[\$ran[0]],\$keys[\$ran[1]]) ;generate first key from 2 random keys
\$enc = _mMul(\$ar, \$key);encrypt text with the key here

if \$binary = 0 Then
for \$i=1 to 13
\$enc = _mMul(\$enc,\$keys[\$ran[\$i+1]])
;then here we run the encrypted text (with the randomized key) through 13 more levels or random encryption
Next
EndIf
\$end = UBound(\$enc, 1) - 1
\$end2 = UBound(\$enc, 2) -1
\$enc[\$end][\$end2] &= \$keysx
;and here we add the numbers of the keys chosen to encrypt the data
\$enc = _keytotext(\$enc, \$end, \$end2)
;then we convert the matrix to a 1d string representation
;there will also be another level of compression here
Return \$enc
EndFunc

Func _decrypt(\$string, \$key)

\$string = _stringtoarray(\$string)

;will be found in the last element of an array
_ArrayDisplay(\$string)
local \$keysx = StringSplit(\$string[UBound(\$string, 1)-1][UBound(\$string, 2)-1], "{}")
\$keysx = StringSplit(\$keysx[2], ";")
_ArrayDisplay(\$keysx)

for \$i=0 to \$keysx[0] - 3
\$string = _mMul(\$string,_inv(\$keys[\$keysx[\$keysx[0]-\$i]]))
Next
\$rankey = _mMul(\$keys[\$keysx[1]],\$keys[\$keysx[2]])
\$string = _mMul(\$string,_inv(\$rankey))
_ArrayDisplay(\$string)

EndFunc```
Edited by nullschritt

##### Share on other sites
you can find some matrix functions.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:

##### Share on other sites

you can find some matrix functions.

I know where the matrix functions, you can see they are already in use in my code, I don't understand how to do what I asked with them.

##### Share on other sites

```Func _decrypt(\$string, \$key)

\$string = _stringtoarray(\$string)

;will be found in the last element of an array
_ArrayDisplay(\$string)
local \$keysx = StringSplit(\$string[UBound(\$string, 1)-1][UBound(\$string, 2)-1], "{}")
\$keysx = StringSplit(\$keysx[2], ";")
_ArrayDisplay(\$keysx)
\$string[UBound(\$string, 1)-1][UBound(\$string, 2)-1] = StringReplace(\$string[UBound(\$string, 1)-1][UBound(\$string, 2)-1], "{"&\$keysx[2]&"}", '')
for \$i=0 to \$keysx[0] - 3
\$string = _mMul(\$string, _inv(\$keys[\$keysx[\$keysx[0]-\$i]]))
Next
\$rankey = _mMul(\$keys[\$keysx[1]],\$keys[\$keysx[2]])
\$string = _mMul(\$string, _inv(\$rankey))
_ArrayDisplay(\$string)

EndFunc```

this is my code now, I feel like I am missing something, the resul seems to be close to what it should be, just not near exact.

##### Share on other sites

Solved, by reducing the size of the marticies, the numbers used in them, and the number of matricies multiplied(13x13x13x13 to 7x7x7x5), as well as added a function to automatically round the results to whole numbers.

It appears that once the number becomes so large, it becomes impossible to accurately reverse.

Edited by nullschritt

## Create an account

Register a new account

×

• Wiki

• Back

• Git