GetInputBlockSize() public method

public GetInputBlockSize ( ) : int
return int
Ejemplo n.º 1
1
        /// <summary>
        /// The decrypt.
        /// </summary>
        /// <param name="encrypted">
        /// The encrypted.
        /// </param>
        /// <param name="pk">
        /// The pk.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string Decrypt(string encrypted, PrivateKey pk)
        {
            var keyParam = pk.GetPrivateKeyParam();
            var engine = new Pkcs1Encoding(new RsaEngine());
            engine.Init(false, keyParam);
            var blockSize = engine.GetInputBlockSize();

            byte[] bytes = Convert.FromBase64String(encrypted);
            byte[] dec = engine.ProcessBlock(bytes, 0, blockSize);
            var clear = this.ToUTF8String(dec);
            return clear;
        }
		internal string Decrypt(string cryptText, string encryptionKey, bool isPrivate)
		{
			RsaKeyParameters key;
			byte[] data = null;
			List<byte> output = new List<byte>();
			string result = null;

			try
			{
				if (isPrivate)
					key = RsaPrivateStringToRsaKey(encryptionKey);
				else
					key = RsaPublicStringToRsaKey(encryptionKey);

				data = Hex.Decode(cryptText);

				IAsymmetricBlockCipher e = new Pkcs1Encoding(new RsaEngine()).GetUnderlyingCipher();
				e.Init(false, key);

				int blockSize = e.GetInputBlockSize();

				if (data != null)
				{
					for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
					{
						int chunkSize;

						if (data.Length <= blockSize)
							chunkSize = data.Length;
						else if ((chunkPosition + blockSize) > data.Length)
							chunkSize = data.Length - chunkPosition;
						else
							chunkSize = blockSize;

						if (chunkSize <= 0)
							break;

						output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize));
					}

					result = ByteArrayToString(output.ToArray());
				}
			}
			catch (Exception ex)
			{
				Debug.Write(ex.ToString());
			}

			return result;
		}
		internal string Encrypt(string plainText, string encryptionKey, bool isPrivate)
		{
			RsaKeyParameters key;
			byte[] data = null;
			List<byte> output = new List<byte>();
			string result = null;

			try
			{
				if (isPrivate)
					key = RsaPrivateStringToRsaKey(encryptionKey);
				else
					key = RsaPublicStringToRsaKey(encryptionKey);

				data = StringToByteArray(plainText);

				IAsymmetricBlockCipher e = new Pkcs1Encoding(new RsaEngine()).GetUnderlyingCipher();
				e.Init(true, key);

				int blockSize = e.GetInputBlockSize();

				if (data != null)
				{
					for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
					{
						int chunkSize;

						if (data.Length <= blockSize)
							chunkSize = data.Length; // If we have less data then the blockSize, do it all
						else if ((chunkPosition + blockSize) > data.Length) // Do we have any remainder data
							chunkSize = data.Length - chunkPosition;
						else
							chunkSize = blockSize; // Normal process, chunk to blockSize

						// No more blocks to process
						if (chunkSize <= 0)
							break;

						output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize));
					}

					result = Encoding.ASCII.GetString(Hex.Encode(output.ToArray()));
				}
			}
			catch (Exception)
			{

			}

			return result;
		}
Ejemplo n.º 4
0
        //PrivateKey priv, PublicKey pub)
        // TODO Move this when other JCE tests are ported from Java
        /**
         * signature with a "forged signature" (sig block not at end of plain text)
         */
        private void doTestBadSig()
        {
            //			Signature           sig = Signature.getInstance("SHA1WithRSAEncryption", "BC");
            ISigner sig = SignerUtilities.GetSigner("SHA1WithRSAEncryption");
            //			KeyPairGenerator    fact;
            //			KeyPair             keyPair;
            //			byte[]              data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            //			fact = KeyPairGenerator.getInstance("RSA", "BC");
            RsaKeyPairGenerator fact = new RsaKeyPairGenerator();

            //			fact.initialize(768, new SecureRandom());
            RsaKeyGenerationParameters factParams = new RsaKeyGenerationParameters(
            //				BigInteger.ValueOf(0x11), new SecureRandom(), 768, 25);
                BigInteger.ValueOf(3), new SecureRandom(), 768, 25);
            fact.Init(factParams);

            //			keyPair = fact.generateKeyPair();
            //
            //			PrivateKey  signingKey = keyPair.getPrivate();
            //			PublicKey   verifyKey = keyPair.getPublic();
            IAsymmetricCipherKeyPair keyPair = fact.GenerateKeyPair();

            IAsymmetricKeyParameter priv = keyPair.Private;
            IAsymmetricKeyParameter pub = keyPair.Public;

            //			testBadSig(signingKey, verifyKey);

            //			MessageDigest sha1 = MessageDigest.getInstance("SHA1", "BC");
            IDigest sha1 = DigestUtilities.GetDigest("SHA1");

            //			Cipher signer = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
            //			IBufferedCipher signer = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
            IAsymmetricBlockCipher signer = new Pkcs1Encoding(new RsaEngine());

            //			signer.init(Cipher.ENCRYPT_MODE, priv);
            signer.Init(true, priv);

            //			byte[] block = new byte[signer.getBlockSize()];
            //			byte[] block = new byte[signer.GetBlockSize()];
            byte[] block = new byte[signer.GetInputBlockSize()];

            //			sha1.update((byte)0);
            sha1.Update(0);

            //			byte[] sigHeader = Hex.decode("3021300906052b0e03021a05000414");
            byte[] sigHeader = Hex.Decode("3021300906052b0e03021a05000414");
            //			System.arraycopy(sigHeader, 0, block, 0, sigHeader.length);
            Array.Copy(sigHeader, 0, block, 0, sigHeader.Length);

            //			sha1.digest(block, sigHeader.length, sha1.getDigestLength());
            sha1.DoFinal(block, sigHeader.Length);

            //			System.arraycopy(sigHeader, 0, block,
            //				sigHeader.length + sha1.getDigestLength(), sigHeader.length);
            Array.Copy(sigHeader, 0, block,
                sigHeader.Length + sha1.GetDigestSize(), sigHeader.Length);

            //			byte[] sigBytes = signer.doFinal(block);
            byte[] sigBytes = signer.ProcessBlock(block, 0, block.Length);

            //			Signature verifier = Signature.getInstance("SHA1WithRSA", "BC");
            ISigner verifier = SignerUtilities.GetSigner("SHA1WithRSA");

            //			verifier.initVerify(pub);
            verifier.Init(false, pub);

            //			verifier.update((byte)0);
            verifier.Update(0);

            //			if (verifier.verify(sig))
            if (verifier.VerifySignature(sigBytes))
            {
            //				fail("bad signature passed");
                Fail("bad signature passed");
            }
        }