nullschritt Posted August 8, 2012 Share Posted August 8, 2012 (edited) 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 expandcollapse popupfunc _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 August 18, 2012 by nullschritt Link to comment Share on other sites More sharing options...
water Posted August 8, 2012 Share Posted August 8, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki  Link to comment Share on other sites More sharing options...
nullschritt Posted August 8, 2012 Author Share Posted August 8, 2012 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. Link to comment Share on other sites More sharing options...
nullschritt Posted August 9, 2012 Author Share Posted August 9, 2012 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 More sharing options...
nullschritt Posted August 18, 2012 Author Share Posted August 18, 2012 (edited) 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 August 18, 2012 by nullschritt Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now