wokier / gwt-crypto

Automatically exported from code.google.com/p/gwt-crypto
8 stars 1 forks source link

Implementation of Triple DES outputs different results in both CryptoJS and gwt-crypto #31

Open vibradeporte opened 4 years ago

vibradeporte commented 4 years ago

Hi,

I am using CryptoJS library for an Angular 2+ project, and I am using gwt-crypto library for a Java project. According to this article, it is possible to make an easy implementation of Triple DES, so I did it like this:

import com.googlecode.gwt.crypto.bouncycastle.DataLengthException;
import com.googlecode.gwt.crypto.bouncycastle.InvalidCipherTextException;
import com.googlecode.gwt.crypto.client.TripleDesCipher;
public class CryptoKit {
    private static final String LOCAL_KEY = "exampleofsecretkey4myapp";
    public static String encryptMessage(String textToEncrypt) {
        byte[] key = LOCAL_KEY.getBytes();
        TripleDesCipher cipher = new TripleDesCipher();
        cipher.setKey(key);
        String textEncrypted = "";
        try {
            textEncrypted = cipher.encrypt(String.valueOf(textToEncrypt));
        } catch (DataLengthException | IllegalStateException | InvalidCipherTextException e) {
            e.printStackTrace();
        }
        return textEncrypted;
    }
    public static String decryptMessage(String textToDecrypt) {
        byte[] key = LOCAL_KEY.getBytes();
        TripleDesCipher cipher = new TripleDesCipher();
        cipher.setKey(key);
        String textDecrypted = "";
        try {
            textDecrypted = cipher.decrypt(textToDecrypt);
        } catch (DataLengthException | IllegalStateException | InvalidCipherTextException e) {
            e.printStackTrace();
        }
        return textDecrypted;
    }
}

As I want to communicate with my Angular app, I tried to do the same exercise using CryptoJS, like:

import { Injectable } from '@angular/core';
import * as CryptoJS from 'crypto-js';
@Injectable({
  providedIn: 'root'
})
export class VdCryptoService {
  private static localKey: string = 'exampleofsecretkey4myapp';
  encryptMessage(message: string): string {
    return CryptoJS.TripleDES.encrypt(message, this.localKey).toString();
  }
  decryptMessage(message: string): string {
    return CryptoJS.TripleDES.decrypt(message, this.localKey).toString(CryptoJS.enc.Utf8);
  }
}

But when I test my two apps, they output different encryption results. For example, if I test with "Example message" as input, I get:

CryptoJS: U2FsdGVkX1/l7hDE9US+MQFcmBw3u2HWN45H3c8shsk=

gwt-crypto: ac5f6601d994bb6a9b7ea304a1523c99

As an interest fact, both apps can encrypt and decrypt correctly, but they ouput different encryptions, and I do not know how to solve this, in order to communicate between them. Somebody could help me?, maybe I need procedures that I don't know about, or I'm doing the implementation wrong.

Thank you.