public static byte [ ] openBox ( byte [ ] header , byte [ ] cipherText , ActorBoxKey key ) throws IntegrityException { CBCHmacBox aesCipher = new CBCHmacBox ( Crypto . createAES128 ( key . getKeyAES ( ) ) , Crypto . createSHA256 ( ) , key . getMacAES ( ) ) ; CBCHmacBox kuzCipher = new CBCHmacBox ( new KuznechikFastEngine ( key . getKeyKuz ( ) ) , new Streebog256 ( ) , key . getMacKuz ( ) ) ; byte [ ] kuzPackage = aesCipher . decryptPackage ( header , ByteStrings . substring ( cipherText , 0 , 16 ) , ByteStrings . substring ( cipherText , 16 , cipherText . length - 16 ) ) ; byte [ ] plainText = kuzCipher . decryptPackage ( header , ByteStrings . substring ( kuzPackage , 0 , 16 ) , ByteStrings . substring ( kuzPackage , 16 , kuzPackage . length - 16 ) ) ; // Validating padding int paddingSize = plainText [ plainText . length - 1 ] & 0xFF ; if ( paddingSize < 0 || paddingSize >= 16 ) { throw new IntegrityException ( "Incorrect padding!" ) ; } PKCS7Padding padding = new PKCS7Padding ( ) ; if ( ! padding . validate ( plainText , plainText . length - 1 - paddingSize , paddingSize ) ) { throw new IntegrityException ( "Padding does not isMatch!" ) ; } return ByteStrings . substring ( plainText , 0 , plainText . length - 1 - paddingSize ) ; }
Opening Encrypted box