KRYPTCODE
Comment: 'License:'
License:
KyrptCode by Avogatro is licensed under a Creative Commons Attribution 3.0 Unported License.
http://creativecommons.org/licenses/by/3.0/legalcode
SHA256 Code are from magnificence7's project Hash Lib 2.0.0
http://www.sc2mapster.com/assets/md5-hash/
ASCIIScript
ASCIIScript
//------------------------ascii part----------------------------
//http://en.wikipedia.org/wiki/ascii
const string KRYPTCODE_alphabet_ascii="\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
const int KRYPTCODE__alphabetLength=256;
string base64_alphabet= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
//fill string s to have fixed size
string KRYPTCODE_fill(string s, int i){
int c=i-StringLength(s);
int temp;
string t="";
while(c>0){
temp = RandomInt(33,KRYPTCODE__alphabetLength-30);
t+=StringSub(KRYPTCODE_alphabet_ascii,temp ,temp);
//t+="A";
c-=1;
}
return t+s;
}
//rm filled String from KRYPTCODE_fill
string KRYPTCODE_cut(string s,int i){
return StringSub(s,i+1,StringLength(s));
}
//convert int to byte no error check
string byte_to_char( int i ){
return StringSub(KRYPTCODE_alphabet_ascii,i+1,i+1);
}
//convert ascii to byte no error check
int char_to_byte( string s ){
int i= StringFind( KRYPTCODE_alphabet_ascii, s , true);
return i-1;
}
//check if string is valid ascii
bool check_string(string s) {
int i = 1;
int a;
while(i<=StringLength(s)){
a = StringFind( KRYPTCODE_alphabet_ascii, StringSub(s,i,i) , true);
if (a<=0){
return false;
}
i+=1;
}
return true;
}
//end---------------------ascii part----------------------------
string KRYPTCODE_AES_CTR_expandString(string in){
int length = 0;
int k = 0;
length = StringLength(in);
if ( length % 16 == 0){
k = (length/16+1)*16-length;
}else{
k = (length/16+2)*16-length;
}
length = k + length;
return KRYPTCODE_fill(in, length-1)+StringSub(KRYPTCODE_alphabet_ascii,k,k);
}
string KRYPTCODE_AES_CTR_shrinkString(string input){
int length = 0;
int k = 0;
length = StringLength(input);
k = char_to_byte(StringSub(input,length,length));
return KRYPTCODE_cut(StringSub(input,1,length-1),k);
}
string ascii_to_base64(string input){
string output;
int chr1;
int chr2;
int chr3;
int enc1;
int enc2;
int enc3;
int enc4;
int i = 1;
while (i<= StringLength(input)){
chr1 = StringFind(KRYPTCODE_alphabet_ascii, StringSub(input,i,i), true)-1;
i+=1;
chr2 = StringFind(KRYPTCODE_alphabet_ascii, StringSub(input,i,i), true)-1;
i+=1;
chr3 = StringFind(KRYPTCODE_alphabet_ascii, StringSub(input,i,i), true)-1;
i+=1;
enc1 = chr1/4;
enc2 = (chr1%4)*16 + chr2/16;
enc3 = (chr2%16)*4 + chr3/64;
enc4 = chr3%64;
if (chr2<0) {
enc3 = 64;
enc4 = 64;
} else if (chr3<0) {
enc4 = 64;
}
output = output + StringSub(base64_alphabet,enc1+1,enc1+1)+ StringSub(base64_alphabet,enc2+1,enc2+1);
output = output + StringSub(base64_alphabet,enc3+1,enc3+1)+ StringSub(base64_alphabet,enc4+1,enc4+1);
}
return output;
}
string base64_to_ascii(string input){
string output;
int chr1;
int chr2;
int chr3;
int enc1;
int enc2;
int enc3;
int enc4;
int i = 1;
while (i<= StringLength(input)){
if ( StringFind( base64_alphabet, StringSub(input,i,i) , true)<1){
return "";
}
i+=1;
}
i=1;
while (i<= StringLength(input)){
enc1 = StringFind( base64_alphabet, StringSub(input,i,i) , true)-1;
i+=1;
enc2 = StringFind( base64_alphabet, StringSub(input,i,i) , true)-1;
i+=1;
enc3 = StringFind( base64_alphabet, StringSub(input,i,i) , true)-1;
i+=1;
enc4 = StringFind( base64_alphabet, StringSub(input,i,i) , true)-1;
i+=1;
chr1 = enc1*4 +enc2/16;
chr2 = (enc2%16)*16+enc3/4;
chr3 = (enc3%4)*64+enc4;
output = output + StringSub(KRYPTCODE_alphabet_ascii,chr1+1,chr1+1);
if (enc3 != 64) {
output = output + StringSub(KRYPTCODE_alphabet_ascii,chr2+1,chr2+1);
}
if (enc4 != 64) {
output = output + StringSub(KRYPTCODE_alphabet_ascii,chr3+1,chr3+1);
}
}
return output;
}
Global Script
// Hash Input
byte[1024] HashInputData;
int HashInputSize = 0;
int GetHashInputLittleEndianInt( int position )
{
int temp;
int result;
position *= 4;
result = HashInputData[position];
temp = HashInputData[position + 1];
result |= temp << 8;
temp = HashInputData[position + 2];
result |= temp << 16;
temp = HashInputData[position + 3];
result |= temp << 24;
return result;
}
int GetHashInputBigEndianInt( int position )
{
int temp;
int result;
position *= 4;
temp = HashInputData[position];
result = temp << 24;
temp = HashInputData[position + 1];
result |= temp << 16;
temp = HashInputData[position + 2];
result |= temp << 8;
temp = HashInputData[position + 3];
result |= temp;
return result;
}
// ASCII conversion
string asciiTable = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff";
byte CharToAsciiCode(string charContainingString, int charPosition)
{
return StringFind( asciiTable, StringSub( charContainingString, charPosition + 1, charPosition + 1), c_stringCase );
}
// Hex Conversion
string[16] HexTable;
string GetHexChar( int number )
{
return HexTable[number];
}
string GetUnsignedLittleEndianHexRepresentation( int unsignedInt )
{
string representation = "";
representation += GetHexChar( ( unsignedInt & 0x000000F0 ) >> 4 );
representation += GetHexChar( ( unsignedInt & 0x0000000F ) );
representation += GetHexChar( ( unsignedInt & 0x0000F000 ) >> 12 );
representation += GetHexChar( ( unsignedInt & 0x00000F00) >> 8 );
representation += GetHexChar( ( unsignedInt & 0x00F00000 ) >> 20 );
representation += GetHexChar( ( unsignedInt & 0x000F0000) >> 16 );
if ( unsignedInt >= 0 )
{
representation += GetHexChar( unsignedInt >> 28 );
}
else
{
unsignedInt += 0x7FFFFFFF + 1;
representation += GetHexChar( 0x8 + ( unsignedInt >> 28 ) );
}
representation += GetHexChar( ( unsignedInt & 0x0F000000) >> 24 );
return representation;
}
string GetUnsignedHexRepresentation( int unsignedInt )
{
string representation = "";
if ( unsignedInt >= 0 )
{
representation += GetHexChar( unsignedInt >> 28 );
}
else
{
unsignedInt += 0x7FFFFFFF + 1;
representation += GetHexChar( 0x8 + ( unsignedInt >> 28 ) );
}
representation += GetHexChar( ( unsignedInt & 0x0F000000) >> 24 );
representation += GetHexChar( ( unsignedInt & 0x00F00000 ) >> 20 );
representation += GetHexChar( ( unsignedInt & 0x000F0000) >> 16 );
representation += GetHexChar( ( unsignedInt & 0x0000F000 ) >> 12 );
representation += GetHexChar( ( unsignedInt & 0x00000F00) >> 8 );
representation += GetHexChar( ( unsignedInt & 0x000000F0 ) >> 4 );
representation += GetHexChar( ( unsignedInt & 0x0000000F ) );
return representation;
}
// Resolve data function
int GetFixedDataAsInt(fixed fixedVar)
{
int data;
data= ( FixedToInt(fixedVar) << 12 );
fixedVar -= data;
data|= FixedToInt( fixedVar* Pow2(12) );
return data;
}
// Operations
int UnsignedRightShift( int input, int shift ) // Right shift pads bit 1's when the value is under zero for signed integers. So this is the work-around.
{
return input >> shift & ~( -1 << ( 32 - shift ) );
}
int UnsignedLeftRotate( int input, int rotation )
{
rotation = ModI( rotation, 32 );
return ( input << rotation ) | UnsignedRightShift (input, 32 - rotation );
}
int UnsignedRightRotate( int input, int rotation )
{
rotation = ModI( rotation, 32 );
return UnsignedRightShift( input, rotation ) | ( input << ( 32 - rotation ) );
}
int UnsignedLeftRotate32( int input, int rotation ) // Optimized version for rotation within range 0 =< x =< 32
{
return ( input << rotation ) | UnsignedRightShift (input, 32 - rotation );
}
int UnsignedRightRotate32( int input, int rotation ) // Optimized version for rotation within range 0 =< x =< 32
{
return UnsignedRightShift( input, rotation ) | ( input << ( 32 - rotation ) );
}
SHA256Script
SHA256 Script
int Sha256Ch( int x, int y, int z )
{
return ( x & y ) ^ ( ~x & z );
}
int Sha256Maj( int x, int y, int z )
{
return ( x & y ) ^ ( x & z ) ^ ( y & z );
}
int Sha256CapitalSigma0( int x )
{
return UnsignedRightRotate32( x, 2 ) ^ UnsignedRightRotate32( x, 13 ) ^ UnsignedRightRotate32( x, 22 );
}
int Sha256CapitalSigma1( int x )
{
return UnsignedRightRotate32( x, 6 ) ^ UnsignedRightRotate32( x, 11 ) ^ UnsignedRightRotate32( x, 25 );
}
int Sha256Sigma0( int x )
{
return UnsignedRightRotate32( x, 7 ) ^ UnsignedRightRotate32( x, 18 ) ^ UnsignedRightShift( x, 3 );
}
int Sha256Sigma1( int x )
{
return UnsignedRightRotate32( x, 17 ) ^ UnsignedRightRotate32( x, 19 ) ^ UnsignedRightShift( x, 10 );
}
int Sha256T0( int e, int f, int g, int h, int k, int w )
{
return h + Sha256CapitalSigma1( e ) + Sha256Ch( e, f, g ) + k + w;
}
int Sha256T1( int a, int b, int c )
{
return Sha256CapitalSigma0( a ) + Sha256Maj( a, b, c );
}
string GenerateSha256HashCode()
{
// Variables
int h0 = 0x6a09e667;
int h1 = 0x0b000000 << 4 | 0x0b67ae85;
int h2 = 0x3c6ef372;
int h3 = 0x0a000000 << 4 | 0x054ff53a;
int h4 = 0x510e527f;
int h5 = 0x09000000 << 4 | 0x0b05688c;
int h6 = 0x1f83d9ab;
int h7 = 0x5be0cd19;
int a; int b; int c; int d; int e; int f; int g; int h; int t0; int t1;
int[64] w;
int i = 0; int j = 0; int k = 0;
int messageNBits;
int paddingLength;
// Prepare message
messageNBits = HashInputSize * 8;
HashInputData[HashInputSize] = 0x80;
HashInputSize += 1;
paddingLength = 56 - ModI( HashInputSize, 64 );
if ( paddingLength < 0 )
{
paddingLength += 64;
}
while ( i < paddingLength )
{
HashInputData[HashInputSize] = 0;
HashInputSize += 1;
i += 1;
}
//Note: You might want to change this when the hash input increases in size.
HashInputData[HashInputSize] = 0;
HashInputSize += 1;
HashInputData[HashInputSize] = 0;
HashInputSize += 1;
HashInputData[HashInputSize] = 0;
HashInputSize += 1;
HashInputData[HashInputSize] = 0;
HashInputSize += 1;
HashInputData[HashInputSize] = 0;
HashInputSize += 1;
HashInputData[HashInputSize] = 0;
HashInputSize += 1;
HashInputData[HashInputSize] = messageNBits >> 8;
HashInputSize += 1;
HashInputData[HashInputSize] = messageNBits;
HashInputSize += 1;
// Loop
i = 0;
while ( i < ( HashInputSize - 1 ) )
{
// Working Variables
a = h0;
b = h1;
c = h2;
d = h3;
e = h4;
f = h5;
g = h6;
h = h7;
// Preparing Extra Message Schedule
j = 0;
while ( j < 16 )
{
w[j] = GetHashInputBigEndianInt( k );
j += 1;
k += 1;
}
while ( j < 64 )
{
w[j] = Sha256Sigma1( w[j - 2] ) +
w[j - 7] +
Sha256Sigma0( w[j - 15] ) +
w[j - 16];
j += 1;
}
// 0 to 15
t0 = Sha256T0( e, f, g, h, 0x428a2f98, w[0] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0; i += 1;
t0 = Sha256T0( d, e, f, g, 0x71374491, w[1] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0; i += 1;
t0 = Sha256T0( c, d, e, f, 0x0b000000 << 4 | 0x05c0fbcf, w[2] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0; i += 1;
t0 = Sha256T0( b, c, d, e, 0x0e000000 << 4 | 0x09b5dba5, w[3] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0; i += 1;
t0 = Sha256T0( a, b, c, d, 0x3956c25b, w[4] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0; i += 1;
t0 = Sha256T0( h, a, b, c, 0x59f111f1, w[5] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0; i += 1;
t0 = Sha256T0( g, h, a, b, 0x09000000 << 4 | 0x023f82a4, w[6] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0; i += 1;
t0 = Sha256T0( f, g, h, a, 0x0a000000 << 4 | 0x0b1c5ed5, w[7] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0; i += 1;
t0 = Sha256T0( e, f, g, h, 0x0d000000 << 4 | 0x0807aa98, w[8] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0; i += 1;
t0 = Sha256T0( d, e, f, g, 0x12835b01, w[9] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0; i += 1;
t0 = Sha256T0( c, d, e, f, 0x243185be, w[10] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0; i += 1;
t0 = Sha256T0( b, c, d, e, 0x550c7dc3, w[11] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0; i += 1;
t0 = Sha256T0( a, b, c, d, 0x72be5d74, w[12] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0; i += 1;
t0 = Sha256T0( h, a, b, c, 0x08000000 << 4 | 0x00deb1fe, w[13] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0; i += 1;
t0 = Sha256T0( g, h, a, b, 0x09000000 << 4 | 0x0bdc06a7, w[14] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0; i += 1;
t0 = Sha256T0( f, g, h, a, 0x0c000000 << 4 | 0x019bf174, w[15] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0;
// 16 to 31
t0 = Sha256T0( e, f, g, h, 0x0e000000 << 4 | 0x049b69c1, w[16] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0;
t0 = Sha256T0( d, e, f, g, 0x0e000000 << 4 | 0x0fbe4786, w[17] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0;
t0 = Sha256T0( c, d, e, f, 0x0fc19dc6, w[18] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0;
t0 = Sha256T0( b, c, d, e, 0x240ca1cc, w[19] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0;
t0 = Sha256T0( a, b, c, d, 0x2de92c6f, w[20] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0;
t0 = Sha256T0( h, a, b, c, 0x4a7484aa, w[21] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0;
t0 = Sha256T0( g, h, a, b, 0x5cb0a9dc, w[22] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0;
t0 = Sha256T0( f, g, h, a, 0x76f988da, w[23] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0;
t0 = Sha256T0( e, f, g, h, 0x09000000 << 4 | 0x083e5152, w[24] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0;
t0 = Sha256T0( d, e, f, g, 0x0a000000 << 4 | 0x0831c66d, w[25] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0;
t0 = Sha256T0( c, d, e, f, 0x0b000000 << 4 | 0x000327c8, w[26] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0;
t0 = Sha256T0( b, c, d, e, 0x0b000000 << 4 | 0x0f597fc7, w[27] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0;
t0 = Sha256T0( a, b, c, d, 0x0c000000 << 4 | 0x06e00bf3, w[28] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0;
t0 = Sha256T0( h, a, b, c, 0x0d000000 << 4 | 0x05a79147, w[29] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0;
t0 = Sha256T0( g, h, a, b, 0x06ca6351, w[30] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0;
t0 = Sha256T0( f, g, h, a, 0x14292967, w[31] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0;
// 32 to 47
t0 = Sha256T0( e, f, g, h, 0x27b70a85, w[32] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0;
t0 = Sha256T0( d, e, f, g, 0x2e1b2138, w[33] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0;
t0 = Sha256T0( c, d, e, f, 0x4d2c6dfc, w[34] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0;
t0 = Sha256T0( b, c, d, e, 0x53380d13, w[35] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0;
t0 = Sha256T0( a, b, c, d, 0x650a7354, w[36] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0;
t0 = Sha256T0( h, a, b, c, 0x766a0abb, w[37] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0;
t0 = Sha256T0( g, h, a, b, 0x08000000 << 4 | 0x01c2c92e, w[38] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0;
t0 = Sha256T0( f, g, h, a, 0x09000000 << 4 | 0x02722c85, w[39] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0;
t0 = Sha256T0( e, f, g, h, 0x0a000000 << 4 | 0x02bfe8a1, w[40] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0;
t0 = Sha256T0( d, e, f, g, 0x0a000000 << 4 | 0x081a664b, w[41] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0;
t0 = Sha256T0( c, d, e, f, 0x0c000000 << 4 | 0x024b8b70, w[42] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0;
t0 = Sha256T0( b, c, d, e, 0x0c000000 << 4 | 0x076c51a3, w[43] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0;
t0 = Sha256T0( a, b, c, d, 0x0d000000 << 4 | 0x0192e819, w[44] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0;
t0 = Sha256T0( h, a, b, c, 0x0d000000 << 4 | 0x06990624, w[45] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0;
t0 = Sha256T0( g, h, a, b, 0x0f000000 << 4 | 0x040e3585, w[46] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0;
t0 = Sha256T0( f, g, h, a, 0x106aa070, w[47] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0;
// 48 to 63
t0 = Sha256T0( e, f, g, h, 0x19a4c116, w[48] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0;
t0 = Sha256T0( d, e, f, g, 0x1e376c08, w[49] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0;
t0 = Sha256T0( c, d, e, f, 0x2748774c, w[50] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0;
t0 = Sha256T0( b, c, d, e, 0x34b0bcb5, w[51] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0;
t0 = Sha256T0( a, b, c, d, 0x391c0cb3, w[52] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0;
t0 = Sha256T0( h, a, b, c, 0x4ed8aa4a, w[53] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0;
t0 = Sha256T0( g, h, a, b, 0x5b9cca4f, w[54] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0;
t0 = Sha256T0( f, g, h, a, 0x682e6ff3, w[55] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0;
t0 = Sha256T0( e, f, g, h, 0x748f82ee, w[56] ); t1 = Sha256T1( a, b, c );
h = t0 + t1; d += t0;
t0 = Sha256T0( d, e, f, g, 0x78a5636f, w[57] ); t1 = Sha256T1( h, a, b );
g = t0 + t1; c += t0;
t0 = Sha256T0( c, d, e, f, 0x08000000 << 4 | 0x04c87814, w[58] ); t1 = Sha256T1( g, h, a );
f = t0 + t1; b += t0;
t0 = Sha256T0( b, c, d, e, 0x08000000 << 4 | 0x0cc70208, w[59] ); t1 = Sha256T1( f, g, h );
e = t0 + t1; a += t0;
t0 = Sha256T0( a, b, c, d, 0x09000000 << 4 | 0x00befffa, w[60] ); t1 = Sha256T1( e, f, g );
d = t0 + t1; h += t0;
t0 = Sha256T0( h, a, b, c, 0x0a000000 << 4 | 0x04506ceb, w[61] ); t1 = Sha256T1( d, e, f );
c = t0 + t1; g += t0;
t0 = Sha256T0( g, h, a, b, 0x0b000000 << 4 | 0x0ef9a3f7, w[62] ); t1 = Sha256T1( c, d, e );
b = t0 + t1; f += t0;
t0 = Sha256T0( f, g, h, a, 0x0c000000 << 4 | 0x067178f2, w[63] ); t1 = Sha256T1( b, c, d );
a = t0 + t1; e += t0;
h0 += a;
h1 += b;
h2 += c;
h3 += d;
h4 += e;
h5 += f;
h6 += g;
h7 += h;
i += 49;
}
return GetUnsignedHexRepresentation(h0) +
GetUnsignedHexRepresentation(h1) +
GetUnsignedHexRepresentation(h2) +
GetUnsignedHexRepresentation(h3) +
GetUnsignedHexRepresentation(h4) +
GetUnsignedHexRepresentation(h5) +
GetUnsignedHexRepresentation(h6) +
GetUnsignedHexRepresentation(h7);
}
AESScriptSHA
void InitializeHashInput(){
HashInputSize = 0;
if ( HexTable[0] == "" )
{
HexTable[0] = "0"; HexTable[1] = "1"; HexTable[2] = "2"; HexTable[3] = "3";
HexTable[4] = "4"; HexTable[5] = "5"; HexTable[6] = "6"; HexTable[7] = "7";
HexTable[8] = "8"; HexTable[9] = "9"; HexTable[10] = "a"; HexTable[11] = "b";
HexTable[12] = "c"; HexTable[13] = "d"; HexTable[14] = "e"; HexTable[15] = "f";
}
}
void AddStringToHashInput(string s){
int i = 0;
int l = StringLength(s);
while ( i < l ){
HashInputData[HashInputSize] = char_to_byte(StringSub(s,i+1,i+1));
HashInputSize += 1;
i+=1;
}
}
string KRYPTCODE_AES_CTR_encryptStringSha256(string in,string password){
string input;
string output;
string myHash;
int i = 0;
int k;
//init
if (SB[0] != 99){
KRYPTCODE_AES_init();
}
if (validateKey(password))
{
KRYPTCODE_AES_expandKey(password);
} else if (KRYPTCODE_AES_key_setted == false) {
return "err";
}
input = KRYPTCODE_AES_CTR_expandString(in);
//hash
InitializeHashInput();
AddStringToHashInput(input);
myHash = GenerateSha256HashCode();
k = StringLength(input);
while (i < k){
output+= KRYPTCODE_AES_encrypt(StringSub(input,i+1,i+16));
i+=16;
}
return ascii_to_base64(output+myHash);
}
string KRYPTCODE_AES_CTR_decryptStringSha256(string myinput,string password){
string in;
string input;
string output="";
int i = 0;
int k;
string myHash;
string oldHash;
if (SB[0] != 99){
KRYPTCODE_AES_init();
}
if (validateKey(password))
{
KRYPTCODE_AES_expandKey(password);
} else if (KRYPTCODE_AES_key_setted == false) {
return "";
}
in = base64_to_ascii(myinput);
k = StringLength(in);
input = StringSub(in,1,k-64);
if (StringLength(input)%16 !=0){
return "err";
}
oldHash = StringSub(in,k-63,k);
while (i < k-64){
output+= KRYPTCODE_AES_decrypt(StringSub(input,i+1,i+16));
i+=16;
}
//hash
InitializeHashInput();
AddStringToHashInput(output);
myHash = GenerateSha256HashCode();
if (myHash == oldHash){
return KRYPTCODE_AES_CTR_shrinkString(output);
}else {
return "err";
}
}
AESScript
AESScript
string KRYPTCODE_result="";
const int blockSize = 16;
bool KRYPTCODE_AES_key_setted = false;
int[256] SB;
int[256] SBInv;
int[16] ShiftRowTab;
int[16] ShiftRowTabInv;
int[256] xtime;
int [176] expanded_key ;
int[128] [17] temp_storage;
const int storage_size = 128;
int last_temp_index = -1;
bool validateEncryptedData(string data){
if (StringLength(data) != 16) {
return false;
}
return true;
}
bool validateKey(string key){
if (StringLength(key) != 16) {
return false;
}
return true;
}
//allocate a 16byte data block in virtual memory
int KRYPTCODE_AES_allocate() {
int i = (last_temp_index+1)%storage_size;
int b;
while ( i != last_temp_index) {
if (temp_storage[i][16] == 0){
temp_storage[i][16] = 1;
last_temp_index = i;
return i;
}
i = (i+1)% storage_size;
}
return i;
}
//deallocate data block in virtual memory
void KRYPTCODE_AES_deallocate(int state) {
temp_storage[state][16] = 0;
}
//convert 16byte data block to 16byte virtual memory
void KRYPTCODE_AES_setOriData(int block, string data)
{
int i = 0;
while ( i < 16) {
temp_storage[block][i] = char_to_byte(StringSub(data,i+1,i+1));
i+=1;
}
}
//return 16byte data block from 16byte virtual memory
string KRYPTCODE_AES_getOriData(int block)
{
string result = "";
int i = 0;
while ( i < 16) {
result += byte_to_char(temp_storage[block][i]);
i+=1;
}
return result;
}
void KRYPTCODE_AES_subBytes( int state, bool inv) {
int i;
if (inv){
i = 0;
while (i<16){
temp_storage[state][i] = SBInv[temp_storage[state][i]];
i+=1;
}
}else{
i = 0;
while (i<16){
temp_storage[state][i] = SB[temp_storage[state][i]];
i+=1;
}
}
}
void KRYPTCODE_AES_addRoundKey(int state, int rkey) {
int i = 0;
while (i<16){
temp_storage[state][i] ^= temp_storage[rkey][i];
i+=1;
}
}
void KRYPTCODE_AES_shiftRows( int state, bool inv) {
byte[16] h;
int i = 0;
while (i<16){
h[i] = temp_storage[state][i];
i+=1;
}
i = 0;
if (inv){
while (i<16){
temp_storage[state][i] = h[ShiftRowTabInv[i]];
i+=1;
}
}else{
while (i<16){
temp_storage[state][i] = h[ShiftRowTab[i]];
i+=1;
}
}
}
void KRYPTCODE_AES_mixColumns(int state) {
int i = 0;
int s0;
int s1;
int s2;
int s3;
int h;
while (i<16){
s0 = temp_storage[state][i+0];
s1 = temp_storage[state][i+1];
s2 = temp_storage[state][i+2];
s3 = temp_storage[state][i+3];
h = s0 ^ s1 ^ s2 ^ s3;
temp_storage[state][i + 0] ^= h ^ xtime[s0 ^ s1];
temp_storage[state][i + 1] ^= h ^ xtime[s1 ^ s2];
temp_storage[state][i + 2] ^= h ^ xtime[s2 ^ s3];
temp_storage[state][i + 3] ^= h ^ xtime[s3 ^ s0];
i+=4;
}
}
void KRYPTCODE_AES_mixColumnsInv(int state) {
int i = 0;
int s0;
int s1;
int s2;
int s3;
int h;
int xh;
int h1;
int h2;
while (i<16){
s0 = temp_storage[state][i+0];
s1 = temp_storage[state][i+1];
s2 = temp_storage[state][i+2];
s3 = temp_storage[state][i+3];
h = s0 ^ s1 ^ s2 ^ s3;
xh = xtime[h];
h1 = xtime[xtime[xh ^ s0 ^ s2]] ^ h;
h2 = xtime[xtime[xh ^ s1 ^ s3]] ^ h;
temp_storage[state][i + 0] ^= h1 ^ xtime[s0 ^ s1];
temp_storage[state][i + 1] ^= h2 ^ xtime[s1 ^ s2];
temp_storage[state][i + 2] ^= h1 ^ xtime[s2 ^ s3];
temp_storage[state][i + 3] ^= h2 ^ xtime[s3 ^ s0];
i+=4;
}
}
//copy 16byte part of the key to virtual memory
void KRYPTCODE_AES_setRkey(int rkey, int start)
{
int i = 0;
while (i<16) {
temp_storage[rkey][i]=expanded_key[start+i];
i+=1;
}
}
void KRYPTCODE_AES_init(){
//we hate galaxy script
int i = 0;
SB[0] = 99;
SB[1] = 124;
SB[2] = 119;
SB[3] = 123;
SB[4] = 242;
SB[5] = 107;
SB[6] = 111;
SB[7] = 197;
SB[8] = 48;
SB[9] = 1;
SB[10] = 103;
SB[11] = 43;
SB[12] = 254;
SB[13] = 215;
SB[14] = 171;
SB[15] = 118;
SB[16] = 202;
SB[17] = 130;
SB[18] = 201;
SB[19] = 125;
SB[20] = 250;
SB[21] = 89;
SB[22] = 71;
SB[23] = 240;
SB[24] = 173;
SB[25] = 212;
SB[26] = 162;
SB[27] = 175;
SB[28] = 156;
SB[29] = 164;
SB[30] = 114;
SB[31] = 192;
SB[32] = 183;
SB[33] = 253;
SB[34] = 147;
SB[35] = 38;
SB[36] = 54;
SB[37] = 63;
SB[38] = 247;
SB[39] = 204;
SB[40] = 52;
SB[41] = 165;
SB[42] = 229;
SB[43] = 241;
SB[44] = 113;
SB[45] = 216;
SB[46] = 49;
SB[47] = 21;
SB[48] = 4;
SB[49] = 199;
SB[50] = 35;
SB[51] = 195;
SB[52] = 24;
SB[53] = 150;
SB[54] = 5;
SB[55] = 154;
SB[56] = 7;
SB[57] = 18;
SB[58] = 128;
SB[59] = 226;
SB[60] = 235;
SB[61] = 39;
SB[62] = 178;
SB[63] = 117;
SB[64] = 9;
SB[65] = 131;
SB[66] = 44;
SB[67] = 26;
SB[68] = 27;
SB[69] = 110;
SB[70] = 90;
SB[71] = 160;
SB[72] = 82;
SB[73] = 59;
SB[74] = 214;
SB[75] = 179;
SB[76] = 41;
SB[77] = 227;
SB[78] = 47;
SB[79] = 132;
SB[80] = 83;
SB[81] = 209;
SB[82] = 0;
SB[83] = 237;
SB[84] = 32;
SB[85] = 252;
SB[86] = 177;
SB[87] = 91;
SB[88] = 106;
SB[89] = 203;
SB[90] = 190;
SB[91] = 57;
SB[92] = 74;
SB[93] = 76;
SB[94] = 88;
SB[95] = 207;
SB[96] = 208;
SB[97] = 239;
SB[98] = 170;
SB[99] = 251;
SB[100] = 67;
SB[101] = 77;
SB[102] = 51;
SB[103] = 133;
SB[104] = 69;
SB[105] = 249;
SB[106] = 2;
SB[107] = 127;
SB[108] = 80;
SB[109] = 60;
SB[110] = 159;
SB[111] = 168;
SB[112] = 81;
SB[113] = 163;
SB[114] = 64;
SB[115] = 143;
SB[116] = 146;
SB[117] = 157;
SB[118] = 56;
SB[119] = 245;
SB[120] = 188;
SB[121] = 182;
SB[122] = 218;
SB[123] = 33;
SB[124] = 16;
SB[125] = 255;
SB[126] = 243;
SB[127] = 210;
SB[128] = 205;
SB[129] = 12;
SB[130] = 19;
SB[131] = 236;
SB[132] = 95;
SB[133] = 151;
SB[134] = 68;
SB[135] = 23;
SB[136] = 196;
SB[137] = 167;
SB[138] = 126;
SB[139] = 61;
SB[140] = 100;
SB[141] = 93;
SB[142] = 25;
SB[143] = 115;
SB[144] = 96;
SB[145] = 129;
SB[146] = 79;
SB[147] = 220;
SB[148] = 34;
SB[149] = 42;
SB[150] = 144;
SB[151] = 136;
SB[152] = 70;
SB[153] = 238;
SB[154] = 184;
SB[155] = 20;
SB[156] = 222;
SB[157] = 94;
SB[158] = 11;
SB[159] = 219;
SB[160] = 224;
SB[161] = 50;
SB[162] = 58;
SB[163] = 10;
SB[164] = 73;
SB[165] = 6;
SB[166] = 36;
SB[167] = 92;
SB[168] = 194;
SB[169] = 211;
SB[170] = 172;
SB[171] = 98;
SB[172] = 145;
SB[173] = 149;
SB[174] = 228;
SB[175] = 121;
SB[176] = 231;
SB[177] = 200;
SB[178] = 55;
SB[179] = 109;
SB[180] = 141;
SB[181] = 213;
SB[182] = 78;
SB[183] = 169;
SB[184] = 108;
SB[185] = 86;
SB[186] = 244;
SB[187] = 234;
SB[188] = 101;
SB[189] = 122;
SB[190] = 174;
SB[191] = 8;
SB[192] = 186;
SB[193] = 120;
SB[194] = 37;
SB[195] = 46;
SB[196] = 28;
SB[197] = 166;
SB[198] = 180;
SB[199] = 198;
SB[200] = 232;
SB[201] = 221;
SB[202] = 116;
SB[203] = 31;
SB[204] = 75;
SB[205] = 189;
SB[206] = 139;
SB[207] = 138;
SB[208] = 112;
SB[209] = 62;
SB[210] = 181;
SB[211] = 102;
SB[212] = 72;
SB[213] = 3;
SB[214] = 246;
SB[215] = 14;
SB[216] = 97;
SB[217] = 53;
SB[218] = 87;
SB[219] = 185;
SB[220] = 134;
SB[221] = 193;
SB[222] = 29;
SB[223] = 158;
SB[224] = 225;
SB[225] = 248;
SB[226] = 152;
SB[227] = 17;
SB[228] = 105;
SB[229] = 217;
SB[230] = 142;
SB[231] = 148;
SB[232] = 155;
SB[233] = 30;
SB[234] = 135;
SB[235] = 233;
SB[236] = 206;
SB[237] = 85;
SB[238] = 40;
SB[239] = 223;
SB[240] = 140;
SB[241] = 161;
SB[242] = 137;
SB[243] = 13;
SB[244] = 191;
SB[245] = 230;
SB[246] = 66;
SB[247] = 104;
SB[248] = 65;
SB[249] = 153;
SB[250] = 45;
SB[251] = 15;
SB[252] = 176;
SB[253] = 84;
SB[254] = 187;
SB[255] = 22;
SBInv[0] = 82;
SBInv[1] = 9;
SBInv[2] = 106;
SBInv[3] = 213;
SBInv[4] = 48;
SBInv[5] = 54;
SBInv[6] = 165;
SBInv[7] = 56;
SBInv[8] = 191;
SBInv[9] = 64;
SBInv[10] = 163;
SBInv[11] = 158;
SBInv[12] = 129;
SBInv[13] = 243;
SBInv[14] = 215;
SBInv[15] = 251;
SBInv[16] = 124;
SBInv[17] = 227;
SBInv[18] = 57;
SBInv[19] = 130;
SBInv[20] = 155;
SBInv[21] = 47;
SBInv[22] = 255;
SBInv[23] = 135;
SBInv[24] = 52;
SBInv[25] = 142;
SBInv[26] = 67;
SBInv[27] = 68;
SBInv[28] = 196;
SBInv[29] = 222;
SBInv[30] = 233;
SBInv[31] = 203;
SBInv[32] = 84;
SBInv[33] = 123;
SBInv[34] = 148;
SBInv[35] = 50;
SBInv[36] = 166;
SBInv[37] = 194;
SBInv[38] = 35;
SBInv[39] = 61;
SBInv[40] = 238;
SBInv[41] = 76;
SBInv[42] = 149;
SBInv[43] = 11;
SBInv[44] = 66;
SBInv[45] = 250;
SBInv[46] = 195;
SBInv[47] = 78;
SBInv[48] = 8;
SBInv[49] = 46;
SBInv[50] = 161;
SBInv[51] = 102;
SBInv[52] = 40;
SBInv[53] = 217;
SBInv[54] = 36;
SBInv[55] = 178;
SBInv[56] = 118;
SBInv[57] = 91;
SBInv[58] = 162;
SBInv[59] = 73;
SBInv[60] = 109;
SBInv[61] = 139;
SBInv[62] = 209;
SBInv[63] = 37;
SBInv[64] = 114;
SBInv[65] = 248;
SBInv[66] = 246;
SBInv[67] = 100;
SBInv[68] = 134;
SBInv[69] = 104;
SBInv[70] = 152;
SBInv[71] = 22;
SBInv[72] = 212;
SBInv[73] = 164;
SBInv[74] = 92;
SBInv[75] = 204;
SBInv[76] = 93;
SBInv[77] = 101;
SBInv[78] = 182;
SBInv[79] = 146;
SBInv[80] = 108;
SBInv[81] = 112;
SBInv[82] = 72;
SBInv[83] = 80;
SBInv[84] = 253;
SBInv[85] = 237;
SBInv[86] = 185;
SBInv[87] = 218;
SBInv[88] = 94;
SBInv[89] = 21;
SBInv[90] = 70;
SBInv[91] = 87;
SBInv[92] = 167;
SBInv[93] = 141;
SBInv[94] = 157;
SBInv[95] = 132;
SBInv[96] = 144;
SBInv[97] = 216;
SBInv[98] = 171;
SBInv[99] = 0;
SBInv[100] = 140;
SBInv[101] = 188;
SBInv[102] = 211;
SBInv[103] = 10;
SBInv[104] = 247;
SBInv[105] = 228;
SBInv[106] = 88;
SBInv[107] = 5;
SBInv[108] = 184;
SBInv[109] = 179;
SBInv[110] = 69;
SBInv[111] = 6;
SBInv[112] = 208;
SBInv[113] = 44;
SBInv[114] = 30;
SBInv[115] = 143;
SBInv[116] = 202;
SBInv[117] = 63;
SBInv[118] = 15;
SBInv[119] = 2;
SBInv[120] = 193;
SBInv[121] = 175;
SBInv[122] = 189;
SBInv[123] = 3;
SBInv[124] = 1;
SBInv[125] = 19;
SBInv[126] = 138;
SBInv[127] = 107;
SBInv[128] = 58;
SBInv[129] = 145;
SBInv[130] = 17;
SBInv[131] = 65;
SBInv[132] = 79;
SBInv[133] = 103;
SBInv[134] = 220;
SBInv[135] = 234;
SBInv[136] = 151;
SBInv[137] = 242;
SBInv[138] = 207;
SBInv[139] = 206;
SBInv[140] = 240;
SBInv[141] = 180;
SBInv[142] = 230;
SBInv[143] = 115;
SBInv[144] = 150;
SBInv[145] = 172;
SBInv[146] = 116;
SBInv[147] = 34;
SBInv[148] = 231;
SBInv[149] = 173;
SBInv[150] = 53;
SBInv[151] = 133;
SBInv[152] = 226;
SBInv[153] = 249;
SBInv[154] = 55;
SBInv[155] = 232;
SBInv[156] = 28;
SBInv[157] = 117;
SBInv[158] = 223;
SBInv[159] = 110;
SBInv[160] = 71;
SBInv[161] = 241;
SBInv[162] = 26;
SBInv[163] = 113;
SBInv[164] = 29;
SBInv[165] = 41;
SBInv[166] = 197;
SBInv[167] = 137;
SBInv[168] = 111;
SBInv[169] = 183;
SBInv[170] = 98;
SBInv[171] = 14;
SBInv[172] = 170;
SBInv[173] = 24;
SBInv[174] = 190;
SBInv[175] = 27;
SBInv[176] = 252;
SBInv[177] = 86;
SBInv[178] = 62;
SBInv[179] = 75;
SBInv[180] = 198;
SBInv[181] = 210;
SBInv[182] = 121;
SBInv[183] = 32;
SBInv[184] = 154;
SBInv[185] = 219;
SBInv[186] = 192;
SBInv[187] = 254;
SBInv[188] = 120;
SBInv[189] = 205;
SBInv[190] = 90;
SBInv[191] = 244;
SBInv[192] = 31;
SBInv[193] = 221;
SBInv[194] = 168;
SBInv[195] = 51;
SBInv[196] = 136;
SBInv[197] = 7;
SBInv[198] = 199;
SBInv[199] = 49;
SBInv[200] = 177;
SBInv[201] = 18;
SBInv[202] = 16;
SBInv[203] = 89;
SBInv[204] = 39;
SBInv[205] = 128;
SBInv[206] = 236;
SBInv[207] = 95;
SBInv[208] = 96;
SBInv[209] = 81;
SBInv[210] = 127;
SBInv[211] = 169;
SBInv[212] = 25;
SBInv[213] = 181;
SBInv[214] = 74;
SBInv[215] = 13;
SBInv[216] = 45;
SBInv[217] = 229;
SBInv[218] = 122;
SBInv[219] = 159;
SBInv[220] = 147;
SBInv[221] = 201;
SBInv[222] = 156;
SBInv[223] = 239;
SBInv[224] = 160;
SBInv[225] = 224;
SBInv[226] = 59;
SBInv[227] = 77;
SBInv[228] = 174;
SBInv[229] = 42;
SBInv[230] = 245;
SBInv[231] = 176;
SBInv[232] = 200;
SBInv[233] = 235;
SBInv[234] = 187;
SBInv[235] = 60;
SBInv[236] = 131;
SBInv[237] = 83;
SBInv[238] = 153;
SBInv[239] = 97;
SBInv[240] = 23;
SBInv[241] = 43;
SBInv[242] = 4;
SBInv[243] = 126;
SBInv[244] = 186;
SBInv[245] = 119;
SBInv[246] = 214;
SBInv[247] = 38;
SBInv[248] = 225;
SBInv[249] = 105;
SBInv[250] = 20;
SBInv[251] = 99;
SBInv[252] = 85;
SBInv[253] = 33;
SBInv[254] = 12;
SBInv[255] = 125;
ShiftRowTab[0] = 0;
ShiftRowTab[1] = 5;
ShiftRowTab[2] = 10;
ShiftRowTab[3] = 15;
ShiftRowTab[4] = 4;
ShiftRowTab[5] = 9;
ShiftRowTab[6] = 14;
ShiftRowTab[7] = 3;
ShiftRowTab[8] = 8;
ShiftRowTab[9] = 13;
ShiftRowTab[10] = 2;
ShiftRowTab[11] = 7;
ShiftRowTab[12] = 12;
ShiftRowTab[13] = 1;
ShiftRowTab[14] = 6;
ShiftRowTab[15] = 11;
i = 0;
while (i<storage_size){
temp_storage[i][16] = 0;
i+=1;
}
i = 0;
while (i<16){
ShiftRowTabInv[ShiftRowTab[i]] = i;
i+=1;
}
i = 0;
while (i<128){
xtime[i] = i << 1;
xtime[128 + i] = (i << 1) ^ 0x0000001b;
i+=1;
}
}
//AES_ExpandKey: expand a cipher key. Depending on the desired encryption
//strength of 128 bits 'key' has to be a ascii string of length
//32, respectively. The key expansion is done "in place", meaning
//that the array 'key' is modified.
bool KRYPTCODE_AES_expandKey(string input){
int i = 0;
int k = 0;
int kl = 16;
int ks = 16*(10+1); //12
int rcon = 1;
int [4] temp;
if ((StringLength(input) != 16) || (check_string(input) == false )){
return false;
}
//create 128 bit key from input, expanded key will be 176 byte long
while ( i < 16) {
expanded_key[i] = char_to_byte(StringSub(input,i+1,i+1));
i+=1;
}
i = kl;
while (i < ks ) {
temp[0] = expanded_key[i-4];
temp[1] = expanded_key[i-3];
temp[2] = expanded_key[i-2];
temp[3] = expanded_key[i-1];
if (ModI(i , kl) == 0) {
temp[0] = SB[temp[1]] ^ rcon;
temp[1] = SB[temp[2]];
temp[2] = SB[temp[3]];
temp[3] = SB[temp[0]];
rcon <<= 1;
if (rcon >= 256){
rcon ^= 0x11b;
}
}
else if ((kl > 24) && (i % kl == 16)){
temp[0] = SB[temp[0]];
temp[1] = SB[temp[1]];
temp[2] = SB[temp[2]];
temp[3] = SB[temp[3]];
}
k=0;
while(k < 4){
expanded_key[i + k] = expanded_key[i + k - kl] ^ temp[k];
k+=1;
}
i+=4;
}
KRYPTCODE_AES_key_setted = true;
return true;
}
//AES_Encrypt: encrypt the 16 byte array 'block' with the previously
//expanded global key 'expanded_key'.
string KRYPTCODE_AES_encrypt(string input){
string result;
int l = 176;
int block = KRYPTCODE_AES_allocate();
int rkey = KRYPTCODE_AES_allocate();
int i;
KRYPTCODE_AES_setOriData(block,input);
KRYPTCODE_AES_setRkey(rkey,0);
//write rkey and block
KRYPTCODE_AES_addRoundKey(block, rkey);
i = 16;
while( i < l - 16) {
KRYPTCODE_AES_subBytes(block,false);
KRYPTCODE_AES_shiftRows(block,false);
KRYPTCODE_AES_mixColumns(block);
//change rkey
KRYPTCODE_AES_setRkey(rkey,i);
KRYPTCODE_AES_addRoundKey(block, rkey);
i += 16;
}
KRYPTCODE_AES_subBytes(block,false);
KRYPTCODE_AES_shiftRows(block,false);
//change rkey
KRYPTCODE_AES_setRkey(rkey,i);
KRYPTCODE_AES_addRoundKey(block, rkey);
result = KRYPTCODE_AES_getOriData(block);
KRYPTCODE_AES_deallocate(rkey);
KRYPTCODE_AES_deallocate(block);
return result;
}
//AES_Decrypt: decrypt the 16 byte array 'block' with the previously
//expanded global key 'expanded_key'.
string KRYPTCODE_AES_decrypt(string input){
string result;
int l = 176;
int block = KRYPTCODE_AES_allocate();
int rkey = KRYPTCODE_AES_allocate();
int i = l -32;
KRYPTCODE_AES_setOriData(block,input);
KRYPTCODE_AES_setRkey(rkey,l-16);
KRYPTCODE_AES_addRoundKey(block, rkey );
KRYPTCODE_AES_shiftRows(block, true);//1=inverse operation
KRYPTCODE_AES_subBytes(block, true);//1=inverse operation
while(i >= 16) {
KRYPTCODE_AES_setRkey(rkey,i);
KRYPTCODE_AES_addRoundKey(block, rkey);
KRYPTCODE_AES_mixColumnsInv(block);
KRYPTCODE_AES_shiftRows(block, true);//1=inverse operation
KRYPTCODE_AES_subBytes(block, true);//1=inverse operation
i -= 16;
}
KRYPTCODE_AES_setRkey(rkey,0);
KRYPTCODE_AES_addRoundKey(block, rkey);
KRYPTCODE_AES_deallocate(rkey);
result = KRYPTCODE_AES_getOriData(block);
KRYPTCODE_AES_deallocate(block);
return result;
}
//end---------------------AES Core part--------------------------
//------------------------AES CTR part---------------------------
//http://en.wikipedia.org/wiki/Counter_mode#Counter_.28CTR.29
string KRYPTCODE_AES_CTR_encryptString(string in,string password){
string input;
string output;
int i;
int k;
//init
if (SB[0] != 99){
KRYPTCODE_AES_init();
}
if (validateKey(password))
{
KRYPTCODE_AES_expandKey(password);
} else if (KRYPTCODE_AES_key_setted == false) {
return "err";
}
input = KRYPTCODE_AES_CTR_expandString(in);
k = StringLength(input);
i = 0;
while (i < k){
output+= KRYPTCODE_AES_encrypt(StringSub(input,i+1,i+16));
i+=16;
}
return output;
}
string KRYPTCODE_AES_CTR_decryptString(string in,string password){
string output;
int i = 0;
int k;
if (SB[0] != 99){
KRYPTCODE_AES_init();
}
if (validateKey(password))
{
KRYPTCODE_AES_expandKey(password);
} else if (KRYPTCODE_AES_key_setted == false) {
return "err";
}
if (StringLength(in)%16 !=0){
return "err";
}
k = StringLength(in);
while (i < k){
output+= KRYPTCODE_AES_decrypt(StringSub(in,i+1,i+16));
i+=16;
}
return KRYPTCODE_AES_CTR_shrinkString(output);
}
AESTest
AESTest
ASCIITest
ASCIITest
KryptCodeEncryptString
KryptCodeEncryptString
KyptCodeDecryptString
KyptCodeDecryptString
AESTest2
Base64Test
KRYPTCODE_AES_init();
KRYPTCODE_AES_expandKey("1234567890123456");
lv_output = KRYPTCODE_AES_CTR_expandString(lv_input);
lv_a = KRYPTCODE_AES_allocate();
KRYPTCODE_AES_setOriData(lv_a,"1234567890123456");
lv_output =KRYPTCODE_AES_getOriData(lv_a);
lv_output = ascii_to_base64(lv_input);
return KRYPTCODE_AES_CTR_encryptStringSha256(lp_input, lp_password);
lv_output = base64_to_ascii(lv_output);
return KRYPTCODE_AES_CTR_decryptStringSha256(lp_input, lp_password);
lv_output = KRYPTCODE_AES_CTR_shrinkString(lv_output);
Ich bin-gluecklich
Ich bin-glueckli
ICH23EDe3d7890hg
ICH23EDe3d7890hg
output:
0
Ich
0
Base64 test:
------------
input:
ICH23EDe3d7890hg
------------
start AES test:
0
0
ASCII test:
output:
input
Untitled Parameter 001
password
Untitled Parameter 001
input
Untitled Parameter 001
password
Untitled Parameter 001
password
password
password
password
a
a
password
password
input
input
b
output
output
input
input
input
input
b
output
output
output
output