Skip to content

Instantly share code, notes, and snippets.

@Hritik14
Last active November 20, 2023 09:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hritik14/48cb78aad9b9048066e1751bcfaa5994 to your computer and use it in GitHub Desktop.
Save Hritik14/48cb78aad9b9048066e1751bcfaa5994 to your computer and use it in GitHub Desktop.
Encrypt aes 256 gcm in Java Springboot and decrypt in Nodejs
const {
pbkdf2Sync,
createDecipheriv,
} = require('crypto');
const algorithm = 'aes-256-gcm';
const password = 'ABCD';
const salt_hex = "FFFFFFFFFFFFFFFF"
const java_encrypted_base64 = "r8URnR8DLWFH1ipBoREtkwX6wi5x/japz85d+3e2BBWCrVs="
const salt = Buffer.from(salt_hex, "hex")
const key = pbkdf2Sync(password, salt, 1024, 32, "sha1")
encrypted = Buffer.from(java_encrypted_base64, "base64")
iv = encrypted.slice(0, 16)
auth = encrypted.slice(encrypted.length - 16, encrypted.length)
encrypted = encrypted.slice(16, encrypted.length - 16)
const decipher = createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(encrypted, null, 'utf8');
decipher.setAuthTag(auth)
decrypted += decipher.final('utf8');
console.log(decrypted);
package com.example.springboot;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.BytesEncryptor;
import org.springframework.util.Base64Utils;
import org.apache.commons.codec.binary.Hex;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
String plaintext = "ABC";
String encrypted_b64;
String encrypted_hex;
BytesEncryptor encryptor = Encryptors.stronger("ABCD", "FFFFFFFFFFFFFFFF");
byte[] encrypted = encryptor.encrypt(plaintext.getBytes());
encrypted_b64 = Base64Utils.encodeToString(encrypted);
encrypted_hex = Hex.encodeHexString(encrypted);
String out="AES\n" +
"Plaintext: " + plaintext + "\n" +
"Encrypted: " + encrypted_b64 + "\n" +
"Encrypted (Hex): " + encrypted_hex + "\n";
System.out.println(out);
return out;
}
}


      Java encryptedr(aes-256-gcm) springboot "stronger"
     ┌─────────────────────────┬───────────────────────────────────────────────┬───────────────────────┐
     │                         │                                               │                       │
     │                         │                                               │                       │
     │   IV (16 bytes)         │        Encrypted text                         │    Auth tag (16 bytes)│
     │                         │                                               │                       │
     │                         │                                               │                       │
     │                         │                                               │                       │
     └─────────────────────────┴───────────────────────────────────────────────┴───────────────────────┘








      Java encryptedr(aes-256-cbc) springboot "standard"
     ┌─────────────────────────┬───────────────────────────────────────────────────────────────────────┐
     │                         │                                                                       │
     │                         │                                                                       │
     │   IV (16 bytes)         │        Encrypted text                                                 │
     │                         │                                                                       │
     │                         │                                                                       │
     │                         │                                                                       │
     └─────────────────────────┴───────────────────────────────────────────────────────────────────────┘


plugins {
id 'org.springframework.boot' version '2.5.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-test'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.45'
implementation 'commons-codec:commons-codec:1.15'
}
test {
useJUnitPlatform()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment