Inheritance: java.util.Random
示例#1
0
		[TestFixtureSetUp] public void Init()
		{ 
			secRand = new SecureRandom ();
			big512 = new byte[64];
			big1024 = new byte[128];
			big2048 = new byte[256];
		}
示例#2
0
 /// <summary>Constructor sets variables and initializes the SecureRandom object.</summary>
 /// <remarks>Constructor sets variables and initializes the SecureRandom object.</remarks>
 /// <param name="appCore">Input CryptobyCore object to get a primetest object</param>
 /// <param name="keyBitSize">
 /// With the half size of this key will generate be a prime
 /// number in the run method
 /// </param>
 public GenPrimeThread(CryptobyCore appCore, int keyBitSize)
 {
     scRandom = new SecureRandom();
     halfKeyBitSize = keyBitSize / 2;
     keyByteSize = halfKeyBitSize / 8;
     core = appCore;
 }
示例#3
0
 /// <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;
 }
示例#4
0
 /// <summary>Test the input number to be probability a prime number.</summary>
 /// <remarks>Test the input number to be probability a prime number.</remarks>
 /// <param name="number">Number for prime test</param>
 /// <returns>
 /// Return true is a probability a prime and false if sure not a
 /// prime
 /// </returns>
 public virtual bool isPrime(BigInteger number)
 {
     bool result = false;
     SecureRandom random = new SecureRandom();
     for (int i = 0; i < this.rounds; i++)
     {
         result = runMillerRabin(number, random);
     }
     if (result == false)
     {
         this.probability = 0;
     }
     else
     {
         this.probability = this.calcProbability(this.rounds);
     }
     return result;
 }
示例#5
0
 /// <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;
 }
 public void init(KeyManager[] km, TrustManager[] tm, SecureRandom sr)
 {
 }
