Skip to content

Commit

Permalink
Minimize correct examples for misuses
Browse files Browse the repository at this point in the history
Minimize corrected examples for misuses

Minimize correct example for instagram4j

Add correct usages for jeesuite-libs

Minimized corrected examples

Add correct usage for dragonite-java

Add minimal corrected examples
  • Loading branch information
akwick committed May 15, 2019
1 parent 5cc1ee5 commit 8cbfda3
Show file tree
Hide file tree
Showing 42 changed files with 326 additions and 5,932 deletions.
80 changes: 4 additions & 76 deletions data/dragonite-java/misuses/2/correct-usages/AES.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,7 @@
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;

public class AES {
public void encrypt(String strDataToEncrypt) {
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();

final int AES_KEYLENGTH = 128;
byte[] iv = new byte[AES_KEYLENGTH / 8];
SecureRandom prng = new SecureRandom();
prng.nextBytes(iv);

Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS7Padding");
aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey,
new IvParameterSpec(iv));

byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt);
}

catch (NoSuchAlgorithmException noSuchAlgo) {
}

catch (NoSuchPaddingException noSuchPad) {
}

catch (InvalidKeyException invalidKey) {
}

catch (BadPaddingException badPadding) {
}

catch (IllegalBlockSizeException illegalBlockSize) {
}

catch (InvalidAlgorithmParameterException invalidParam) {
}
}

public void decrypt(byte[] cipherText, SecretKey secretKey, byte[] iv){
try {
Cipher aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS7Padding");
aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
byte[] byteDecryptedText = aesCipherForDecryption.doFinal(cipherText);
String decryptedText = new String(byteDecryptedText);
}

catch (NoSuchAlgorithmException noSuchAlgo) {
}

catch (NoSuchPaddingException noSuchPad) {
}

catch (InvalidKeyException invalidKey) {
}

catch (BadPaddingException badPadding) {
}

catch (IllegalBlockSizeException illegalBlockSize) {
}

catch (InvalidAlgorithmParameterException invalidParam) {
}
}
ConstructRandomizedIV(offset int, len int) {
random = SecureRandom();
iv = new IvParameterSpec(random);
}
Loading

0 comments on commit 8cbfda3

Please sign in to comment.