Giter VIP home page Giter VIP logo

sytelus / cryptojs Goto Github PK

View Code? Open in Web Editor NEW
978.0 49.0 567.0 135 KB

This is unmodified copy of Google Code hosted CryptoJS project. CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.

License: Other

JavaScript 99.95% Batchfile 0.05%

cryptojs's Issues

wiredep and the main property of bower.json

As noted here in this issue, cryptojslib defines **/*.js in the main property. When grunt-wiredep comes across this, it parse it into your html like so:

<script src="bower_components/cryptojslib/**/*.js"></script>

Any package that lists this library as a dependency will ultimately have this same issue. If you can't point to a specific file in your main then I suggest note defining the main property at all. That way, grunt-wiredep will throw a warning, stating that it was unable to wire up said dependency and to wire it up manually. It's better to see that warning than to have **/*.js added to the HTML.

Difference in output of java's crypt lib and crypto-js

This is the java code. I am trying to replicate the same functionality in javascript.

public String populateHMAC(String app_id, String mobile, String token,
String deviceId) {

String hmac = null;
try {
    CryptLib cryptLib = new CryptLib();
    String message = app_id + "|" + mobile + "|" + deviceId;
    byte[] tokenBytes = Base64.decode(token, 2);//cryptLib.hexStringToByteArray(token);

    String temp=Base64.encodeToString(cryptLib.SHA256(message),2);

    byte[] tempArr=Base64.decode(temp,2);

    byte[] hmacBytes = cryptLib.encrypt(
            cryptLib.SHA256(message),
            tokenBytes);
    hmac = Base64.encodeToString(hmacBytes, Base64.DEFAULT);
} catch (Exception e) {
    e.printStackTrace();
}
return hmac;

}
These are the functions inside CryptLib

The SHA256 function

 public byte[] SHA256(String paramString) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(paramString.getBytes("UTF-8"));
byte[] digest = md.digest();
return digest;

}
And the encrypt function

public byte[] encrypt(byte[] data, byte[] key) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
byte[] iv = new byte[16];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher acipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] arrayOfByte1;
acipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
arrayOfByte1 = acipher.doFinal(data);
return arrayOfByte1;

}
This is the javascript code for the same functionality. I am using the crypto-js library.

        var crypto = require('crypto-js');

        populateHMAC( app_id,  mobile, token, deviceId){

        var rawStr = token;
        var wordArray = crypto.enc.Utf8.parse(rawStr);
        var base64 = crypto.enc.Base64.stringify(wordArray);

        var enctoken=btoa(token);


        var message= app_id + "|" + mobile + "|" + deviceId;

        var decodedString= atob(enctoken);

        message=encodeURIComponent(message);

        var hash= crypto.SHA256(message);//.toString(crypto.enc.Utf8);

        console.log("params",decodedString,hash.toString(crypto.enc.Hex));


        var iv = crypto.enc.Hex.parse('0000000000000000'); 
        var encryptedString = crypto.AES.encrypt(hash, decodedString, {
                    iv:iv,
                    mode: crypto.mode.CBC,
                    padding: crypto.pad.Pkcs7
                });

        var encodedString= encryptedString.ciphertext.toString(crypto.enc.Base64);


         return encodedString;
        }

The two outputs are different and I am unable to figure out why.

Long ouput of AES-256 cipher

Why AES cipher with 256 bit key produced ciphertext of 48 bytes long?
(it should be 32 bytes long)

let encrypt = function(data, key) {
    let iv = new Uint8Array(16);
    window.crypto.getRandomValues(iv);

    let encKey = convert.ua2words(key, 32);
    let encIv = {
        iv: convert.ua2words(iv, 16)
    };
    let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Hex.parse(data), encKey, encIv);
    return {
        ciphertext: encrypted.ciphertext,
        iv: iv,
        key: key
    };
};

Maximum call stack size exceed in core.js

Line 226 of core.js : crash sometime for obscur reasons
(with chromium 50.0.2661.75 (64-bit))
(not with FireFox 46.0)
solved by changing

219             } else if (thatWords.length > 0xffff) {
220                 // Copy one word at a time
221                 for (var i = 0; i < thatSigBytes; i += 4) {
222                     thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
223                 }
224             } else {
225                 // Copy all words at once
226                 thisWords.push.apply(thisWords, thatWords);
227             }

by

219             } else {
220                 // Copy one word at a time
221                 for (var i = 0; i < thatSigBytes; i += 4) {
222                     thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
223                 }
224             }
Error : Uncaught RangeError: Maximum call stack size exceeded  
  C_lib.WordArray.Base.extend.concat  
  C_lib.BufferedBlockAlgorithm.Base.extend._append  
  C_lib.Hasher.BufferedBlockAlgorithm.extend.finalize  
  (anonymous function)  
  hashChunk  
  (anonymous function)

EDIT : fix can be done by lowering the word limit : passed from Oxffff to 0xfff

219             } else if (thatWords.length > 0xfff) {
220                 // Copy one word at a time
221                 for (var i = 0; i < thatSigBytes; i += 4) {
222                     thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
223                 }
224             } else {
225                 // Copy all words at once
226                 thisWords.push.apply(thisWords, thatWords);
227             }

License

Can you update license please?

Finding an exact decryption mechanism.

def encryption(payload_data, merchant)
key = merchant.api_secret_key
algorithm = 'AES-128-CBC'
begin
cipher = OpenSSL::Cipher.new(algorithm)
cipher.encrypt()
cipher.key = key
crypt = cipher.update(payload_data) + cipher.final()
crypt_string = (Base64.encode64(crypt))
rescue Exception => e
Rails.logger.info "encryption failed #{e.message}"
end
end

I tried the method described in https://code.google.com/archive/p/crypto-js/issues/91. But i couldnt decrypt the message. The above attached is the encryption mechanism I use in Ruby.

Cannot install with npm

We are migrating from bower package manager to npm. And we have faced with issue that we cannot install cryptojslib as npm module.
Our package json file looks like next:
"dependencies": { "backbone": "~1.3.2", "codemirror": "5.29.0", "cryptojslib": "sytelus/CryptoJS.git#v3.1.2", }
As you can see we want to install cryptojslib from github repository with specific tag version.
While npm installing we realized that we cannot install cryptojslib package via npm because cryptojslib repository has no package.json file. Please add this file to github repository.

Cannot read property 'createDecryptor' of undefined

Hi

I'm trying to use the library to encrypt/decrypt aes messages but when I try to decrypt a message I got the message of the title.
This is what I try to do:

var key = CryptoJS.enc.Base64.parse(password);
var decryptedData = CryptoJS.AES.decrypt( response.token, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
} );

This are the loaded libraries:

<script src="../../ext-libs/crypto/core-min.js"></script> <script src="../../ext-libs/crypto/aes.js"></script> <script src="../../ext-libs/crypto/cipher-core-min.js"></script> <script src="../../ext-libs/crypto/enc-base64-min.js"></script> <script src="../../ext-libs/crypto/pbkdf2.js"></script> <script src="../../ext-libs/crypto/enc-utf16-min.js"></script>

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.