-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimize correct examples for misuses
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
Showing
42 changed files
with
326 additions
and
5,932 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.