Init() публичный Метод

public Init ( bool forEncryption, ICipherParameters param ) : void
forEncryption bool
param ICipherParameters
Результат void
Пример #1
0
        private void EncDec(
            string				label,
            RsaKeyParameters	pubParameters,
            RsaKeyParameters	privParameters,
            byte[]				seed,
            byte[]				input,
            byte[]				output)
        {
            IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());

            cipher.Init(true, new ParametersWithRandom(pubParameters, new VecRand(seed)));

            byte[] outBytes = cipher.ProcessBlock(input, 0, input.Length);

            for (int i = 0; i != output.Length; i++)
            {
                if (outBytes[i] != output[i])
                {
                    Fail(label + " failed encryption");
                }
            }

            cipher.Init(false, privParameters);

            outBytes = cipher.ProcessBlock(output, 0, output.Length);

            for (int i = 0; i != input.Length; i++)
            {
                if (outBytes[i] != input[i])
                {
                    Fail(label + " failed decoding");
                }
            }
        }
Пример #2
0
        // signature
        public static string CreateSignature(string email, string password, RsaKeyParameters key)
        {
            
            byte[] prefix = { 0x00 };
            var keyStruct = KeyToStruct(key);
            var toEncrypt = Encoding.UTF8.GetBytes(email + "\x00" + password);
            var cipher = new OaepEncoding(new RsaEngine(), new Sha1Digest(), null);
            cipher.Init(true, key);
            var encrypted = cipher.ProcessBlock(toEncrypt, 0, toEncrypt.Length);

            var digest = new Sha1Digest();
            var hash = new byte[digest.GetByteLength()];
            digest.BlockUpdate(keyStruct, 0, keyStruct.Length);
            digest.DoFinal(hash, 0);
            var hashExcerpt = hash.Take(4).ToArray();

            return DataTypeUtils.UrlSafeBase64(DataTypeUtils.CombineBytes(prefix, hashExcerpt, encrypted));
        }
Пример #3
0
		private void doTestOaep(RsaKeyParameters pubParameters, RsaKeyParameters privParameters)
		{
			//
			// OAEP - public encrypt, private decrypt
			//
			IAsymmetricBlockCipher eng = new OaepEncoding(new RsaBlindedEngine());
			byte[] data = Hex.Decode(input);

			eng.Init(true, pubParameters);

			try
			{
				data = eng.ProcessBlock(data, 0, data.Length);
			}
			catch (Exception e)
			{
				Fail("failed - exception " + e.ToString(), e);
			}

			eng.Init(false, privParameters);

			try
			{
				data = eng.ProcessBlock(data, 0, data.Length);
			}
			catch (Exception e)
			{
				Fail("failed - exception " + e.ToString(), e);
			}

			if (!input.Equals(Hex.ToHexString(data)))
			{
				Fail("failed OAEP Test");
			}
		}
Пример #4
0
        /// <summary>
        /// Creates an asymmetric algorithm instance (RSA) with the public key of this instance,
        /// ready for encryption
        /// </summary>
        /// <returns></returns>
        public IAsymmetricBlockCipher CreateRSAEncrypter()
        {
            if (_keyParams.AlgorithmId == TPMAlgorithmId.TPM_ALG_RSA && _keyParams.EncScheme == TPMEncScheme.TPM_ES_RSAESOAEP_SHA1_MGF1)
            {
                IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha1Digest(), Encoding.ASCII.GetBytes("TCPA"));

                RsaKeyParameters parameters =
                    new RsaKeyParameters( false,
                                         new BigInteger(1, _publicKey.Pubkey),
                                         new BigInteger(1, ((TPMRSAKeyParams)_keyParams.Params).GetExponent()));

                cipher.Init(true, parameters);

                return cipher;
            }
            else if (_keyParams.AlgorithmId == TPMAlgorithmId.TPM_ALG_RSA && _keyParams.EncScheme == TPMEncScheme.TPM_ES_RSAESPKCSv15)
            {
                IAsymmetricBlockCipher cipher = new Pkcs1Encoding(new RsaEngine());

                RsaKeyParameters parameters =
                    new RsaKeyParameters( false,
                                         new BigInteger(1, _publicKey.Pubkey),
                                         new BigInteger(1, ((TPMRSAKeyParams)_keyParams.Params).GetExponent()));

                cipher.Init(true, parameters);

                return cipher;
            }
            else
                throw new NotSupportedException(string.Format("Algorithm '{0}' with '{1}' is not supported", _keyParams.AlgorithmId, _keyParams.EncScheme));
        }
Пример #5
0
        private static string EncryptId()
        {
            var cipher = new OaepEncoding(new RsaEngine());

            cipher.Init(true, CreateCipherParameters());

            var decryptedId = Encoding.UTF8.GetBytes(Id);
            var encryptedId = cipher.ProcessBlock(decryptedId, 0, decryptedId.Length);

            return Convert.ToBase64String(encryptedId);
        }
Пример #6
0
    public ByteString ProceedMessageEncoding(AsymmetricKeyParameter messageKey, ByteString messageBytes)
    {
      Debug.Log("Proceeding with key IsPrivate={0} Message Size={1}.", messageKey.IsPrivate, messageBytes.Length);

      OaepEncoding cipherEncoder = new OaepEncoding(new RsaBlindedEngine(), new Sha256Digest());
      cipherEncoder.Init(!(messageKey.IsPrivate), messageKey);

      using (MemoryStream outputStream = new MemoryStream())
      using (Stream inputStream = new MemoryStream(messageBytes.ToByteArray())) {
        byte[] inputBlock = new byte[cipherEncoder.GetInputBlockSize()];
        int readBytes = 0;

        while ((readBytes = inputStream.Read(inputBlock, 0, inputBlock.Length)) > 0) {
          byte[] outputBlock = cipherEncoder.ProcessBlock(inputBlock, 0, readBytes);
          outputStream.Write(outputBlock, 0, outputBlock.Length);

          if (readBytes != inputBlock.Length)
            break;
        }
        return ByteString.CopyFrom(outputStream.ToArray(), 0, (int)outputStream.Length);
      }
    }