Search in sources :

Example 61 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project derby by apache.

the class EncryptionManager method decryptData.

// This method decrypts the usreid/password with the middle 8 bytes of
// the generated secret key and an encryption token. Then it returns the
// decrypted data in a byte array.
// plainText   The byte array form userid/password to encrypt.
// initVector  The byte array which is used to calculate the
// encryption token.
// targetPublicKey   DERBY' public key.
// Returns the decrypted data in a byte array.
public byte[] decryptData(byte[] cipherText, int securityMechanism, byte[] initVector, byte[] targetPublicKey) throws SqlException {
    byte[] plainText = null;
    Key key = null;
    if (token_ == null) {
        token_ = calculateEncryptionToken(securityMechanism, initVector);
    }
    try {
        if (secKey_ == null) {
            // use this encryption key to initiate a SecretKeySpec object
            secKey_ = generatePrivateKey(targetPublicKey);
            SecretKeySpec desKey = new SecretKeySpec(secKey_, "DES");
            key = desKey;
        } else {
            // use this encryption key to initiate a SecretKeySpec object
            DESKeySpec desKey = new DESKeySpec(secKey_);
            if (secretKeyFactory_ == null) {
                secretKeyFactory_ = SecretKeyFactory.getInstance("DES", providerName);
            }
            key = secretKeyFactory_.generateSecret(desKey);
        }
        // We use DES in CBC mode because this is the mode used in PROTOCOL. The
        // encryption mode has to be consistent for encryption and decryption.
        // CBC mode requires an initialization vector(IV) parameter. In CBC mode
        // we need to initialize the Cipher object with an IV, which can be supplied
        // using the javax.crypto.spec.IvParameterSpec class.
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", providerName);
        // generate a IVParameterSpec object and use it to initiate the
        // Cipher object.
        IvParameterSpec ivParam = new IvParameterSpec(token_);
        // initiate the Cipher using encryption mode, encryption key and the
        // IV parameter.
        cipher.init(Cipher.DECRYPT_MODE, key, ivParam);
        // Execute the final phase of encryption
        plainText = cipher.doFinal(cipherText);
    } catch (NoSuchPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_NO_SUCH_PADDING));
    } catch (BadPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_BAD_PADDING));
    } catch (IllegalBlockSizeException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_ILLEGAL_BLOCK_SIZE));
    } catch (GeneralSecurityException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.SECURITY_EXCEPTION_ENCOUNTERED), e);
    }
    return plainText;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) PublicKey(java.security.PublicKey) Key(java.security.Key) DHPublicKey(javax.crypto.interfaces.DHPublicKey)

Example 62 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project derby by apache.

the class EncryptionManager method encryptData.

// This method encrypts the usreid/password with the middle 8 bytes of
// the generated secret key and an encryption token. Then it returns the
// encrypted data in a byte array.
// plainText   The byte array form userid/password to encrypt.
// initVector  The byte array which is used to calculate the
// encryption token.
// targetPublicKey   DERBY' public key.
// Returns the encrypted data in a byte array.
public byte[] encryptData(byte[] plainText, int securityMechanism, byte[] initVector, byte[] targetPublicKey) throws SqlException {
    byte[] cipherText = null;
    Key key = null;
    if (token_ == null) {
        token_ = calculateEncryptionToken(securityMechanism, initVector);
    }
    try {
        if (secKey_ == null) {
            // use this encryption key to initiate a SecretKeySpec object
            secKey_ = generatePrivateKey(targetPublicKey);
            SecretKeySpec desKey = new SecretKeySpec(secKey_, "DES");
            key = desKey;
        } else {
            // use this encryption key to initiate a SecretKeySpec object
            DESKeySpec desKey = new DESKeySpec(secKey_);
            if (secretKeyFactory_ == null) {
                secretKeyFactory_ = SecretKeyFactory.getInstance("DES", providerName);
            }
            key = secretKeyFactory_.generateSecret(desKey);
        }
        // We use DES in CBC mode because this is the mode used in PROTOCOL. The
        // encryption mode has to be consistent for encryption and decryption.
        // CBC mode requires an initialization vector(IV) parameter. In CBC mode
        // we need to initialize the Cipher object with an IV, which can be supplied
        // using the javax.crypto.spec.IvParameterSpec class.
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", providerName);
        // generate a IVParameterSpec object and use it to initiate the
        // Cipher object.
        IvParameterSpec ivParam = new IvParameterSpec(token_);
        // initiate the Cipher using encryption mode, encryption key and the
        // IV parameter.
        cipher.init(Cipher.ENCRYPT_MODE, key, ivParam);
        // Execute the final phase of encryption
        cipherText = cipher.doFinal(plainText);
    } catch (NoSuchPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_NO_SUCH_PADDING));
    } catch (BadPaddingException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_BAD_PADDING));
    } catch (IllegalBlockSizeException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.CRYPTO_ILLEGAL_BLOCK_SIZE));
    } catch (GeneralSecurityException e) {
        throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.SECURITY_EXCEPTION_ENCOUNTERED), e);
    }
    return cipherText;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) PublicKey(java.security.PublicKey) Key(java.security.Key) DHPublicKey(javax.crypto.interfaces.DHPublicKey)