示例#7
0
        /// <summary>Encrypt plainInput with publicKey in blocks.</summary>
        /// <remarks>
        /// Encrypt plainInput with publicKey in blocks. The first encrypted block will
        /// be xored with a Initial Vektor and every next encrypted block will be xored
        /// with the previous block and written to output array. Encrypted Initial
        /// Vector stored in last two bytes of output byte array.
        /// </remarks>
        /// <param name="plainInput">Byte Array to encrypt in RSA Mode</param>
        /// <param name="publicKey">RSA Public Key as Byte Array</param>
        /// <returns>RSA Encrypted plainInput as Byte Array</returns>
        public virtual byte[] encrypt(byte[] plainInput, byte[] publicKey)
        {
            BigInteger n = new BigInteger(publicKey);
            int keySize = publicKey.Length;
            int dataBlockSize = keySize + 2 * (keySize / 128);

            int plainBlockSize = keySize - 2;
            int wholeLen = plainInput.Length;
            int cryptBlocksLen = (wholeLen / plainBlockSize) * dataBlockSize;

            int dataBlocksLen = cryptBlocksLen + 3 * dataBlockSize;

            int plainBlocksLen = (wholeLen / plainBlockSize) * plainBlockSize;
            byte[] cryptOutput = new byte[dataBlocksLen];
            if (wholeLen > keySize)
            {
                int percentProgress;
                int prevPercent = -1;
                int plainPlusOneBlockSize = plainBlockSize + 1;
                int rest = wholeLen - plainBlocksLen;
                int halfOfVektor;
                byte[] dataBlock = new byte[dataBlockSize];
                byte[] cryptBlock;
                byte[] plainBlock = new byte[plainBlockSize];
                byte[] plusOnePlainBlock = new byte[plainPlusOneBlockSize];
                byte[] initVektorBlock;
                byte[] firstVektorBlock;
                byte[] secVektorBlock;
                byte[] cryptBlockSize;
                byte[] one = BigInteger.ONE.toByteArray();
                do
                {
                    SecureRandom rnd = new SecureRandom();
                    halfOfVektor = dataBlockSize / 2;
                    do
                    {
                        firstVektorBlock = new BigInteger(halfOfVektor * 8 - 1, rnd).toByteArray();
                        secVektorBlock = new BigInteger(halfOfVektor * 8 - 1, rnd).toByteArray();
                    }
                    while (firstVektorBlock.Length != halfOfVektor || secVektorBlock.Length != halfOfVektor);
                    initVektorBlock = new byte[dataBlockSize];
                    System.Array.Copy(firstVektorBlock, 0, initVektorBlock, 0, halfOfVektor);
                    System.Array.Copy(secVektorBlock, 0, initVektorBlock, halfOfVektor, halfOfVektor);
                    firstVektorBlock = encryptBlock(firstVektorBlock, n);
                    secVektorBlock = encryptBlock(secVektorBlock, n);
                }
                while (firstVektorBlock.Length != keySize || secVektorBlock.Length != keySize);
                byte[] prevBlock = new byte[dataBlockSize];
                System.Array.Copy(initVektorBlock, 0, prevBlock, 0, dataBlockSize);
                byte[] nextBlock = new byte[dataBlockSize];
                int j = 0;
                for (int i = 0; i < plainBlocksLen; i += plainBlockSize)
                {
                    // Convert i to percent for ProgressBar
                    percentProgress = (int)(((float)i / (float)plainBlocksLen) * 100);
                    // Print ProgressBar
            //					if (percentProgress > prevPercent)
            //					{
            //						CryptobyHelper.printProgressBar(percentProgress);
            //					}
                    prevPercent = percentProgress;
                    // Copy Part of PlainInput in to Block
                    System.Array.Copy(plainInput, i, plainBlock, 0, plainBlockSize);
                    // Add a One Byte into first Byte of Plain Array
                    System.Array.Copy(one, 0, plusOnePlainBlock, 0, one.Length);
                    System.Array.Copy(plainBlock, 0, plusOnePlainBlock, 1, plainBlockSize);
                    // Encrypt Block
                    cryptBlock = encryptBlock(plusOnePlainBlock, n);
                    // Copy Crypt Block in to extended DataBlock
                    System.Array.Copy(cryptBlock, 0, dataBlock, 0, cryptBlock.Length);
                    // Copy in last Byte of dataBlock the Size of cryptBlock
                    cryptBlockSize = BigInteger.valueOf(cryptBlock.Length - keySize).toByteArray();
                    System.Array.Copy(cryptBlockSize, 0, dataBlock, dataBlock.Length - 1, cryptBlockSize
                        .Length);
                    System.Array.Copy(dataBlock, 0, nextBlock, 0, dataBlockSize);
                    // XOR dataBlock with prevBlock
                    dataBlock = CryptobyHelper.xorByteArrays(dataBlock, prevBlock);
                    System.Array.Copy(nextBlock, 0, prevBlock, 0, dataBlockSize);
                    // Copy xored dataBlock to Output Array
                    System.Array.Copy(dataBlock, 0, cryptOutput, j, dataBlockSize);
                    j += dataBlockSize;
                }
                if (rest != 0)
                {
                    // crypt rest of PlainInput
                    plainBlock = new byte[rest];
                    plusOnePlainBlock = new byte[rest + 1];
                    dataBlock = new byte[dataBlockSize];
                    System.Array.Copy(plainInput, plainBlocksLen, plainBlock, 0, plainBlock.Length);
                    // Add a One Byte in to first Byte of Plain Array
                    System.Array.Copy(one, 0, plusOnePlainBlock, 0, one.Length);
                    System.Array.Copy(plainBlock, 0, plusOnePlainBlock, 1, plainBlock.Length);
                    // Encrypt rest of PlainInput
                    cryptBlock = encryptBlock(plusOnePlainBlock, n);
                    System.Array.Copy(cryptBlock, 0, dataBlock, 0, cryptBlock.Length);
                    // Copy in last Byte of dataBlock the Size of cryptBlock
                    cryptBlockSize = BigInteger.valueOf(cryptBlock.Length - keySize).toByteArray();
                    System.Array.Copy(cryptBlockSize, 0, dataBlock, dataBlock.Length - 1, 1);
                    // XOR dataBlock with prevBlock
                    dataBlock = CryptobyHelper.xorByteArrays(dataBlock, prevBlock);
                    System.Array.Copy(dataBlock, 0, cryptOutput, dataBlocksLen - 3 * dataBlockSize, dataBlockSize
                        );
                }
                else
                {
                    dataBlock = new byte[dataBlockSize];
                    System.Array.Copy(dataBlock, 0, cryptOutput, dataBlocksLen - 3 * dataBlockSize, dataBlockSize
                        );
                }
                // Put crypted initVektor into last 2 Bytes of cryptOutput Array
                System.Array.Copy(firstVektorBlock, 0, cryptOutput, dataBlocksLen - 2 * dataBlockSize
                    , firstVektorBlock.Length);
                System.Array.Copy(secVektorBlock, 0, cryptOutput, dataBlocksLen - 1 * dataBlockSize
                    , secVektorBlock.Length);
            }
            else
            {
                cryptOutput = encryptBlock(plainInput, n);
            }
            //CryptobyHelper.printProgressBar(100);
            return cryptOutput;
        }
