Jump to content

[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
$keys = _loadkey($key)

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)

$keys = _loadkey($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
Link to comment
Share on other sites

you can find some matrix functions.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Func _decrypt($string, $key)

$keys = _loadkey($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.

Link to comment
Share on other sites

  • 2 weeks later...

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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...