Want to add a script or a project? Upload it and a half million people will see it and your name here this year.
![]() |
![]() |
||
---|---|---|---|
Category: | Contributor: | Creator | |
Encryption | Nekow42 Zarf | ARCFOUR_Strong_Encryption_Implement |
Category: Encryption
By : Nekow42 Zarf
Created: 2010-12-27 Edited: 2010-12-27
Worlds: Second Life
Browse the Zip file
1 // Encryption2 //This is an exact implementation of ARCFOUR that passes the test vectors.34 //Note: Key setup is a long5 //process taking over 20 seconds.67 //Notes on use: The same password can be used more than once, but the nonce always has to be different8 //(something random). You can use this to secure inter-object communication by having a shared (but secret)9 //password, and have the two objects exchange a nonce beforehand. A warning, there is no garbage fitering,10 //so the state array could be polluted by someone injecting fake strings.11 //To prevent this, the first thing exchanged encrypted12 //could be a negative port number, and the two objects could listen only for each other's key. For proper13 //decryption, messages must arrive in the exact order they were encrypted.1415 //This work is licensed under a http://creativecommons.org/licenses/by/3.0/ Creative Commons Attribution 3.0 Unported License16 //Please credit Nekow42 Zarf.17 //Nardok Corrimal, and Strife Onizuka contributed and need credit too.18 list theState;19 integer i;20 integer j;21 //===================================================//22 // Combined Library //23 // "Nov 3 2007", "00:46:15" //24 // Copyright (C) 2004-2007, Strife Onizuka (cc-by) //25 // http://creativecommons.org/licenses/by/3.0/ //26 //===================================================//27 //{29 {31 if(result & 0x80000000){32 return 0;33 }34 return result >> 24;35 }363739 {41 bytes = (input >= 0x80) * (bytes + ~(((1 << bytes) - input) > 0)) / 5;//adjust42 string result = "%" + byte2hex((input >> (6 * bytes)) | ((0x3F80 >> bytes) << !bytes));43 while(bytes)44 result += "%" + byte2hex((((input >> (6 * (bytes = ~-bytes))) | 0x80) & 0xBF));45 return llUnescapeURL(result);46 }4749 {//Helper function for use with unicode characters.50 integer y = (x >> 4) & 0xF;51 return llGetSubString(hexc, y, y) + llGetSubString(hexc, x & 0xF, x & 0xF);52 }//This function would benifit greatly from the DUP opcode, it would remove 19 bytes.5354 string hexc="0123456789ABCDEF";5556 //} Combined Library58 {59 //All addition is done modulous60 list WholePassword;61 list statePartOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75];62 list statePartTwo = [76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150];63 list statePartThree = [151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225];64 list statePartFour = [226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255];65 theState = (statePartOne = statePartTwo = statePartThree = statePartFour = []) + statePartOne + statePartTwo + statePartThree + statePartFour;66 integer swapper;67 //setup the plaintext list68 integer ThePerpetualMax;69 //setup the password. Use i since we already have it.70 //New addition in 1.1, use an MD5 hashing function on the password71 password = llMD5String(password + nonce,0);72 for(i = 0; i < 31; i+=2)73 {74 WholePassword = (WholePassword = []) + WholePassword + (integer)("0x"+llGetSubString(password,i,i+1));75 }76 //Now WholePassword is equal to a list of the hex digits of the MD5 of the password plus the nonce77 i=0;78 j=0;79 //mix init, this sets up the ARCFOUR PRNG80 ThePerpetualMax = llGetListLength(WholePassword);81 for(i=0 ; i < 256; i++)82 {83 //Mix step one84 //Add to the variable j the contents of the ith element of the state array and the nth element of the key, where n is equal to i modulo the length of the key.85 j = (j + llList2Integer(theState, i) + llList2Integer(WholePassword,i % ThePerpetualMax)) % 256;86 //Mix step two. Swap the ith element and the jth element of the state array87 swapper = llList2Integer(theState, i);88 theState=llListReplaceList(theState, [llList2Integer(theState,j)] ,i,i);89 theState=llListReplaceList(theState, [swapper],j,j);90 }91 i=0;92 j=0;93 //Key setup is complete!9495 }97 //This does not repeat key setup, but continues with the same state array and values for i and j98 {99 //All addition is done modulous100 list PlaintextBytes;101 string CipheredBytes;102 integer n;103 integer counter;104 integer swapper;105 integer ByteToAdd;106 //setup the plaintext list107 integer ThePerpetualMax;108 ThePerpetualMax = llStringLength(plaintext);109 for(counter = 0; counter < ThePerpetualMax; counter++)110 {111 PlaintextBytes = (PlaintextBytes = []) + PlaintextBytes + [UTF8ToUnicodeInteger(llGetSubString(plaintext,counter,counter))];112113 }114 ThePerpetualMax = llGetListLength(PlaintextBytes);115 for(counter=0;counter < ThePerpetualMax;counter++)116 {117 //The variable i is incremented by one118 i = (i + 1) % 256;119 //The contents of the ith element of the state array is then added to j120 j = (j + llList2Integer(theState, i)) % 256;121 //The ith and jth elements of the state array are swapped and their contents are added together to form a new value n.122 swapper = llList2Integer(theState, i);123 theState=llListReplaceList(theState, [llList2Integer(theState,j)] ,i,i);124 theState=llListReplaceList(theState, [swapper],j,j);125 n = (llList2Integer(theState, i) + llList2Integer(theState, j)) % 256;126 //The nth element of the state array is then combined with the message byte, using a bit by bit exclusive-or operation, to form the output byte.127 ByteToAdd = llList2Integer(theState, n) ^ llList2Integer(PlaintextBytes, counter);128 CipheredBytes = CipheredBytes + byte2hex(ByteToAdd);129 }130131132 return CipheredBytes;133 }134 default135 {136137 touch_start(integer total_number)138 {139 llOwnerSay("Key is 'Key', plaintext is 'Plaintext'. Output:");140 KeySetup("Key","");141 llOwnerSay("Key setup complete. Encrypting now...");142 llOwnerSay(EncryptText("Plaintext"));143 }144 }
Category: Encryption
By : Nekow42 Zarf
Created: 2010-12-27 Edited: 2010-12-27
Worlds: Second Life
1 //This is an exact implementation of ARCFOUR that passes the test vectors.23 //Note: Key setup is a long4 //process taking over 20 seconds.56 //Notes on use: The same password can be used more than once, but the nonce always has to be different7 //(something random). You can use this to secure inter-object communication by having a shared (but secret)8 //password, and have the two objects exchange a nonce beforehand. A warning, there is no garbage fitering,9 //so the state array could be polluted by someone injecting fake strings.10 //To prevent this, the first thing exchanged encrypted11 //could be a negative port number, and the two objects could listen only for each other's key. For proper12 //decryption, messages must arrive in the exact order they were encrypted.1314 //This work is licensed under a http://creativecommons.org/licenses/by/3.0/ Creative Commons Attribution 3.0 Unported License15 //Please credit Nekow42 Zarf.16 //Nardok Corrimal, and Strife Onizuka contributed and need credit too.17 list theState;18 integer i;19 integer j;20 //===================================================//21 // Combined Library //22 // "Nov 3 2007", "00:46:15" //23 // Copyright (C) 2004-2007, Strife Onizuka (cc-by) //24 // http://creativecommons.org/licenses/by/3.0/ //25 //===================================================//26 //{28 {30 if(result & 0x80000000){31 return 0;32 }33 return result >> 24;34 }353638 {40 bytes = (input >= 0x80) * (bytes + ~(((1 << bytes) - input) > 0)) / 5;//adjust41 string result = "%" + byte2hex((input >> (6 * bytes)) | ((0x3F80 >> bytes) << !bytes));42 while(bytes)43 result += "%" + byte2hex((((input >> (6 * (bytes = ~-bytes))) | 0x80) & 0xBF));44 return llUnescapeURL(result);45 }4648 {//Helper function for use with unicode characters.49 integer y = (x >> 4) & 0xF;50 return llGetSubString(hexc, y, y) + llGetSubString(hexc, x & 0xF, x & 0xF);51 }//This function would benefit greatly from the DUP opcode, it would remove 19 bytes.5253 string hexc="0123456789ABCDEF";5455 //} Combined Library57 {58 list WholePassword;59 list statePartOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75];60 list statePartTwo = [76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150];61 list statePartThree = [151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225];62 list statePartFour = [226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255];63 theState = (statePartOne = statePartTwo = statePartThree = statePartFour = []) + statePartOne + statePartTwo + statePartThree + statePartFour;64 integer swapper;65 //setup the plaintext list66 integer ThePerpetualMax;67 //setup the password. Use i since we already have it.68 //New addition in 1.1, use an MD5 hashing function on the password69 password = llMD5String(password + nonce,0);70 for(i = 0; i < 31; i+=2)71 {72 WholePassword = (WholePassword = []) + WholePassword + (integer)("0x"+llGetSubString(password,i,i+1));73 }74 //Now WholePassword is equal to a list of the hex digits of the MD5 of the password plus the nonce75 i=0;76 j=0;77 //mix init, this sets up the ARCFOUR PRNG78 ThePerpetualMax = llGetListLength(WholePassword);79 for(i=0 ; i < 256; i++)80 {81 //Mix step one82 //Add to the variable j the contents of the ith element of the state array and the nth element of the key, where n is equal to i modulo the length of the key.83 j = (j + llList2Integer(theState, i) + llList2Integer(WholePassword,i % ThePerpetualMax)) % 256;84 //Mix step two. Swap the ith element and the jth element of the state array85 swapper = llList2Integer(theState, i);86 theState=llListReplaceList(theState, [llList2Integer(theState,j)] ,i,i);87 theState=llListReplaceList(theState, [swapper],j,j);88 }89 i=0;90 j=0;91 //Key setup is complete!92 }94 {95 //This does not repeat key setup, but continues with the same state array and values for i and j96 list CiphertextBytes;97 string DecryptedString;98 integer n;99 integer counter;100 integer swapper;101 integer ByteToAdd;102 //setup the plaintext list103 integer ThePerpetualMax;104 ThePerpetualMax = llStringLength(ciphertext) - 1;105 for(counter = 0; counter < ThePerpetualMax; counter+=2)106 {107 CiphertextBytes = (CiphertextBytes = []) + CiphertextBytes + (integer)("0x"+llGetSubString(ciphertext,counter,counter+1));108 }109 //setup the password. Use i since we already have it.110 ThePerpetualMax = llGetListLength(CiphertextBytes);111 for(counter=0;counter < ThePerpetualMax;counter++)112 {113 //The variable i is incremented by one114 i = (i + 1) % 256;115 //The contents of the ith element of the state array is then added to j116 j = (j + llList2Integer(theState, i)) % 256;117 //The ith and jth elements of the state array are swapped and their contents are added together to form a new value n.118 swapper = llList2Integer(theState, i);119 theState=llListReplaceList(theState, [llList2Integer(theState,j)] ,i,i);120 theState=llListReplaceList(theState, [swapper],j,j);121 n = (llList2Integer(theState, i) + llList2Integer(theState, j)) % 256;122 //The nth element of the state array is then combined with the message byte, using a bit by bit exclusive-or operation, to form the output byte.123 ByteToAdd = llList2Integer(theState, n) ^ llList2Integer(CiphertextBytes, counter);124 DecryptedString = DecryptedString + UnicodeIntegerToUTF8(ByteToAdd);125 }126 return DecryptedString;127 }128 default129 {130131 touch_start(integer total_number)132 {133 llOwnerSay( "Key Setup");134 KeySetup("Key","");135 llOwnerSay( "Decrypting test string.");136 llOwnerSay(DecryptText("506C61696E74657874"));137 }138 }
Category: Encryption
By : Nekow42 Zarf
Created: 2010-12-27 Edited: 2010-12-27
Worlds: Second Life
1 list theState;2 integer i;3 integer j;4 //===================================================//5 // Combined Library //6 // "Nov 3 2007", "00:46:15" //7 // Copyright (C) 2004-2007, Strife Onizuka (cc-by) //8 // http://creativecommons.org/licenses/by/3.0/ //9 //===================================================//10 //{12 {14 if(result & 0x80000000){15 return 0;16 }17 return result >> 24;18 }192022 {24 bytes = (input >= 0x80) * (bytes + ~(((1 << bytes) - input) > 0)) / 5;//adjust25 string result = "%" + byte2hex((input >> (6 * bytes)) | ((0x3F80 >> bytes) << !bytes));26 while(bytes)27 result += "%" + byte2hex((((input >> (6 * (bytes = ~-bytes))) | 0x80) & 0xBF));28 return llUnescapeURL(result);29 }3032 {//Helper function for use with unicode characters.33 integer y = (x >> 4) & 0xF;34 return llGetSubString(hexc, y, y) + llGetSubString(hexc, x & 0xF, x & 0xF);35 }//This function would benifit greatly from the DUP opcode, it would remove 19 bytes.3637 string hexc="0123456789ABCDEF";3839 //} Combined Library41 {42 //All addition is done modulous43 list WholePassword;44 list statePartOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75];45 list statePartTwo = [76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150];46 list statePartThree = [151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225];47 list statePartFour = [226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255];48 theState = statePartOne + statePartTwo + statePartThree + statePartFour;49 statePartOne = [];50 statePartTwo = [];51 statePartThree = [];52 statePartFour = [];53 integer swapper;54 //setup the plaintext list55 integer ThePerpetualMax;56 //setup the password. Use i since we already have it.57 ThePerpetualMax = llStringLength(password);58 for(i = 0; i < ThePerpetualMax; i++)59 {60 WholePassword = (WholePassword = []) + WholePassword + [UTF8ToUnicodeInteger(llGetSubString(password,i,i))];61 }62 ThePerpetualMax = llStringLength(nonce);63 for(i = 0; i < ThePerpetualMax; i++)64 {65 WholePassword = (WholePassword = []) + WholePassword + [UTF8ToUnicodeInteger(llGetSubString(nonce,i,i))];66 }67 //Now WholePassword is equal to a list of the ASCII values of the password + the nonce68 i=0;69 j=0;70 //mix init, this sets up the ARCFOUR PRNG71 ThePerpetualMax = llGetListLength(WholePassword);72 for(i=0 ; i < 256; i++)73 {74 //Mix step one75 //Add to the variable j the contents of the ith element of the state array and the nth element of the key, where n is equal to i modulo the length of the key.76 j = (j + llList2Integer(theState, i) + llList2Integer(WholePassword,i % ThePerpetualMax)) % 256;77 //Mix step two. Swap the ith element and the jth element of the state array78 swapper = llList2Integer(theState, i);79 theState=llListReplaceList(theState, [llList2Integer(theState,j)] ,i,i);80 theState=llListReplaceList(theState, [swapper],j,j);81 }82 i=0;83 j=0;84 //Key setup is complete!8586 }88 //This does not repeat key setup, but continues with the same state array and values for i and j89 {90 //All addition is done modulous91 string Output;92 list PlaintextBytes;93 list CipheredBytes;94 integer n;95 integer counter;96 integer swapper;97 integer ByteToAdd;98 //setup the plaintext list99 integer ThePerpetualMax;100 ThePerpetualMax = llStringLength(plaintext);101 for(counter = 0; counter < ThePerpetualMax; counter++)102 {103 PlaintextBytes = (PlaintextBytes = []) + PlaintextBytes + [UTF8ToUnicodeInteger(llGetSubString(plaintext,counter,counter))];104105 }106 ThePerpetualMax = llGetListLength(PlaintextBytes);107 for(counter=0;counter < ThePerpetualMax;counter++)108 {109 //The variable i is incremented by one110 i = (i + 1) % 256;111 //The contents of the ith element of the state array is then added to j112 j = (j + llList2Integer(theState, i)) % 256;113 //The ith and jth elements of the state array are swapped and their contents are added together to form a new value n.114 swapper = llList2Integer(theState, i);115 theState=llListReplaceList(theState, [llList2Integer(theState,j)] ,i,i);116 theState=llListReplaceList(theState, [swapper],j,j);117 n = (llList2Integer(theState, i) + llList2Integer(theState, j)) % 256;118 //The nth element of the state array is then combined with the message byte, using a bit by bit exclusive-or operation, to form the output byte.119 ByteToAdd = llList2Integer(theState, n) ^ llList2Integer(PlaintextBytes, counter);120 CipheredBytes = (CipheredBytes = []) + CipheredBytes + [ByteToAdd];121 }122123124 return llList2CSV(CipheredBytes);125 }126 default127 {128129 touch_start(integer total_number)130 {131 llSay(0, "Key is 'Key', plaintext is 'Plaintext'. Output:");132 KeySetup("Key","");133 llSay(0,"Key setup complete. Encrypting now...");134 llSay(0,EncryptText("Plaintext"));135 //will output 187, 243, 22, 232, 217, 64, 175, 10, 211136 }137 }
Back to the Best Free Tools in Second Life and OpenSim.