示例#8
0
        private static bool runMillerRabin(BigInteger number, SecureRandom random
			)
        {
            if (number.compareTo(BigInteger.valueOf(3)) <= 0)
            {
                return number.compareTo(BigInteger.ONE) != 0;
            }
            // Ensures that temp > 1 and temp < n.
            BigInteger temp = BigInteger.ZERO;
            do
            {
                temp = new BigInteger(number.bitLength() - 1, random);
            }
            while (temp.compareTo(BigInteger.ONE) <= 0);
            // Screen out n if our random number happens to share a factor with n.
            if (!number.gcd(temp).Equals(BigInteger.ONE))
            {
                return false;
            }
            // For debugging, prints out the integer to test with.
            //System.out.println("Testing with " + temp);
            BigInteger d = number.subtract(BigInteger.ONE);
            // Figure s and d Values
            int s = 0;
            while ((d.mod(TWO)).Equals(BigInteger.ZERO))
            {
                d = d.divide(TWO);
                s++;
            }
            BigInteger curValue = temp.modPow(d, number);
            // If this works out, it's a prime
            if (curValue.Equals(BigInteger.ONE))
            {
                return true;
            }
            // Otherwise, we will check to see if this value successively
            // squared ever yields -1.
            for (int r = 0; r < s; r++)
            {
                // We need to really check n-1 which is equivalent to -1.
                if (curValue.Equals(number.subtract(BigInteger.ONE)))
                {
                    return true;
                }
                else
                {
                    // Square this previous number - here I am just doubling the
                    // exponent. A more efficient implementation would store the
                    // value of the exponentiation and square it mod n.
                    curValue = curValue.modPow(TWO, number);
                }
            }
            // If none of our tests pass, we return false. The number is
            // definitively composite if we ever get here.
            return false;
        }
示例#9
0
 public void Init()
 {
     secRand = new SecureRandom ();
 }
示例#10
0
 /// <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;
 }
示例#11
0
        /// <summary>
        /// Initializes this signature object with the specified
        /// private key and source of randomness for signing operations.
        ///
        /// <para>This concrete method has been added to this previously-defined
        /// abstract class. (For backwards compatibility, it cannot be abstract.)
        ///
        /// </para>
        /// </summary>
        /// <param name="privateKey"> the private key of the identity whose signature
        /// will be generated. </param>
        /// <param name="random"> the source of randomness
        /// </param>
        /// <exception cref="InvalidKeyException"> if the key is improperly
        /// encoded, parameters are missing, and so on. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void engineInitSign(PrivateKey privateKey, SecureRandom random) throws InvalidKeyException
        protected internal virtual void EngineInitSign(PrivateKey privateKey, SecureRandom random)
        {
            this.AppRandom = random;
            EngineInitSign(privateKey);
        }