コード例 #1
0
        public static string GenEncrypedPin(string pin, string pinToken, string sessionId, RsaPrivateCrtKeyParameters rsa, ulong it)
        {
            ulong time = (ulong)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            var bPinToken = Convert.FromBase64String(pinToken);


            IAsymmetricBlockCipher cipherRsa = new RsaBlindedEngine();

            cipherRsa = new OaepEncoding(cipherRsa, new Sha256Digest(), new Sha256Digest(), Encoding.ASCII.GetBytes(sessionId));
            BufferedAsymmetricBlockCipher cipher = new BufferedAsymmetricBlockCipher(cipherRsa);

            cipher.Init(false, rsa);
            cipher.ProcessBytes(bPinToken, 0, bPinToken.Length);
            var key = cipher.DoFinal();


            var bPin      = Encoding.ASCII.GetBytes(pin);
            var btime     = BitConverter.GetBytes(time);
            var biterator = BitConverter.GetBytes(it);


            int len = bPin.Length + btime.Length + biterator.Length;

            IBlockCipher cipherAes    = new AesEngine();
            int          bsize        = cipherAes.GetBlockSize();
            KeyParameter keyParameter = new KeyParameter(key);


            int nPadding = bsize - len % bsize;
            var bPadding = new byte[nPadding];

            len += (len % bsize == 0 ? 0 : nPadding);


            var blocks = new byte[len];

            Array.Copy(bPin, blocks, bPin.Length);
            Array.Copy(btime, 0, blocks, bPin.Length, btime.Length);
            Array.Copy(biterator, 0, blocks, bPin.Length + btime.Length, biterator.Length);
            Array.Copy(bPadding, 0, blocks, bPin.Length + btime.Length + biterator.Length, nPadding);

            var iv = new byte[bsize];

            random.NextBytes(iv);

            CbcBlockCipher   cbcBc            = new CbcBlockCipher(cipherAes);
            ParametersWithIV parametersWithIV = new ParametersWithIV(keyParameter, iv);

            BufferedBlockCipher bc = new BufferedBlockCipher(cbcBc);

            bc.Init(true, parametersWithIV);
            var bOut = bc.ProcessBytes(blocks);
            var rz   = new byte[bOut.Length + bsize];

            Array.Copy(iv, rz, iv.Length);
            Array.Copy(bOut, 0, rz, iv.Length, bOut.Length);

            return(Convert.ToBase64String(rz));
        }
コード例 #2
0
        /// <summary>
        /// Encrypts the string encoded plain text
        /// </summary>
        /// <param name="asymmetricEncryptionAlgorithm"></param>
        /// <param name="hashAlgorithm"></param>
        /// <param name="asymmetricEncryptionPadding"></param>
        /// <param name="asymmetricKeyParameter"></param>
        /// <param name="plainText"></param>
        /// <returns>Base64 encrypted encryptedInput text</returns>
        private string doEncrypt(AsymmetricEncryptionAlgorithm asymmetricEncryptionAlgorithm, HashAlgorithm hashAlgorithm, AsymmetricEncryptionPadding asymmetricEncryptionPadding, AsymmetricKeyParameter asymmetricKeyParameter, string plainText)
        {
            IAsymmetricBlockCipher asymEngine = getEngine(asymmetricEncryptionAlgorithm);
            IDigest hash = getDigest(hashAlgorithm);
            IAsymmetricBlockCipher        cipher         = getPadding(asymEngine, hash, asymmetricEncryptionPadding);
            BufferedAsymmetricBlockCipher bufferedCipher = new BufferedAsymmetricBlockCipher(cipher);

            if (this.error.existsError())
            {
                return("");
            }
            bufferedCipher.Init(true, asymmetricKeyParameter);
            EncodingUtil eu = new EncodingUtil();

            byte[] inputBytes = eu.getBytes(plainText);
            if (eu.GetError().existsError())
            {
                this.error = eu.GetError();
                return("");
            }
            bufferedCipher.ProcessBytes(inputBytes, 0, inputBytes.Length);
            byte[] outputBytes = bufferedCipher.DoFinal();
            if (outputBytes == null || outputBytes.Length == 0)
            {
                this.error.setError("AE041", "Asymmetric encryption error");
                return("");
            }
            this.error.cleanError();
            return(Base64.ToBase64String(outputBytes));
        }
コード例 #3
0
        /// <summary>
        /// Decrypts the base64 encoded encrypted text
        /// </summary>
        /// <param name="asymmetricEncryptionAlgorithm">string AsymmetricEncryptionAlgorithm enum, algorithm name</param>
        /// <param name="hashAlgorithm">string HashAlgorithm enum, algorithm name</param>
        /// <param name="asymmetricEncryptionPadding">string AsymmetricEncryptionPadding enum, padding name</param>
        /// <param name="asymmetricKeyParameter">AsymmetricKeyParameter with loaded key for specified algorithm</param>
        /// <param name="encryptedInput">string Base64 to decrypt</param>
        /// <returns>string decypted encryptedInput text</returns>
        private string doDecrypt(AsymmetricEncryptionAlgorithm asymmetricEncryptionAlgorithm, HashAlgorithm hashAlgorithm, AsymmetricEncryptionPadding asymmetricEncryptionPadding, AsymmetricKeyParameter asymmetricKeyParameter, string encryptedInput)
        {
            IAsymmetricBlockCipher asymEngine = getEngine(asymmetricEncryptionAlgorithm);
            IDigest hash = getDigest(hashAlgorithm);
            IAsymmetricBlockCipher        cipher         = getPadding(asymEngine, hash, asymmetricEncryptionPadding);
            BufferedAsymmetricBlockCipher bufferedCipher = new BufferedAsymmetricBlockCipher(cipher);

            if (this.error.existsError())
            {
                return("");
            }
            bufferedCipher.Init(false, asymmetricKeyParameter);
            byte[] inputBytes = Base64.Decode(encryptedInput);
            bufferedCipher.ProcessBytes(inputBytes, 0, inputBytes.Length);
            byte[] outputBytes = bufferedCipher.DoFinal();
            if (outputBytes == null || outputBytes.Length == 0)
            {
                this.error.setError("AE040", "Asymmetric decryption error");
                return("");
            }
            EncodingUtil eu = new EncodingUtil();

            this.error = eu.GetError();
            return(eu.getString(outputBytes));
        }
コード例 #4
0
        public byte[] Decrypto(byte[] data, AsymmetricKeyParameter ppk)
        {
            BufferedAsymmetricBlockCipher rsa = new BufferedAsymmetricBlockCipher(new Pkcs1Encoding(new RsaEngine()));

            rsa.Init(false, ppk);
            return(rsa.DoFinal(data));
        }
コード例 #5
0
        /// <summary>
        /// Process asymmetric data using a custom expected length.
        /// The output will ALWAYS be resized to fit expected length
        /// </summary>
        public override byte[] ProcessWithLength(bool encrypting, byte[] in_data, ICipherParameters keyParameter, out int length)
        {
            BufferedAsymmetricBlockCipher bcipher = new BufferedAsymmetricBlockCipher(new RsaEngine());

            bcipher.Init(encrypting, keyParameter);

            byte[] out_data = new byte[bcipher.GetOutputSize(in_data.Length)];
            int    len      = bcipher.DoFinal(in_data, out_data, 0);

            length = len;
            return(out_data);
        }
コード例 #6
0
        public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            var len = _cipher.ProcessBytes(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);

            return(_cipher.DoFinal(inputBuffer, inputOffset, len, outputBuffer, outputOffset));
        }