Example 63 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project FireAPI by FireBlade-Serv.

the class BigBrotherListener method onPacketReceive.

@Override
public void onPacketReceive(Packet packet) {
    try {
        ConnectionHandler server = packet.getConnection();
        String name = (server.getName().equals("default-name")) ? server.getIP() : server.getName();
        if (!(packet instanceof PacketPing)) {
            if (!(packet instanceof PacketSpyAction)) {
                System.out.println("[BigBrother] Packet reçu : " + name + " -> " + packet.getClass().getSimpleName());
            } else {
                System.out.println("[BigBrother] Packet reçu : " + name + " -> " + packet.getClass().getSimpleName() + " / " + ((PacketSpyAction) packet).getAction().name());
            }
        }
        if (server.isLogged().equals(LoginResult.NOT_LOGGED)) {
            if (packet instanceof PacketLogin) {
                PacketLogin pl = (PacketLogin) packet;
                try {
                    String cpass = pl.decryptPass(this.log.getKey());
                    System.out.println("[BigBrother] PacketLogin reçu, valeur du PASS (crypt) : " + pl.getCryptPassword());
                    System.out.println("[BigBrother] PacketLogin reçu, valeur du PASS (D_key) : " + cpass);
                    if (cpass.equals(this.log.getPassword())) {
                        server.sendPacket(new PacketText("[BigBrother] Connection réussie !"));
                        server.setLoginResult(LoginResult.LOGGED);
                        listeners.callOnConnectionSuccessfullServerListener(server);
                    } else {
                        server.sendMessage("[BigBrother] Mot de passe incorect !");
                        if (limit.containsKey(server)) {
                            limit.replace(server, limit.get(server) + 1);
                            if (limit.get(server) == 3) {
                                server.sendPacket(new PacketText("[BigBrother] Trop de tentatives ratées ! Déconnexion ..."));
                                server.close();
                            }
                        } else {
                            limit.put(server, 1);
                        }
                    }
                } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | IOException e) {
                    return;
                }
            } else {
                server.sendPacket(new PacketText("[BigBrother] Vous devez vous connecter !"));
                return;
            }
        }
        if (packet instanceof PacketCommand) {
            this.listeners.callOnCommandServerListener((PacketCommand) packet);
        } else if (packet instanceof PacketVersion) {
            PacketVersion ver = (PacketVersion) packet;
            if (!this.connectionstype.containsKey(VersionType.SPIGOT_VERSION)) {
                if (ver.getType().equals(VersionType.SPIGOT_VERSION)) {
                    List<ConnectionHandler> list = new CopyOnWriteArrayList<>();
                    list.add(server);
                    this.connectionstype.put(VersionType.SPIGOT_VERSION, list);
                }
            } else {
                if (ver.getType().equals(VersionType.SPIGOT_VERSION)) {
                    List<ConnectionHandler> list = this.connectionstype.get(VersionType.SPIGOT_VERSION);
                    list.add(server);
                    this.connectionstype.replace(VersionType.SPIGOT_VERSION, list);
                }
            }
            if (!this.connectionstype.containsKey(VersionType.BUNGEECORD_VERSION)) {
                if (ver.getType().equals(VersionType.BUNGEECORD_VERSION)) {
                    List<ConnectionHandler> list = new CopyOnWriteArrayList<>();
                    list.add(server);
                    this.connectionstype.put(VersionType.BUNGEECORD_VERSION, list);
                }
            } else {
                if (ver.getType().equals(VersionType.BUNGEECORD_VERSION)) {
                    List<ConnectionHandler> list = this.connectionstype.get(VersionType.BUNGEECORD_VERSION);
                    list.add(server);
                    this.connectionstype.replace(VersionType.BUNGEECORD_VERSION, list);
                }
            }
        } else if (packet instanceof PacketFriends) {
            PacketFriends pf = (PacketFriends) packet;
            if (pf.getAction() instanceof FriendsActionTransmetterGUI) {
                FriendsActionTransmetterGUI fa = (FriendsActionTransmetterGUI) pf.getAction();
                if (fa.to().equals(VersionType.SPIGOT_VERSION)) {
                    for (ConnectionHandler chs : this.connected) {
                        if (chs.getName().equals(fa.getServerDestination())) {
                            chs.sendPacket(packet);
                        }
                    }
                }
            }
        } else if (packet instanceof PacketPlayerPing) {
            PacketPlayerPing pp = (PacketPlayerPing) packet;
            if (pp.getState().equals(PingState.INIT_SERVER)) {
                pp.setState(PingState.BUNGEE_REQUEST);
                try {
                    this.getServerConnectionByNameUnsafe(VersionType.BUNGEECORD_VERSION, "main-bungeecord").sendPacket(pp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if (packet instanceof PacketSpyAction) {
            PacketSpyAction spy = (PacketSpyAction) packet;
            Calendar cal = GregorianCalendar.getInstance();
            cal.setTime(new Date());
            if (this.gs.ifDataFileExists(spy.getPlayerName())) {
                this.gs.updateDataFile(spy.getPlayerName(), BigBrotherSpyUtils.mergeHistory(this.gs.getHistory(spy.getPlayerName()), spy));
            } else {
                BigBrotherSpyHistory gh = new BigBrotherSpyHistory(spy.getPlayerName(), spy.getIP(), BigBrotherSpyUtils.toFireCalendar(cal));
                gh.putMessage(BigBrotherSpyUtils.toFireCalendar(spy.getActionDate()), spy.getAction(), spy.getFormatedMsg(), spy.getRawMsg());
                this.gs.createNewDataFile(spy.getPlayerName(), gh);
            }
        } else if (packet instanceof PacketSpyHistoryGetter) {
            PacketSpyHistoryGetter get = (PacketSpyHistoryGetter) packet;
            if (get.getState().equals(BigBrotherSpyHistoryGetterState.REQUEST)) {
                get.setState(BigBrotherSpyHistoryGetterState.SEND);
                get.setHistory(this.gs.getHistory(get.getPlayerName()));
                server.sendPacket(get);
            }
        } else if (packet instanceof PacketBigBrotherAC) {
            PacketBigBrotherAC gacp = (PacketBigBrotherAC) packet;
            if (gacp.getType().equals(BigBrotherTypeAC.CHEAT_DETECTION)) {
                if (gacp.getTODO().equals(BigBrotherActionAC.INFORM_STAFF)) {
                    this.getServerConnectionByNameUnsafe(VersionType.BUNGEECORD_VERSION, "main-bungeecord").sendPacket(gacp);
                }
            }
        }
    } catch (Exception e) {
        return;
    }
}
Also used : PacketCommand(fr.glowstoner.connectionsapi.network.packets.command.PacketCommand) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) PacketVersion(fr.glowstoner.fireapi.bigbrother.console.packets.PacketVersion) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) PacketPlayerPing(fr.glowstoner.fireapi.bigbrother.console.packets.ping.PacketPlayerPing) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) PacketText(fr.glowstoner.connectionsapi.network.packets.PacketText) FriendsActionTransmetterGUI(fr.glowstoner.fireapi.bungeecord.friends.packets.action.FriendsActionTransmetterGUI) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) BigBrotherSpyHistory(fr.glowstoner.fireapi.bigbrother.spy.BigBrotherSpyHistory) PacketPing(fr.glowstoner.connectionsapi.network.packets.PacketPing) PacketSpyAction(fr.glowstoner.fireapi.bigbrother.spy.packets.PacketSpyAction) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) PacketLogin(fr.glowstoner.connectionsapi.network.packets.login.PacketLogin) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) PacketFriends(fr.glowstoner.fireapi.bungeecord.friends.packets.PacketFriends) Date(java.util.Date) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ConnectionHandler(fr.glowstoner.connectionsapi.network.ConnectionHandler) PacketSpyHistoryGetter(fr.glowstoner.fireapi.bigbrother.spy.packets.PacketSpyHistoryGetter) PacketBigBrotherAC(fr.glowstoner.fireapi.bigbrother.ac.packet.PacketBigBrotherAC)

