Exemple #1
0
        public static byte[] Encrypt(this IDssPrivateKeyEntry keyEntry, byte[] digestValue)
        {
            IBufferedCipher cipher = CipherUtilities.GetCipher(
                keyEntry.GetSignatureAlgorithm().GetPadding());

            cipher.Init(true, ((KSPrivateKeyEntry)keyEntry).PrivateKey);
            return(cipher.DoFinal(digestValue));
        }
 /// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
 /// <exception cref="System.IO.IOException"></exception>
 public virtual byte[] Sign(Stream stream, DigestAlgorithm digestAlgo, IDssPrivateKeyEntry
      keyEntry)
 {
     if (SignatureAlgorithm.RSA == keyEntry.GetSignatureAlgorithm())
     {
         IDigest digester = DigestUtilities.GetDigest(digestAlgo.GetName());
         byte[] buffer = new byte[4096];
         int count = 0;
         while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
         {
             digester.BlockUpdate(buffer, 0, count);
         }
         byte[] digestValue = DigestUtilities.DoFinal(digester);
         return EncryptDigest(digestValue, digestAlgo, keyEntry);
     }
     else
     {
         //jbonilla
         throw new System.NotImplementedException("Implementar cuando no es RSA");
         //Sharpen.Signature signature = Sharpen.Signature.GetInstance(keyEntry.GetSignatureAlgorithm
         //    ().GetJavaSignatureAlgorithm(digestAlgo));
         //try
         //{
         //    signature.InitSign(((KSPrivateKeyEntry)keyEntry).GetPrivateKey());
         //    byte[] buffer = new byte[4096];
         //    int count = 0;
         //    while ((count = stream.Read(buffer)) > 0)
         //    {
         //        signature.Update(buffer, 0, count);
         //    }
         //    byte[] signValue = signature.Sign();
         //    return signValue;
         //}
         //catch (SignatureException e)
         //{
         //    throw new RuntimeException(e);
         //}
         //catch (InvalidKeyException e)
         //{
         //    throw new RuntimeException(e);
         //}
     }
 }
