CryptIX salausalgoritmi
CLASS.CRYPTIX.PHP
<?php
/*
TEKNISIÄ TIETOJA:
160 bitin (20 tavun) osiokoko.
160 bitin SHA1:llä muodostettu avain (raaka).
Alustusvektori lisätään avaimeen.
TURVALLISUUS:
Käyttää ECB:tä, eli heikkoa moodia.
PÄIVITYKSET:
Myöhemmin ehkä lisään enemmän moodeja, kuten:
CBC, OFB, CFB ja CTR.
MUUTA:
Nopeustesti:
~75kb/s salaus ja purku (Intel Pentium Dual-Core E5200).
~35kb/s salaus ja purku (Intel Pentium III).
Copyright (C) 2009; Joonas Lauhala.
Saat levittää, käyttää ja muokata tätä koodia vapasti,
siten että tämä kommentti säilyy.
*/
class CryptIX
{
private $key;
private function Reset()
{
$this->key = Array(
0x35a7394b, 0x77629b5b, 0xe2aa32a7, 0x98004601,
0x4192fd75, 0xf6c5bccf, 0x8323a8df, 0xad05439f,
0x5f88d93f, 0xd13af3a1, 0xbee9351b, 0x97fdfa05,
0xd0c88249, 0xe281b0c1, 0x97fdebe5, 0x3619b3ad,
0x42056bcd, 0x5f5e1af9, 0xf762dec7, 0x36422623
);
}
private function Hex2Raw( $text )
{
$output = array();
for( $i = 0; $i < strlen( $text ); $i += 2 ) {
$output[] = hexdec( substr( $text, $i, 2 ) );
}
return $output;
}
private function SetKey( $key, $iv = "" )
{
if( strlen( $iv ) <= 0 )
$this->Reset();
if( is_array( $key ) )
$key = implode( array_map( "chr", $key ), "" );
$key = $this->Hex2Raw( sha1( $key . $iv ) );
for( $i = 0; $i < count( $key ); $i++ )
$this->key[$i] ^= $key[$i];
}
private function GenerateIV()
{
$output = "";
$iv = sha1( time() + rand() );
for( $i = 0; $i < strlen( $iv ); $i += 2 )
{
$key = hexdec( substr( $iv, $i, 2 ) );
$output .= chr( $key ^ rand() );
}
return $output;
}
private function Pad( $text )
{
$length = strlen( $text );
if( ( $length % 20 ) != 0 )
{
$length = ( 19 - ( $length % 20 ) );
if( $length >= 1 )
$text .= str_repeat( "\0", $length );
$text .= chr( $length );
}
return $text;
}
private function Unpad( $text )
{
$length = strlen( $text );
if( ( $length % 20 ) == 0 )
{
$pads = ord( $text[$length-1] );
if( $pads > 0 && $pads < 20 )
{
$bool = 0;
for( $i = 1; $i <= $pads; $i++ )
{
$bool &= ord( $text[($length-1)-$i] );
if( $bool != 0 )
return $text;
}
$text = substr( $text, 0, ( $length - 1 ) - $pads );
}
}
return $text;
}
public function Encrypt( $key, $data )
{
$output = "";
$this->SetKey( $key );
$iv = $this->GenerateIV();
$data = $this->Pad( $iv . $data );
for( $b = 0; $b < ( strlen( $data ) / 20 ); $b++ )
{
if( $b != 0 )
$this->SetKey( $this->key, $iv );
$block = substr( $data, ( $b * 20 ), 20 );
for( $i = 0; $i < strlen( $block ); $i++ )
$output .= chr( ord( $block[$i] ) ^ $this->key[$i] );
}
return $output;
}
public function Decrypt( $key, $data )
{
$output = "";
$this->SetKey( $key );
for( $b = 0; $b < ( strlen( $data ) / 20 ); $b++ )
{
if( $b != 0 )
$this->SetKey( $this->key, $iv );
$block = substr( $data, ( $b * 20 ), 20 );
for( $i = 0; $i < strlen( $block ); $i++ )
$output .= chr( ord( $block[$i] ) ^ $this->key[$i] );
if( $b == 0 )
{
$iv = $output;
$output = "";
}
}
return $this->Unpad( $output );
}
}
?>TESTI.PHP
<?php include_once( "class.cryptix.php" ); define( "AVAIN", "minunavaimeni" ); $cryptix = new CryptIX; $salattu = base64_encode( $cryptix->Encrypt( AVAIN, "sehän toimii :)" ) ); $purettu = $cryptix->Decrypt( AVAIN, base64_decode( $salattu ) ); echo "<h3>CryptIX testi</h3>"; echo "<strong>Salattu: </strong>" . $salattu . "</br>"; echo "<strong>Purettu: </strong>" . $purettu . "</br>"; ?> Parannusehdotuksia otetaan vastaan, esim: - Suoritusen nopeutus - Turvallisuus seikat - Koodin ulkoasu, jne
Aihe on jo aika vanha, joten et voi enää vastata siihen.