/// <summary>Generate a random SHA3 Hash/Key as String.</summary> /// <remarks>Generate a random SHA3 Hash/Key as String.</remarks> /// <param name="keySize">Size of hash. Allowed are 224, 256, 384 and 512.</param> /// <returns>SHA3 hash as Hex String</returns> public virtual string generateKey(int keySize) { SecureRandom scRandom = new SecureRandom(); byte[] randomPW = new byte[40]; scRandom.nextBytes(randomPW); KeyGenSHA3.init(keySize); KeyGenSHA3.update(randomPW, randomPW.Length * 8); string output = CryptobyHelper.bytesToHexString(KeyGenSHA3.getHash()); return output; }
/// <summary>Encrypt a byte array in blocks with AES algorithm in CBC mode.</summary> /// <remarks>Encrypt a byte array in blocks with AES algorithm in CBC mode.</remarks> /// <param name="plainInput">Plain byte array to encrypt</param> /// <param name="key">256 Bit key to encrypt plainInput</param> /// <returns>Return encrypted byte array</returns> public virtual byte[] encrypt(byte[] plainInput, byte[] key) { int inputLength = plainInput.Length; int percentProgress; int prevPercent = -1; byte[] inputLengthByte = ByteBuffer.allocate (4).order (ByteOrder.BIG_ENDIAN).putInt (inputLength).array (); int restInput = plainInput.Length % nBytes; byte[] exKey = initKeyExpand(key); byte[] cipher = new byte[nBytes]; byte[] cryptOutput = new byte[(inputLength - restInput) + nBytes * 2]; int outputLength = cryptOutput.Length; byte[] initVector = new byte[nBytes]; byte[] nextBlock = new byte[nBytes]; SecureRandom scRandom = new SecureRandom(); // Copy plaintext Array into crypt Array System.Array.Copy(plainInput, 0, cryptOutput, 0, inputLength); // Fill Initialization Vector with Random Bytes scRandom.nextBytes(initVector); // Copy first Input Block to nextBlock System.Array.Copy(cryptOutput, 0, nextBlock, 0, nBytes); // XOR Random initVektor with first Input Block nextBlock = CryptobyHelper.xorByteArrays(nextBlock, initVector); // Copy xored prevBlock into first Input Block System.Array.Copy(nextBlock, 0, cryptOutput, 0, nBytes); // Encrypt last BlockArray initVector = encryptCipher (initVector, exKey); // Add the initVector Array in to last BlockArray and encrypt it System.Array.Copy(initVector, 0, cryptOutput, outputLength - nBytes, nBytes); // Add in the first Byte after CryptText the origin length of plaintext Array System.Array.Copy(inputLengthByte, 0, cryptOutput, outputLength - nBytes * 2, 4); // Encrypt every Block in CBC Mode for (int i = 0; i < outputLength - nBytes; i += nBytes) { // Convert i to percent for ProgressBar // percentProgress = (int)(((float)i / (float)(outputLength - nBytes)) * 100); // // Print ProgressBar // if (percentProgress > prevPercent) // { // CryptobyHelper.printProgressBar(percentProgress); // } // prevPercent = percentProgress; // Copy current block in to Cipher Array System.Array.Copy(nextBlock, 0, cipher, 0, nBytes); // Encrypt Cipher cipher = this.encryptCipher(cipher, exKey); // CBC Mode: XOR next PlainBlock with encrypted Cipher if (i + nBytes < outputLength) { System.Array.Copy(cryptOutput, i + nBytes, nextBlock, 0, nBytes); nextBlock = CryptobyHelper.xorByteArrays(nextBlock, cipher); } // Copy Cipher back in decryptOutput Array System.Array.Copy(cipher, 0, cryptOutput, i, nBytes); } // CryptobyHelper.printProgressBar(100); return cryptOutput; }
/// <summary>Generate a random SHA3 Hash/Key as byte array.</summary> /// <remarks>Generate a random SHA3 Hash/Key as byte array.</remarks> /// <param name="keySize">Size of hash. Allowed are 224, 256, 384 and 512.</param> /// <returns>SHA3 hash as byte array</returns> public virtual byte[] generateKeyByte(int keySize) { SecureRandom scRandom = new SecureRandom(); byte[] randomPW = new byte[40]; scRandom.nextBytes(randomPW); KeyGenSHA3.init(keySize); KeyGenSHA3.update(randomPW, randomPW.Length * 8); byte[] output = KeyGenSHA3.getHash(); return output; }