Class DnieCipherImpl


  • public final class DnieCipherImpl
    extends javax.crypto.CipherSpi
    RSA cipher implementation. Supports RSA en/decryption and signing/verifying using PKCS#1 v1.5 padding and without padding (raw RSA). Note that raw RSA is supported mostly for completeness and should only be used in rare cases. Objects should be instantiated by calling Cipher.getInstance() using the following algorithm names: . "RSA/ECB/PKCS1Padding" (or "RSA") for PKCS#1 padding. The mode (blocktype) is selected based on the en/decryption mode and public/private key used . "RSA/ECB/NoPadding" for rsa RSA. We only do one RSA operation per doFinal() call. If the application passes more data via calls to update() or doFinal(), we throw an IllegalBlockSizeException when doFinal() is called (see JCE API spec). Bulk encryption using RSA does not make sense and is not standardized. Note: RSA keys should be at least 512 bits long
    Since:
    1.5
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static byte[] B0  
      private byte[] buffer
      Buffer para los datos.
      private int bufOfs
      Número de octetos en el buffer (el offset).
      private int mode  
      private static int MODE_DECRYPT  
      private static int MODE_ENCRYPT  
      private static int MODE_SIGN  
      private static int MODE_VERIFY  
      private java.lang.String oaepHashAlgorithm
      Algoritmo de huella para el OAEP.
      private int outputSize
      Tamaño de los datos de salida.
      private static java.lang.String PAD_NONE
      RSA sin relleno.
      private static java.lang.String PAD_OAEP_MGF1  
      private static java.lang.String PAD_PKCS1
      RSA con relleno PKCS#1 v1.5.
      private RSAPadding padding  
      private java.lang.String paddingType  
      private java.security.interfaces.RSAPrivateKey privateKey
      Clave privada, si se ha inicializado el cifrador con una clave privada.
      private java.security.interfaces.RSAPublicKey publicKey
      La clave pública, si se ha inicializado el cifrador con una clave pública.
      private java.security.SecureRandom random  
      private java.security.spec.AlgorithmParameterSpec spec  
    • Constructor Summary

      Constructors 
      Constructor Description
      DnieCipherImpl()
      Construye un cifrador RSA para el DNIe.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private byte[] doFinal()  
      protected byte[] engineDoFinal​(byte[] inData, int inOfs, int inLen)  
      protected int engineDoFinal​(byte[] inData, int inOfs, int inLen, byte[] out, int outOfs)  
      protected int engineGetBlockSize()  
      protected byte[] engineGetIV()  
      protected int engineGetKeySize​(java.security.Key key)  
      protected int engineGetOutputSize​(int inputLen)  
      protected java.security.AlgorithmParameters engineGetParameters()  
      protected void engineInit​(int opmode, java.security.Key key, java.security.AlgorithmParameters params, java.security.SecureRandom rnd)  
      protected void engineInit​(int opmode, java.security.Key key, java.security.SecureRandom rnd)  
      protected void engineInit​(int opmode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom rnd)  
      protected void engineSetMode​(java.lang.String cipherMode)  
      protected void engineSetPadding​(java.lang.String paddingName)  
      protected java.security.Key engineUnwrap​(byte[] wrappedKey, java.lang.String algorithm, int type)  
      protected byte[] engineUpdate​(byte[] inData, int inOfs, int inLen)  
      protected int engineUpdate​(byte[] inData, int inOfs, int inLen, byte[] out, int outOfs)  
      protected byte[] engineWrap​(java.security.Key key)  
      private void init​(int opmode, java.security.Key key, java.security.SecureRandom rnd, java.security.spec.AlgorithmParameterSpec params)
      Inicializa el cifrador.
      private void update​(byte[] inData, int inOfs, int inLen)  
      • Methods inherited from class javax.crypto.CipherSpi

        engineDoFinal, engineUpdate, engineUpdateAAD, engineUpdateAAD
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • B0

        private static final byte[] B0
      • PAD_NONE

        private static final java.lang.String PAD_NONE
        RSA sin relleno.
        See Also:
        Constant Field Values
      • PAD_PKCS1

        private static final java.lang.String PAD_PKCS1
        RSA con relleno PKCS#1 v1.5.
        See Also:
        Constant Field Values
      • mode

        private int mode
      • paddingType

        private java.lang.String paddingType
      • spec

        private java.security.spec.AlgorithmParameterSpec spec
      • buffer

        private byte[] buffer
        Buffer para los datos.
      • bufOfs

        private int bufOfs
        Número de octetos en el buffer (el offset).
      • outputSize

        private int outputSize
        Tamaño de los datos de salida.
      • publicKey

        private java.security.interfaces.RSAPublicKey publicKey
        La clave pública, si se ha inicializado el cifrador con una clave pública.
      • privateKey

        private java.security.interfaces.RSAPrivateKey privateKey
        Clave privada, si se ha inicializado el cifrador con una clave privada.
      • oaepHashAlgorithm

        private final java.lang.String oaepHashAlgorithm
        Algoritmo de huella para el OAEP.
        See Also:
        Constant Field Values
      • random

        private java.security.SecureRandom random
    • Constructor Detail

      • DnieCipherImpl

        public DnieCipherImpl()
        Construye un cifrador RSA para el DNIe.
    • Method Detail

      • engineSetMode

        protected void engineSetMode​(java.lang.String cipherMode)
                              throws java.security.NoSuchAlgorithmException
        Specified by:
        engineSetMode in class javax.crypto.CipherSpi
        Throws:
        java.security.NoSuchAlgorithmException
      • engineSetPadding

        protected void engineSetPadding​(java.lang.String paddingName)
                                 throws javax.crypto.NoSuchPaddingException
        Specified by:
        engineSetPadding in class javax.crypto.CipherSpi
        Throws:
        javax.crypto.NoSuchPaddingException
      • engineGetBlockSize

        protected int engineGetBlockSize()
        Specified by:
        engineGetBlockSize in class javax.crypto.CipherSpi
      • engineGetOutputSize

        protected int engineGetOutputSize​(int inputLen)
        Specified by:
        engineGetOutputSize in class javax.crypto.CipherSpi
      • engineGetIV

        protected byte[] engineGetIV()
        Specified by:
        engineGetIV in class javax.crypto.CipherSpi
      • engineGetParameters

        protected java.security.AlgorithmParameters engineGetParameters()
        Specified by:
        engineGetParameters in class javax.crypto.CipherSpi
      • engineInit

        protected void engineInit​(int opmode,
                                  java.security.Key key,
                                  java.security.SecureRandom rnd)
                           throws java.security.InvalidKeyException
        Specified by:
        engineInit in class javax.crypto.CipherSpi
        Throws:
        java.security.InvalidKeyException
      • engineInit

        protected void engineInit​(int opmode,
                                  java.security.Key key,
                                  java.security.spec.AlgorithmParameterSpec params,
                                  java.security.SecureRandom rnd)
                           throws java.security.InvalidKeyException,
                                  java.security.InvalidAlgorithmParameterException
        Specified by:
        engineInit in class javax.crypto.CipherSpi
        Throws:
        java.security.InvalidKeyException
        java.security.InvalidAlgorithmParameterException
      • engineInit

        protected void engineInit​(int opmode,
                                  java.security.Key key,
                                  java.security.AlgorithmParameters params,
                                  java.security.SecureRandom rnd)
                           throws java.security.InvalidKeyException,
                                  java.security.InvalidAlgorithmParameterException
        Specified by:
        engineInit in class javax.crypto.CipherSpi
        Throws:
        java.security.InvalidKeyException
        java.security.InvalidAlgorithmParameterException
      • init

        private void init​(int opmode,
                          java.security.Key key,
                          java.security.SecureRandom rnd,
                          java.security.spec.AlgorithmParameterSpec params)
                   throws java.security.InvalidKeyException,
                          java.security.InvalidAlgorithmParameterException
        Inicializa el cifrador.
        Throws:
        java.security.InvalidKeyException
        java.security.InvalidAlgorithmParameterException
      • update

        private void update​(byte[] inData,
                            int inOfs,
                            int inLen)
      • engineUpdate

        protected byte[] engineUpdate​(byte[] inData,
                                      int inOfs,
                                      int inLen)
        Specified by:
        engineUpdate in class javax.crypto.CipherSpi
      • engineUpdate

        protected int engineUpdate​(byte[] inData,
                                   int inOfs,
                                   int inLen,
                                   byte[] out,
                                   int outOfs)
        Specified by:
        engineUpdate in class javax.crypto.CipherSpi
      • engineDoFinal

        protected byte[] engineDoFinal​(byte[] inData,
                                       int inOfs,
                                       int inLen)
                                throws javax.crypto.BadPaddingException,
                                       javax.crypto.IllegalBlockSizeException
        Specified by:
        engineDoFinal in class javax.crypto.CipherSpi
        Throws:
        javax.crypto.BadPaddingException
        javax.crypto.IllegalBlockSizeException
      • engineDoFinal

        protected int engineDoFinal​(byte[] inData,
                                    int inOfs,
                                    int inLen,
                                    byte[] out,
                                    int outOfs)
                             throws javax.crypto.ShortBufferException,
                                    javax.crypto.BadPaddingException,
                                    javax.crypto.IllegalBlockSizeException
        Specified by:
        engineDoFinal in class javax.crypto.CipherSpi
        Throws:
        javax.crypto.ShortBufferException
        javax.crypto.BadPaddingException
        javax.crypto.IllegalBlockSizeException
      • engineWrap

        protected byte[] engineWrap​(java.security.Key key)
                             throws java.security.InvalidKeyException,
                                    javax.crypto.IllegalBlockSizeException
        Overrides:
        engineWrap in class javax.crypto.CipherSpi
        Throws:
        java.security.InvalidKeyException
        javax.crypto.IllegalBlockSizeException
      • engineUnwrap

        protected java.security.Key engineUnwrap​(byte[] wrappedKey,
                                                 java.lang.String algorithm,
                                                 int type)
                                          throws java.security.InvalidKeyException,
                                                 java.security.NoSuchAlgorithmException
        Overrides:
        engineUnwrap in class javax.crypto.CipherSpi
        Throws:
        java.security.InvalidKeyException
        java.security.NoSuchAlgorithmException
      • engineGetKeySize

        protected int engineGetKeySize​(java.security.Key key)
                                throws java.security.InvalidKeyException
        Overrides:
        engineGetKeySize in class javax.crypto.CipherSpi
        Throws:
        java.security.InvalidKeyException