Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is there any nodejs equivalent implementation? #30

Open
xiangnanscu opened this issue Oct 1, 2022 · 1 comment
Open

is there any nodejs equivalent implementation? #30

xiangnanscu opened this issue Oct 1, 2022 · 1 comment

Comments

@xiangnanscu
Copy link

ngx_encrypted_session will plant an expiration time this part is hard. I don't know how to implement it in nodejs. I try to use this snippet to decrypt the string encrypted by encrypted-session-nginx-module, the output is a partial success:

>82^��/H�����j�EG��ɭ���tޡ�楠","permission":128,"username":"11111111","id":1}}c@9}

the nodejs decrypt snippet

import crypto from "crypto";

const ENC_KEY = "xxxxxxxx"; // set random encryption key
const IV = "xxxxxxx"; // set random initialisation vector
// ENC_KEY and IV can be generated as crypto.randomBytes(32).toString('hex');

// const phrase = "who let the dogs out";

const encrypt = (val: string) => {
  let cipher = crypto.createCipheriv("aes-256-cbc", ENC_KEY, IV);
  let encrypted = cipher.update(val, "utf8", "base64");
  encrypted += cipher.final("base64");
  return encrypted;
};

const decrypt = (encrypted: string) => {
  let decipher = crypto.createDecipheriv("aes-256-cbc", ENC_KEY, IV);
  let decrypted = decipher.update(encrypted, "base64", "utf8");
  return decrypted + decipher.final("utf8");
};
@paras5125
Copy link

sample code implementation in java

`
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

public class TimeBasedEncryption {

private static final String ENCRYPTION_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String SECRET_KEY = "your_key_16bytes";
private static final String INIT_VECTOR = "your_iv_16bytes";

public static void main(String[] args) {
    String ip = "192.168.0.1";
    String encryptedIP = encryptIP(ip);
    System.out.println("Encrypted IP: " + encryptedIP);

    String decryptedIP = decryptIP(encryptedIP);
    System.out.println("Decrypted IP: " + decryptedIP);
}

public static String encryptIP(String ip) {
    LocalDateTime expirationTime = LocalDateTime.now().plusHours(1);
    String timestamp = expirationTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    String message = timestamp + ip;

    try {
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(INIT_VECTOR.getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static String decryptIP(String encryptedIP) {
    try {
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedIP);
        SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(INIT_VECTOR.getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        String decryptedMessage = new String(decryptedBytes, StandardCharsets.UTF_8);

        // Extract timestamp and IP address
        LocalDateTime expirationTime = LocalDateTime.parse(decryptedMessage.substring(0, 14),
                DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        LocalDateTime currentTime = LocalDateTime.now();
        if (currentTime.isAfter(expirationTime)) {
            throw new RuntimeException("Expired encrypted IP");
        }
        return decryptedMessage.substring(14);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants