Search in sources :

Example 41 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project xDrip by NightscoutFoundation.

the class G5CollectionService method calculateHash.

@SuppressLint("GetInstance")
private synchronized byte[] calculateHash(byte[] data) {
    if (data.length != 8) {
        Log.e(TAG, "Decrypt Data length should be exactly 8.");
        return null;
    }
    byte[] key = cryptKey();
    if (key == null)
        return null;
    byte[] doubleData;
    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.put(data);
    bb.put(data);
    doubleData = bb.array();
    Cipher aesCipher;
    try {
        aesCipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] aesBytes = aesCipher.doFinal(doubleData, 0, doubleData.length);
        bb = ByteBuffer.allocate(8);
        bb.put(aesBytes, 0, 8);
        return bb.array();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : 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) ByteBuffer(java.nio.ByteBuffer) SuppressLint(android.annotation.SuppressLint)

Example 42 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project xDrip-plus by jamorham.

the class G5CollectionService method calculateHash.

@SuppressLint("GetInstance")
private synchronized byte[] calculateHash(byte[] data) {
    if (data.length != 8) {
        Log.e(TAG, "Decrypt Data length should be exactly 8.");
        return null;
    }
    byte[] key = cryptKey();
    if (key == null)
        return null;
    byte[] doubleData;
    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.put(data);
    bb.put(data);
    doubleData = bb.array();
    Cipher aesCipher;
    try {
        aesCipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] aesBytes = aesCipher.doFinal(doubleData, 0, doubleData.length);
        bb = ByteBuffer.allocate(8);
        bb.put(aesBytes, 0, 8);
        return bb.array();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : 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) ByteBuffer(java.nio.ByteBuffer) SuppressLint(android.annotation.SuppressLint)

Example 43 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project KeePassDX by Kunzisoft.

the class ImporterV3 method openDatabase.