Exemple #3
0
 /// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
 /// <exception cref="System.IO.IOException"></exception>
 public virtual byte[] Sign(Stream stream, DigestAlgorithm digestAlgo, IDssPrivateKeyEntry
                            keyEntry)
 {
     if (SignatureAlgorithm.RSA == keyEntry.GetSignatureAlgorithm())
     {
         IDigest digester = DigestUtilities.GetDigest(digestAlgo.GetName());
         byte[]  buffer   = new byte[4096];
         int     count    = 0;
         while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
         {
             digester.BlockUpdate(buffer, 0, count);
         }
         byte[] digestValue = DigestUtilities.DoFinal(digester);
         return(EncryptDigest(digestValue, digestAlgo, keyEntry));
     }
     else
     {
         //jbonilla
         throw new System.NotImplementedException("Implementar cuando no es RSA");
         //Sharpen.Signature signature = Sharpen.Signature.GetInstance(keyEntry.GetSignatureAlgorithm
         //    ().GetJavaSignatureAlgorithm(digestAlgo));
         //try
         //{
         //    signature.InitSign(((KSPrivateKeyEntry)keyEntry).GetPrivateKey());
         //    byte[] buffer = new byte[4096];
         //    int count = 0;
         //    while ((count = stream.Read(buffer)) > 0)
         //    {
         //        signature.Update(buffer, 0, count);
         //    }
         //    byte[] signValue = signature.Sign();
         //    return signValue;
         //}
         //catch (SignatureException e)
         //{
         //    throw new RuntimeException(e);
         //}
         //catch (InvalidKeyException e)
         //{
         //    throw new RuntimeException(e);
         //}
     }
 }
        /// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
        public override byte[] EncryptDigest(byte[] digestValue, DigestAlgorithm digestAlgo
                                             , IDssPrivateKeyEntry keyEntry)
        {
            try
            {
                DigestInfo digestInfo = new DigestInfo(digestAlgo.GetAlgorithmIdentifier(), digestValue
                                                       );

                //Sharpen.Cipher cipher = Sharpen.Cipher.GetInstance(keyEntry.GetSignatureAlgorithm
                //    ().GetPadding());

                IBufferedCipher cipher = CipherUtilities.GetCipher(keyEntry.GetSignatureAlgorithm
                                                                       ().GetPadding());

                //cipher.Init(Sharpen.Cipher.ENCRYPT_MODE, ((KSPrivateKeyEntry)keyEntry).GetPrivateKey
                //    ());

                cipher.Init(true, ((KSPrivateKeyEntry)keyEntry).PrivateKey);

                return(cipher.DoFinal(digestInfo.GetDerEncoded()));
            }

            /*catch (NoSuchPaddingException e)
             * {
             *  throw new RuntimeException(e);
             * }*/
            catch (InvalidKeyException e)
            {
                throw new RuntimeException(e);
            }

            /*catch (IllegalBlockSizeException e)
             * {
             *  throw new RuntimeException(e);
             * }
             * catch (BadPaddingException)
             * {
             *  // More likely the password is not good.
             *  throw new BadPasswordException(BadPasswordException.MSG.PKCS12_BAD_PASSWORD);
             * }*/
        }
        /// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
        public override byte[] EncryptDigest(byte[] digestValue, DigestAlgorithm digestAlgo
                                             , IDssPrivateKeyEntry keyEntry)
        {
            try
            {
                ByteArrayOutputStream digestInfo = new ByteArrayOutputStream();
                //jbonilla: cambio de enum a clase.
                if (digestAlgo.Equals(DigestAlgorithm.SHA1))
                {
                    digestInfo.Write(Constants.SHA1_DIGEST_INFO_PREFIX);
                }
                else
                {
                    if (digestAlgo.Equals(DigestAlgorithm.SHA256))
                    {
                        digestInfo.Write(Constants.SHA256_DIGEST_INFO_PREFIX);
                    }
                    else
                    {
                        if (digestAlgo.Equals(DigestAlgorithm.SHA256))
                        {
                            digestInfo.Write(Constants.SHA512_DIGEST_INFO_PREFIX);
                        }
                    }
                }
                digestInfo.Write(digestValue);
                //Sharpen.Cipher cipher = Sharpen.Cipher.GetInstance(keyEntry.GetSignatureAlgorithm
                //    ().GetPadding());
                IBufferedCipher cipher = CipherUtilities.GetCipher(keyEntry.GetSignatureAlgorithm
                                                                       ().GetPadding());

                //cipher.Init(Sharpen.Cipher.ENCRYPT_MODE, ((KSPrivateKeyEntry)keyEntry).GetPrivateKey
                //    ());
                cipher.Init(true, ((KSPrivateKeyEntry)keyEntry).PrivateKey);
                return(cipher.DoFinal(digestInfo.ToByteArray()));
            }
            catch (IOException e)
            {
                // Writing in a ByteArrayOutputStream. Should never happens.
                throw new RuntimeException(e);
            }

            /*catch (NoSuchPaddingException e)
             * {
             *  throw new RuntimeException(e);
             * }*/
            catch (InvalidKeyException e)
            {
                throw new RuntimeException(e);
            }

            /*catch (IllegalBlockSizeException e)
             * {
             *  throw new RuntimeException(e);
             * }
             * catch (BadPaddingException)
             * {
             *  // More likely the password is not good.
             *  throw new BadPasswordException(BadPasswordException.MSG.PKCS12_BAD_PASSWORD);
             * }*/
        }
        /// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
        public override byte[] EncryptDigest(byte[] digestValue, DigestAlgorithm digestAlgo
            , IDssPrivateKeyEntry keyEntry)
        {
            try
            {
                ByteArrayOutputStream digestInfo = new ByteArrayOutputStream();
                //jbonilla: cambio de enum a clase.
                if (digestAlgo.Equals(DigestAlgorithm.SHA1))
                {
                    digestInfo.Write(Constants.SHA1_DIGEST_INFO_PREFIX);
                }
                else
                {
                    if (digestAlgo.Equals(DigestAlgorithm.SHA256))
                    {
                        digestInfo.Write(Constants.SHA256_DIGEST_INFO_PREFIX);
                    }
                    else
                    {
                        if (digestAlgo.Equals(DigestAlgorithm.SHA256))
                        {
                            digestInfo.Write(Constants.SHA512_DIGEST_INFO_PREFIX);
                        }
                    }
                }
                digestInfo.Write(digestValue);
                //Sharpen.Cipher cipher = Sharpen.Cipher.GetInstance(keyEntry.GetSignatureAlgorithm
                //    ().GetPadding());
                IBufferedCipher cipher = CipherUtilities.GetCipher(keyEntry.GetSignatureAlgorithm
                    ().GetPadding());

                //cipher.Init(Sharpen.Cipher.ENCRYPT_MODE, ((KSPrivateKeyEntry)keyEntry).GetPrivateKey
                //    ());
                cipher.Init(true, ((KSPrivateKeyEntry)keyEntry).PrivateKey);
                return cipher.DoFinal(digestInfo.ToByteArray());
            }
            catch (IOException e)
            {
                // Writing in a ByteArrayOutputStream. Should never happens.
                throw new RuntimeException(e);
            }
            /*catch (NoSuchPaddingException e)
            {
                throw new RuntimeException(e);
            }*/
            catch (InvalidKeyException e)
            {
                throw new RuntimeException(e);
            }
            /*catch (IllegalBlockSizeException e)
            {
                throw new RuntimeException(e);
            }
            catch (BadPaddingException)
            {
                // More likely the password is not good.
                throw new BadPasswordException(BadPasswordException.MSG.PKCS12_BAD_PASSWORD);
            }*/
        }
        /// <exception cref="Sharpen.NoSuchAlgorithmException"></exception>
        public override byte[] EncryptDigest(byte[] digestValue, DigestAlgorithm digestAlgo
            , IDssPrivateKeyEntry keyEntry)
        {
            try
            {
                DigestInfo digestInfo = new DigestInfo(digestAlgo.GetAlgorithmIdentifier(), digestValue
                    );

                //Sharpen.Cipher cipher = Sharpen.Cipher.GetInstance(keyEntry.GetSignatureAlgorithm
                //    ().GetPadding());

                IBufferedCipher cipher = CipherUtilities.GetCipher(keyEntry.GetSignatureAlgorithm
                    ().GetPadding());

                //cipher.Init(Sharpen.Cipher.ENCRYPT_MODE, ((KSPrivateKeyEntry)keyEntry).GetPrivateKey
                //    ());

                cipher.Init(true, ((KSPrivateKeyEntry)keyEntry).PrivateKey);

                return cipher.DoFinal(digestInfo.GetDerEncoded());
            }
            /*catch (NoSuchPaddingException e)
            {
                throw new RuntimeException(e);
            }*/
            catch (InvalidKeyException e)
            {
                throw new RuntimeException(e);
            }
            /*catch (IllegalBlockSizeException e)
            {
                throw new RuntimeException(e);
            }
            catch (BadPaddingException)
            {
                // More likely the password is not good.
                throw new BadPasswordException(BadPasswordException.MSG.PKCS12_BAD_PASSWORD);
            }*/
        }