Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
Create Enc.java
Browse files Browse the repository at this point in the history
  • Loading branch information
mishraomp authored Oct 22, 2023
1 parent 4cd3f60 commit 4767b5b
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions vault-test/Enc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;
public class Enc {
public static void main(String[] args) throws Exception {
if(args == null){
System.err.println("args is null");
System.exit(1);
}
if(args.length < 3){
System.err.println("args needs to provide key, iv and text to encrypt");
System.exit(1);
}
// Your AES-256 key (32 bytes)
String key = args[0];

// The initialization vector (IV) (16 bytes)
String iv = args[1];

// The string to be encrypted
String plaintext = args[2];

// Encrypt the string
byte[] encryptedBytes = encrypt(plaintext, key, iv);
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("Encrypted: " + encryptedText);
System.out.println("\n");

// Decrypt the string
String decryptedText = decrypt(Base64.getDecoder().decode(encryptedText), key, iv);
System.out.println("Decrypted: " + decryptedText);
}

public static byte[] encrypt(String plaintext, String key, String iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec initVector = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, secretKey, initVector);
return cipher.doFinal(plaintext.getBytes());
}

public static String decrypt(byte[] encryptedBytes, String key, String iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec initVector = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey, initVector);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
}

0 comments on commit 4767b5b

Please sign in to comment.