Git Product home page Git Product logo

jackson-json-crypto's Introduction

CodeQL Java CI with Gradle Maven Central

Jackson JSON Crypto Module

A Jackson module to support JSON encryption and decryption operations. Encryption is via AES. Key generation is password based.

Keyword: Jackson, JSON, AES, PKCS5, Encryption, Salt, Initialization Vector, IV, Java

Based on an idea from meltmedia

If you find this project useful, you may want to Buy me a Coffee! โ˜• Thanks ๐Ÿ‘

Build

Windows

gradlew clean build test

Linux

./gradlew clean build test

How to use

These examples are demonstrated in the CryptoDemo unit test class

Option 1 - Very Quick and Easy

Add the crypto module to your project. Common use case with a cipher of AES/CBC/PKCS5Padding and a key factory algorithm of PBKDF2WithHmacSHA512

Just supply a password.

ObjectMapper objectMapper = EncryptionService.getInstance("Password1");

Option 2 - Quick and Easy

Similar to Option 1, but you already have a ObjectMapper

ObjectMapper objectMapper = ...
EncryptionService encryptionService = 
   new EncryptionService(objectMapper, new PasswordCryptoContext("Password1");
objectMapper.registerModule(new CryptoModule().addEncryptionService(encryptionService));

Option 3 - Configure Everything

Where you just need full control.

// get an object mapper
ObjectMapper objectMapper = new ObjectMapper();
// set up a custom crypto context - Defines the interface to the crypto algorithms used
ICryptoContext cryptoContext = new PasswordCryptoContext("Password");
// The encryption service holds functionality to map clear to / from encrypted JSON
EncryptionService encryptionService = new EncryptionService(objectMapper, cryptoContext);
// Create a Jackson module and tell it about the encryption service
CryptoModule cryptoModule = new CryptoModule().addEncryptionService(encryptionService);
 // Tell Jackson about the new module
objectMapper.registerModule(cryptoModule);

Encrypt a field

Any field that is required to be encrypted has to be marked as such. This can be done by either annotating the getter() or by annotating the field definition.

This ...

    private String critical;
    ...
    @JsonProperty
    @Encrypt
    public String getCritical() {
        return this.critical;
    }

... or ...

    @JsonProperty
    @Encrypt
    private String critical;

Output JSON Format

{  
   "critical":{  
      "salt":"IRqsz99no75sx9SCGrzOSEdoMVw=",
      "iv":"bfKvxBhq7X5su9VtvDdOGQ==",
      "value":"pXWsFPzCnmPieitbGfkvofeQE3fj0Kb4mSP7e28+Jc0="
   }
}

Using Jenkins

The project includes a Jenkins file to control a pipeline build.

Include Using Maven

<dependency>
  <groupId>com.codingrodent</groupId>
  <artifactId>jackson-json-crypto</artifactId>
  <version>2.2.0</version>
</dependency>

Include Using Gradle

compile group: 'com.codingrodent', name: 'jackson-json-crypto', version: '2.2.0'

jackson-json-crypto's People

Contributors

codesqueak avatar shubhsingh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jackson-json-crypto's Issues

Decryption of data while retrieval not happening

I am using Jackson Json Crypto module v 1.1.0 for encryption as it is compatible with JDK 8. Encryption is working fine and data is getting saved in encrypted form but while deserializing, data is not getting back converted to actual data. I believe the @Encrypt annotation which is used and defined on the field is not getting triggered before the object mapper does deserializing. I have downloaded the decompiled library code as well and tried debugging, where i found deserializing module constructors are getting called initially but later the @OverRide deserialize function is not getting triggered.

Encrypted O/P
"order_id": { "salt": "Bf53VhtWMF95Cp7wLb8EJ9tRfIc=", "iv": "X83bkmfrvHk4SGCTJ+zx/g==", "value": "L8VEVKkEp0j7KG0cG2IFjHdNmMKcIxnLIb0+fiNqnDr2AXRrFgwJaaR6E8f5n7DI" }

Post-decryption it should be
"order_id": "abcd_!234"

But the O/P is coming the encrypted value as it is
"order_id": { "salt": "Bf53VhtWMF95Cp7wLb8EJ9tRfIc=", "iv": "X83bkmfrvHk4SGCTJ+zx/g==", "value": "L8VEVKkEp0j7KG0cG2IFjHdNmMKcIxnLIb0+fiNqnDr2AXRrFgwJaaR6E8f5n7DI" }

Information about lack of integrity protection missing | Security

Hey @codesqueak,

Awesome library, but I have noticed that you do not mention anything about the usage of this library. Since the AES/CBC is used, it does not offer any integrity protection of the ciphertext like MAC (malleable). If the library is wrongly used, e.g.
someone trust it to keep the integrity of the data, it might lead to the security issue in their application.

For the simplicity, I can show the simple example when someone was encrypting Social Security Number (SSN) when serializing to JSON. The developer took the false assumption that since the value is encrypted it cannot be tampered and trusted it for authentication or authorizing some actions in the system. Now, let's say that the IV, salt and Value (cipher text) are in the control of the user/attacker (e.g. those values are encoded and stored in the Cookie for keeping user's session).
The attacker can change the IV, so that the cipher-text will be decrypted to the plain-text of the attacker's choice.

