Last active
March 28, 2020 11:52
-
-
Save smtm/092b3e441feb405374c1322758140c66 to your computer and use it in GitHub Desktop.
RKSV logic as PHP implementation (wrapped into a html page with a test scenario)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>RKSV PHP Funktionen</title> | |
</head> | |
<body> | |
<h1>RKSV PHP Funktionen in PHP </h1> | |
<p> 31.1.2017 as per <a href="https://github.com/a-sit-plus/at-registrierkassen-mustercode/issues/220#issuecomment-276019670">github comment thread</a></p> | |
<?php | |
$aeskey = "WQRtiiya3hYh/Uz44Bv3x8ETl1nrH6nCdErn69g5/lU="; | |
// case 1 | |
$kassenid = "CashBox01"; | |
$belegnummer = "00000001"; | |
$umsatz = 5509; | |
// case 2 | |
//$kassenid = "CASHBOX-DEMO-1"; | |
//$belegnummer = "-Receipt-ID-571"; | |
//$umsatz = 38741; | |
$aes = new RKSVAES256CTR( $aeskey ); | |
$iv = $aes->GenerateIV( $kassenid, $belegnummer, false ); | |
$iv64 = $aes->getb64($iv); | |
$umsatz_encode = $aes->encodeUmsatz($umsatz); | |
$umsatz_encode_64 = $aes->getb64($umsatz_encode); | |
$umsatz_aes_encrypt = $aes->encryptAESUmsatz( $umsatz, $kassenid, $belegnummer ); | |
$umsatz_aes_decrypt = $aes->decryptAESUmsatz( $umsatz_aes_encrypt, $kassenid, $belegnummer ); | |
echo "<h2> Input</h2>"; | |
echo "aeskey: ".$aeskey."<br/>"; | |
echo "aesmethod: ".$aes->AESMethod."<br/>"; | |
echo "kassenid: ".$kassenid."<br/>"; | |
echo "belegnr: ".$belegnummer."<br/>"; | |
echo "umsatz: ".$umsatz."<br/><br/>"; | |
echo "<h2>Encode & Encrypt Umsatz</h2>"; | |
echo "IV: ".$iv."<br/>"; | |
echo "IV64: ".$iv64."<br/>"; | |
echo "encoded umsatz: ".$umsatz_encode."<br/>"; | |
echo "encoded umsatz 64: ".$umsatz_encode_64."<br/>"; | |
echo "encrypted umsatz: ".$umsatz_aes_encrypt."<br/>"; | |
echo "<h2>Decrypt Umsatz</h2>"; | |
echo "decrypted umsatz: ".$umsatz_aes_decrypt."<br/>"; | |
class RKSVAES256CTR | |
{ | |
//var $AESMethod = 'AES-256-ECB'; | |
var $AESMethod = 'AES-256-CFB'; | |
function __construct( $key ) { | |
$this->AESKey = $key; | |
} | |
function GenerteAESKey() { | |
$nbytes = openssl_random_pseudo_bytes(32); | |
$test = base64_encode( $nbytes ); | |
return $test; | |
} | |
function encryptAESUmsatz( $umsatz, $kassenid, $nr ) { | |
$iv = $this->GenerateIV( $kassenid, $nr ); | |
$key_bin = base64_decode( $this->AESKey ); | |
$data = $this->encodeUmsatz( $umsatz ); | |
$encoded = openssl_encrypt($data, $this->AESMethod, $key_bin, OPENSSL_ZERO_PADDING, $iv); | |
return $encoded; | |
} | |
function decryptAESUmsatz( $umsatz, $kassenid, $nr ) { | |
$iv = $this->GenerateIV( $kassenid, $nr, true ); //bytes = ok | |
$key_bin = base64_decode( $this->AESKey ); //bytes = Ok | |
$ret = openssl_decrypt( $umsatz, $this->AESMethod, $key_bin, OPENSSL_ZERO_PADDING, $iv ); | |
return $this->decodeUmsatz( $ret ); | |
} | |
function decodeUmsatz( $in ) { | |
$bytes = $this->getBytes( $in ); | |
$nbytes = array_reverse( $bytes ); | |
//http://stackoverflow.com/questions/12683833/how-to-convert-byte-array-to-integer-in-php | |
$umsatz = ($nbytes[3]<<24) + ($nbytes[2]<<16) + ($nbytes[1]<<8) + $nbytes[0]; | |
return $umsatz; | |
} | |
function encodeUmsatz( $in ) { | |
$umsatzBytes = $this->getBytes( $in ); | |
for( $i=count( $umsatzBytes ); $i<16; $i++ ) { | |
$umsatzBytes[] =0; | |
} | |
$ret = array_reverse( $umsatzBytes ); | |
$text = $this->getText2( $ret ); | |
return $text; | |
} | |
function getb64( $text ) { | |
return base64_encode($text); | |
} | |
function GenerateIV( $kassenid, $belegnummer, $debug = false ) { | |
$in = $kassenid . $belegnummer; | |
$hash = substr( hash('sha256', $in, true), 0, 16 ); | |
return $hash; | |
} | |
function getText2( $in ) { | |
//bytes to text | |
$ret = ""; | |
foreach( $in as $item ) $ret .= chr( $item ); | |
return $ret; | |
} | |
function getBytes( $in ) { | |
//text / numeric to bytes | |
if( is_numeric( $in ) ) { | |
return unpack("C*", pack("L", $in ) ); | |
} else { | |
for($i = 0; $i < strlen($in); $i++) { | |
$ret[] = ord( substr( $in, $i, 1 ) ); | |
} | |
return $ret; | |
} | |
} | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hallo
danke für die Unterstützung. Was empfehlen Sie für die Signatur?