Example 64 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project FireAPI by FireBlade-Serv.

the class FireCrypt method decrypt.

public String decrypt(String text) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, this.key);
    String en = null;
    try {
        en = new String(c.doFinal(Base64.getDecoder().decode(text)));
    } catch (Exception ex) {
        en = new String(c.update(Base64.getDecoder().decode(text)));
    }
    return en;
}
Also used : Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 65 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project Pix-Art-Messenger by kriztan.

the class XmppAxolotlMessage method encrypt.

public void encrypt(String plaintext) throws CryptoFailedException {
    try {
        SecretKey secretKey = new SecretKeySpec(innerKey, KEYTYPE);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance(CIPHERMODE, PROVIDER);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        this.ciphertext = cipher.doFinal(Config.OMEMO_PADDING ? getPaddedBytes(plaintext) : plaintext.getBytes());
        if (Config.PUT_AUTH_TAG_INTO_KEY && this.ciphertext != null) {
            this.authtagPlusInnerKey = new byte[16 + 16];
            byte[] ciphertext = new byte[this.ciphertext.length - 16];
            System.arraycopy(this.ciphertext, 0, ciphertext, 0, ciphertext.length);
            System.arraycopy(this.ciphertext, ciphertext.length, authtagPlusInnerKey, 16, 16);
            System.arraycopy(this.innerKey, 0, authtagPlusInnerKey, 0, this.innerKey.length);
            this.ciphertext = ciphertext;
        }
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
        throw new CryptoFailedException(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException)

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