Below you can see the Java code:

        ObjectMapper objectMapper = EncryptionService.getInstance("Password1");
         // Sample Good SSN: 790714615 - attacker knows that cause he has set up the account
        String json1 = "{\"ssn\":{\"salt\":\"uzaYY1PaEpWS6SC9lUWKWw==\",\"iv\":\"6mCYbjLB2mEk1gsWRqiWiw==\",\"value\":\"JZpjE/JqkrdOi1JcGAtP9w==\"}}";
        SSNGetterPoJo pojo1 = objectMapper.readValue(json1, SSNGetterPoJo.class);
        System.out.println(pojo1.getSSN());
        // IV changed which would result in fake SSN after decryption: 111111111
        String json2 = "{\"ssn\":{\"salt\":\"uzaYY1PaEpWS6SC9lUWKWw==\",\"iv\":\"6maQbzTB32Yk0gsWRqiWiw==\",\"value\":\"JZpjE/JqkrdOi1JcGAtP9w==\"}}";
        SSNGetterPoJo pojo2 = objectMapper.readValue(json2, SSNGetterPoJo.class);
        System.out.println(pojo2.getSSN());

SSNGetterPoJo.java is almost the same as SecureGetterPojo.java

Here is the python3 code that I have used to generate the IV which changes the plain-text after decryption:

import base64

def xors(s1, s2):
    return bytes(ord(a) ^ ord(b) for a, b in zip(s1,s2))

def xorb (s1, s2):
    return bytes(a^b for a,b in zip(s1,s2))

a1 = xors("790714615", "111111111")
iv = "6mCYbjLB2mEk1gsWRqiWiw=="

b1 = base64.b64decode(iv)
new_iv = b1[0:1] + xorb(b1[1:],a1) + b1[10:]

print(base64.b64encode(new_iv))

It would be worth to mention that in the description of this library or if you want to offer the integrity protection change the cipher to the one offering MAC like AES/GCM. If you decide to change the cipher to AES/GCM, then it would be cool to create the Security Advisory and assign CVE-ID for that issue to me as the reporter.

Error on serialization with @JsonIdentityInfo on partial encrypted class

Hi!
I have a class which has some properties annotated with @Encrypt.
Also I want to use @JsonIdentityInfo(generator = JSOGGenerator.class) annotation on classes to handle potential circular references and also minimising JSON output.

However the conjunction may result in Objects whcih have id X to be decrypted, and later be referenced by another cleartext property as "@ref":X. This reference cannot be resolved until deserialized.

Since the encrypted part should not be accesible /manipulatable in the frontend (e.g. a browser) but the visible properties of the response should be visible this is an issue.

{
"@id" : "1",
"somEncryptedProperty" : {
"iv" : "OgEe3ag[....]sbQ==",
"salt" : "gHnRbP[....]41MpFOgA=",
"value" : "m/Xw9/hMG4[....]WO58LYVrDsmU="
},
"broken_visibleProperty" : {
"@ref" : "5"
},
"somOtherEncryptedThing" : {
"iv" : "OgEe[....]EnsbQ==",
"salt" : "H8N[....]pFOgA=",
"value" : "ltDDA/RnYT9szpCZklXYwA8XbQ0Rcvtxy1sV[....]3PzVXs"
},
"somthing_visible" : {
"@id" : "7",
"event" : null,
"id" : "_eeClIR47EemIFYn30KSx0A",
"inputs" : { }
}
}

DES encryption algo with 64 bit keyLength is not supported

Hi,

I want to use DES algo but only AES algo with 256 bit keyLength is hardcoded into createSecretKeySpec method of BaseCryptoContext class.

I would suggest to allow changing these values with parameters just like salt and password.

Also in same method iterationCount should also be allowed to change using parameter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.