Search in sources :

Example 46 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project jeesuite-libs by vakinge.

the class RSA method encrypt.

/**
 * 加密
 *
 * @param key
 * @param plainBytes
 * @return
 */
public static byte[] encrypt(PublicKey key, byte[] plainBytes) {
    ByteArrayOutputStream out = null;
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        int inputLen = plainBytes.length;
        if (inputLen <= MAX_ENCRYPT_BLOCK) {
            return cipher.doFinal(plainBytes);
        }
        out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(plainBytes, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(plainBytes, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        return out.toByteArray();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("无此解密算法");
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        return null;
    } catch (InvalidKeyException e) {
        throw new RuntimeException("解密私钥非法,请检查");
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException("密文长度非法");
    } catch (BadPaddingException e) {
        throw new RuntimeException("密文数据已损坏");
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (Exception e2) {
        }
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 47 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project mssql-jdbc by Microsoft.

the class SQLServerColumnEncryptionJavaKeyStoreProvider method encryptRSAOAEP.

/**
 * Encrypt plainText with the certificate provided
 *
 * @param plainText
 *            plain CEK to be encrypted
 * @param certificateDetails
 * @return encrypted CEK
 * @throws SQLServerException
 */
private byte[] encryptRSAOAEP(byte[] plainText, CertificateDetails certificateDetails) throws SQLServerException {
    byte[] cipherText = null;
    try {
        Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
        rsa.init(Cipher.ENCRYPT_MODE, certificateDetails.certificate.getPublicKey());
        rsa.update(plainText);
        cipherText = rsa.doFinal();
    } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalBlockSizeException | NoSuchPaddingException | BadPaddingException e) {
        MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_EncryptionFailed"));
        Object[] msgArgs = { e.getMessage() };
        throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
    }
    return cipherText;
}
Also used : MessageFormat(java.text.MessageFormat) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 48 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project mssql-jdbc by Microsoft.

the class SQLServerAeadAes256CbcHmac256Algorithm method encryptData.

/**
 * Performs encryption of plain text
 *
 * @param plainText
 *            text to be encrypted
 * @param hasAuthenticationTag
 *            specify if encryption needs authentication
 * @return cipher text
 * @throws SQLServerException
 */
protected byte[] encryptData(byte[] plainText, boolean hasAuthenticationTag) throws SQLServerException {
    aeLogger.entering(SQLServerAeadAes256CbcHmac256Algorithm.class.getName(), Thread.currentThread().getStackTrace()[1].getMethodName(), "Encrypting data.");
    // this encryption type is deterministic
    assert (plainText != null);
    byte[] iv = new byte[blockSizeInBytes];
    // Secret/private key to be used in AES encryption
    SecretKeySpec skeySpec = new SecretKeySpec(columnEncryptionkey.getEncryptionKey(), "AES");
    if (isDeterministic) {
        // this method makes sure this is 16 bytes key
        try {
            iv = SQLServerSecurityUtility.getHMACWithSHA256(plainText, columnEncryptionkey.getIVKey(), blockSizeInBytes);
        } catch (InvalidKeyException | NoSuchAlgorithmException e) {
            MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_EncryptionFailed"));
            Object[] msgArgs = { e.getMessage() };
            throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
        }
    } else {
        SecureRandom random = new SecureRandom();
        random.nextBytes(iv);
    }
    int numBlocks = plainText.length / blockSizeInBytes + 1;
    int hmacStartIndex = 1;
    int authenticationTagLen = hasAuthenticationTag ? keySizeInBytes : 0;
    int ivStartIndex = hmacStartIndex + authenticationTagLen;
    int cipherStartIndex = ivStartIndex + blockSizeInBytes;
    // Output buffer size = size of VersionByte + Authentication Tag + IV + cipher Text blocks.
    int outputBufSize = 1 + authenticationTagLen + iv.length + (numBlocks * blockSizeInBytes);
    byte[] outBuffer = new byte[outputBufSize];
    // Copying the version to output buffer
    outBuffer[0] = algorithmVersion;
    // Coping IV to the output buffer
    System.arraycopy(iv, 0, outBuffer, ivStartIndex, iv.length);
    try {
        // initialization vector
        IvParameterSpec ivector = new IvParameterSpec(iv);
        Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivector);
        int count = 0;
        // this is where cipherText starts
        int cipherIndex = cipherStartIndex;
        if (numBlocks > 1) {
            count = (numBlocks - 1) * blockSizeInBytes;
            cipherIndex += encryptCipher.update(plainText, 0, count, outBuffer, cipherIndex);
        }
        // doFinal will complete the encryption
        byte[] buffTmp = encryptCipher.doFinal(plainText, count, plainText.length - count);
        // Encryption completed
        System.arraycopy(buffTmp, 0, outBuffer, cipherIndex, buffTmp.length);
        if (hasAuthenticationTag) {
            Mac hmac = Mac.getInstance("HmacSHA256");
            SecretKeySpec initkey = new SecretKeySpec(columnEncryptionkey.getMacKey(), "HmacSHA256");
            hmac.init(initkey);
            hmac.update(version, 0, version.length);
            hmac.update(iv, 0, iv.length);
            hmac.update(outBuffer, cipherStartIndex, numBlocks * blockSizeInBytes);
            hmac.update(versionSize, 0, version.length);
            byte[] hash = hmac.doFinal();
            // coping the authentication tag in the output buffer which holds cipher text
            System.arraycopy(hash, 0, outBuffer, hmacStartIndex, authenticationTagLen);
        }
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | ShortBufferException e) {
        MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_EncryptionFailed"));
        Object[] msgArgs = { e.getMessage() };
        throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
    }
    aeLogger.exiting(SQLServerAeadAes256CbcHmac256Algorithm.class.getName(), Thread.currentThread().getStackTrace()[1].getMethodName(), "Data encrypted.");
    return outBuffer;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) MessageFormat(java.text.MessageFormat) SecureRandom(java.security.SecureRandom) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 49 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project vip by guangdada.

the class AesCbcUtil method decrypt.

/**
 * AES解密
 *
 * @param data           //密文,被加密的数据
 * @param key            //秘钥
 * @param iv             //偏移量
 * @param encodingFormat //解密后的结果需要进行的编码
 * @return
 * @throws Exception
 */
public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception {
    // initialize();
    // 被加密的数据
    byte[] dataByte = Base64.decodeBase64(data);
    // 加密秘钥
    byte[] keyByte = Base64.decodeBase64(key);
    // 偏移量
    byte[] ivByte = Base64.decodeBase64(iv);
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
        AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
        parameters.init(new IvParameterSpec(ivByte));
        // 初始化
        cipher.init(Cipher.DECRYPT_MODE, spec, parameters);
        byte[] resultByte = cipher.doFinal(dataByte);
        if (null != resultByte && resultByte.length > 0) {
            String result = new String(resultByte, encodingFormat);
            return result;
        }
        return null;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidParameterSpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BadPaddingException(javax.crypto.BadPaddingException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 50 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project lzc_app_lib by httplzc.

the class AESUtils method encryptSecret.

public static String encryptSecret(byte[] pubKey, byte[] data) {
    try {
        // 恢复密钥
        SecretKey secretKey = new SecretKeySpec(pubKey, AES);
        // Cipher完成加密或解密工作类
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS1Padding");
        // 对Cipher初始化,加密模式
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // 加密data
        byte[] cipherByte = cipher.doFinal(data);
        return Base64.encodeToString(cipherByte, Base64.DEFAULT);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

NoSuchPaddingException (javax.crypto.NoSuchPaddingException)259 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)237 InvalidKeyException (java.security.InvalidKeyException)216 Cipher (javax.crypto.Cipher)187 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)181 BadPaddingException (javax.crypto.BadPaddingException)180 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)119 SecretKeySpec (javax.crypto.spec.SecretKeySpec)91 IOException (java.io.IOException)83 IvParameterSpec (javax.crypto.spec.IvParameterSpec)66 SecretKey (javax.crypto.SecretKey)45 KeyStoreException (java.security.KeyStoreException)40 CertificateException (java.security.cert.CertificateException)40 UnrecoverableKeyException (java.security.UnrecoverableKeyException)35 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)27 NoSuchProviderException (java.security.NoSuchProviderException)27 GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)18 FileNotFoundException (java.io.FileNotFoundException)16 SecureRandom (java.security.SecureRandom)16