public PwDatabaseV3 openDatabase(InputStream inStream, String password, InputStream kfIs, UpdateStatus status, long roundsFix) throws IOException, InvalidDBException {
    PwDatabaseV3 newManager;
    // Load entire file, most of it's encrypted.
    int fileSize = inStream.available();
    // Pad with a blocksize (Twofish uses 128 bits), since Android 4.3 tries to write more to the buffer
    byte[] filebuf = new byte[fileSize + 16];
    inStream.read(filebuf, 0, fileSize);
    inStream.close();
    // Parse header (unencrypted)
    if (fileSize < PwDbHeaderV3.BUF_SIZE)
        throw new IOException("File too short for header");
    PwDbHeaderV3 hdr = new PwDbHeaderV3();
    hdr.loadFromFile(filebuf, 0);
    if ((hdr.signature1 != PwDbHeader.PWM_DBSIG_1) || (hdr.signature2 != PwDbHeaderV3.DBSIG_2)) {
        throw new InvalidDBSignatureException();
    }
    if (!hdr.matchesVersion()) {
        throw new InvalidDBVersionException();
    }
    status.updateMessage(R.string.creating_db_key);
    newManager = createDB();
    newManager.setMasterKey(password, kfIs);
    // Select algorithm
    if ((hdr.flags & PwDbHeaderV3.FLAG_RIJNDAEL) != 0) {
        newManager.algorithm = PwEncryptionAlgorithm.Rjindal;
    } else if ((hdr.flags & PwDbHeaderV3.FLAG_TWOFISH) != 0) {
        newManager.algorithm = PwEncryptionAlgorithm.Twofish;
    } else {
        throw new InvalidAlgorithmException();
    }
    // Copy for testing
    newManager.copyHeader(hdr);
    newManager.numKeyEncRounds = hdr.numKeyEncRounds;
    newManager.name = "KeePass Password Manager";
    // Generate transformedMasterKey from masterKey
    newManager.makeFinalKey(hdr.masterSeed, hdr.transformSeed, newManager.numKeyEncRounds);
    status.updateMessage(R.string.decrypting_db);
    // Initialize Rijndael algorithm
    Cipher cipher;
    try {
        if (newManager.algorithm == PwEncryptionAlgorithm.Rjindal) {
            cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding");
        } else if (newManager.algorithm == PwEncryptionAlgorithm.Twofish) {
            cipher = CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING");
        } else {
            throw new IOException("Encryption algorithm is not supported");
        }
    } catch (NoSuchAlgorithmException e1) {
        throw new IOException("No such algorithm");
    } catch (NoSuchPaddingException e1) {
        throw new IOException("No such pdading");
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(newManager.finalKey, "AES"), new IvParameterSpec(hdr.encryptionIV));
    } catch (InvalidKeyException e1) {
        throw new IOException("Invalid key");
    } catch (InvalidAlgorithmParameterException e1) {
        throw new IOException("Invalid algorithm parameter.");
    }
    // Decrypt! The first bytes aren't encrypted (that's the header)
    int encryptedPartSize;
    try {
        encryptedPartSize = cipher.doFinal(filebuf, PwDbHeaderV3.BUF_SIZE, fileSize - PwDbHeaderV3.BUF_SIZE, filebuf, PwDbHeaderV3.BUF_SIZE);
    } catch (ShortBufferException e1) {
        throw new IOException("Buffer too short");
    } catch (IllegalBlockSizeException e1) {
        throw new IOException("Invalid block size");
    } catch (BadPaddingException e1) {
        throw new InvalidPasswordException();
    }
    // Copy decrypted data for testing
    newManager.copyEncrypted(filebuf, PwDbHeaderV3.BUF_SIZE, encryptedPartSize);
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("No SHA-256 algorithm");
    }
    NullOutputStream nos = new NullOutputStream();
    DigestOutputStream dos = new DigestOutputStream(nos, md);
    dos.write(filebuf, PwDbHeaderV3.BUF_SIZE, encryptedPartSize);
    dos.close();
    byte[] hash = md.digest();
    if (!Arrays.equals(hash, hdr.contentsHash)) {
        Log.w("KeePassDroid", "Database file did not decrypt correctly. (checksum code is broken)");
        throw new InvalidPasswordException();
    }
    // Import all groups
    int pos = PwDbHeaderV3.BUF_SIZE;
    PwGroupV3 newGrp = new PwGroupV3();
    for (int i = 0; i < hdr.numGroups; ) {
        int fieldType = LEDataInputStream.readUShort(filebuf, pos);
        pos += 2;
        int fieldSize = LEDataInputStream.readInt(filebuf, pos);
        pos += 4;
        if (fieldType == 0xFFFF) {
            // End-Group record.  Save group and count it.
            newGrp.populateBlankFields(newManager);
            newManager.groups.add(newGrp);
            newGrp = new PwGroupV3();
            i++;
        } else {
            readGroupField(newManager, newGrp, fieldType, filebuf, pos);
        }
        pos += fieldSize;
    }
    // Import all entries
    PwEntryV3 newEnt = new PwEntryV3();
    for (int i = 0; i < hdr.numEntries; ) {
        int fieldType = LEDataInputStream.readUShort(filebuf, pos);
        int fieldSize = LEDataInputStream.readInt(filebuf, pos + 2);
        if (fieldType == 0xFFFF) {
            // End-Group record.  Save group and count it.
            newEnt.populateBlankFields(newManager);
            newManager.entries.add(newEnt);
            newEnt = new PwEntryV3();
            i++;
        } else {
            readEntryField(newManager, newEnt, filebuf, pos);
        }
        pos += 2 + 4 + fieldSize;
    }
    newManager.constructTree(null);
    return newManager;
}
Also used : PwEntryV3(com.keepassdroid.database.PwEntryV3) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PwDbHeaderV3(com.keepassdroid.database.PwDbHeaderV3) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) InvalidDBSignatureException(com.keepassdroid.database.exception.InvalidDBSignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) InvalidDBVersionException(com.keepassdroid.database.exception.InvalidDBVersionException) PwDatabaseV3(com.keepassdroid.database.PwDatabaseV3) InvalidAlgorithmException(com.keepassdroid.database.exception.InvalidAlgorithmException) PwGroupV3(com.keepassdroid.database.PwGroupV3) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DigestOutputStream(java.security.DigestOutputStream) ShortBufferException(javax.crypto.ShortBufferException) InvalidPasswordException(com.keepassdroid.database.exception.InvalidPasswordException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest) NullOutputStream(com.keepassdroid.stream.NullOutputStream)

Example 44 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project sldeditor by robward-scisys.

the class EncryptedPropertiesApache method initialise.

/* (non-Javadoc)
     * @see com.sldeditor.common.property.EncryptedPropertiesInterface#initialise(java.lang.String)
     */
@Override
public void initialise(String password) {
    PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
    SecretKeyFactory kf;
    try {
        kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(password.toCharArray()));
        encrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        decrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        encrypter.init(Cipher.ENCRYPT_MODE, k, ps);
        decrypter.init(Cipher.DECRYPT_MODE, k, ps);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 45 with NoSuchPaddingException

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

the class RSA method decrypt.

/**
 * 解密
 *
 * @param key
 * @param encodedText
 * @return
 */
public static String decrypt(PrivateKey key, byte[] encodedText) {
    ByteArrayOutputStream out = null;
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        int inputLen = encodedText.length;
        if (inputLen <= MAX_DECRYPT_BLOCK) {
            return new String(cipher.doFinal(encodedText), DEFAULT_CHARSET);
        }
        out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encodedText, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encodedText, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        return new String(out.toByteArray(), DEFAULT_CHARSET);
    } 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)

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