Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tracker.service.impl;
- import lombok.extern.slf4j.Slf4j;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.spec.SecretKeySpec;
- import java.security.InvalidKeyException;
- import java.security.Key;
- import java.time.Instant;
- import java.util.Base64;
- import java.util.UUID;
- @Slf4j
- public class CryptoExample {
- private static Base64.Encoder encoder = Base64.getUrlEncoder();
- private static Base64.Decoder decoder = Base64.getUrlDecoder();
- private static final String SIGNING_KEY = "CD1DB294C533DCE6"; // 128 bit key
- private static final Key AES_KEY = new SecretKeySpec(SIGNING_KEY.getBytes(), "AES");
- private static final String SECRET_VALUE = String.format("%s:%s", UUID.randomUUID().toString(), Instant.now().getEpochSecond());
- public static void main(String[] args) throws Exception {
- // Create key and cipher
- Cipher cipher = Cipher.getInstance("AES");
- // encrypt the text
- String encoded = encrypt(SECRET_VALUE, cipher);
- log.info("Encrypted: {}", encoded);
- // decrypt the text
- String decryptedText = decrypt(encoded, cipher);
- log.info("Decrypted: {}",decryptedText);
- log.info("Does decrypted text matches: {}", decryptedText.equals(SECRET_VALUE));
- }
- private static String encrypt(String text, Cipher cipher) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
- cipher.init(Cipher.ENCRYPT_MODE, AES_KEY);
- byte[] encrypted = cipher.doFinal(text.getBytes());
- return encoder.encodeToString(encrypted);
- }
- private static String decrypt(String encrypted64, Cipher cipher) throws Exception {
- byte[] encrypted = decoder.decode(encrypted64);
- cipher.init(Cipher.DECRYPT_MODE, AES_KEY);
- return new String(cipher.doFinal(encrypted));
- }
- //=================BouncyCastle=================
- /* public static void main(String[] args) throws Exception {
- Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
- Key key = new SecretKeySpec(SIGNING_KEY.getBytes(), "AES");
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
- log.info(new String(SECRET_VALUE.getBytes()));
- // encryption pass
- cipher.init(Cipher.ENCRYPT_MODE, key);
- byte[] cipherText = new byte[cipher.getOutputSize(SECRET_VALUE.length())];
- int ctLength = cipher.update(SECRET_VALUE.getBytes(), 0, SECRET_VALUE.length(), cipherText, 0);
- ctLength += cipher.doFinal(cipherText, ctLength);
- log.info("Encrypted: {}", encoder.encodeToString(cipherText));
- log.info("ct: {}", ctLength);
- // decryption pass
- cipher.init(Cipher.DECRYPT_MODE, key);
- byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
- int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
- ptLength += cipher.doFinal(plainText, ptLength);
- log.info("Decrypted: {}", new String(plainText));
- log.info("pt: {}", ptLength);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement