Giter Club home page Giter Club logo

cryptojs.swift's Introduction

CryptoJS.swift

Cross-platform cryptographic functions in swift using the Crypto JS library. Allows you to share the same crypto between a native iOS/OSX application and a web application.

Compatible with https://github.com/brix/crypto-js.

⚠️ New project in development

The javascript library used to implement the cryptography in this project is no longer maintained and suffers severe performance limitations over the new WebCrypto API. A new modern library called WebCrypto.swift is being developed. WebCrypto.swift leverages the power of the WebCrypto API while keeping backwards compatibility with this project.

No new development will happen in this repository. Do not submit new feature requests here. If you want something to be implemented, please submit an issue in the new repository:

https://github.com/etienne-martin/WebCrypto.swift

Platforms Supported

iOS
macOS
Web browsers
openSSL

Installation

Drag and drop CryptoJS.swift and the javascript files in your Xcode project.

AES

Supported modes: CBC (the default), CFB, CTR, OFB, ECB
Supported padding schemes: Pkcs7 (the default), Iso97971, AnsiX923, Iso10126, ZeroPadding, NoPadding

// Load the AES module
let AES = CryptoJS.AES()

// Basic AES encryption
let encrypted = AES.encrypt("Secret message", password: "password123")
let decrypted = AES.decrypt(encrypted, password: "password123")

// AES encryption with custom mode and padding
CryptoJS.mode.ECB() // Load custom mode
CryptoJS.pad.Iso97971() // Load custom padding scheme
let encrypted = AES.encrypt("Secret message", password: "password123", options:[ "mode": CryptoJS.mode().ECB, "padding": CryptoJS.pad().Iso97971 ])
let decrypted = AES.decrypt(encrypted, password: "password123", options:[ "mode": CryptoJS.mode().ECB, "padding": CryptoJS.pad().Iso97971 ])
Compatible with OpenSSL
# Basic openSSL AES encryption
openssl enc -aes-256-cbc -e -in /foo/thePlainTextFile.txt -out /bar/theEncryptedFile.txt -pass pass:"password123" -base64

# Basic openSSL AES decryption
openssl enc -aes-256-cbc -d -in /foo/theEncryptedFile.txt -out /bar/theDecryptedFile.txt -pass pass:"password123" -base64

TripleDES

// Load the TripleDES module
let TripleDES = CryptoJS.TripleDES()

// Basic TripleDES encryption
let encrypted = TripleDES.encrypt("secretMessage", password: "password123")
let decrypted = TripleDES.decrypt(encrypted, password: "password123")

DES

// Load the DES module
let DES = CryptoJS.DES()

// Basic DES encryption
let encrypted = DES.encrypt("secretMessage", password: "password123")
let decrypted = DES.decrypt(encrypted, password: "password123")

Hashers

let MD5 = CryptoJS.MD5()
let SHA1 = CryptoJS.SHA1()
let SHA224 = CryptoJS.SHA224()
let SHA256 = CryptoJS.SHA256()
let SHA384 = CryptoJS.SHA384()
let SHA512 = CryptoJS.SHA512()
let SHA3 = CryptoJS.SHA3()
let RIPEMD160 = CryptoJS.RIPEMD160()

var hash = MD5.hash("mystring")
var hash = SHA1.hash("mystring")
var hash = SHA224.hash("mystring")
var hash = SHA256.hash("mystring")
var hash = SHA384.hash("mystring")
var hash = SHA3.hash("mystring")
var hash = SHA512.hash("mystring")
var hash = SHA3.hash("mystring",outputLength: 256)
var hash = RIPEMD160.hash("mystring")

Contributing

When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

Update the README.md with details of changes to the plugin.

Update the examples by demonstrating the changes to the plugin.

Build the project & test all the features before submitting your pull request.

Authors

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

cryptojs.swift's People

Contributors

canopener avatar etienne-martin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar

cryptojs.swift's Issues

CBC mode

hello, isn't there a CBC mode ? and a PKCS7 padding ? thanks

Can't decrypt data with aes decrypt function in native

I have function used in cross platform application and i want to convert it into native please help me?

function fn_decryptData(var_data,var_key){
	var encrypted = CryptoJS.enc.Base64.parse(var_data);
	var encodedString = btoa(var_key);
	var key = CryptoJS.enc.Base64.parse(encodedString);
	var iv = `CryptoJS.enc.Base64.parse('somestring');`
	return (CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt({ ciphertext: encrypted },key,{ mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: iv,  })));
}

How to use this in native to decrypt data

Encryption options in Swift 3

I'm trying to set the encryption options in swift 3 and I'm able to get the code to compile with the code below

AES.encrypt(secretMessage: pw, secretKey: key, options: ["mode": CryptoJS.mode.ECB()] as? AnyObject)!
However this causes the app to crash when the code is run. Without the as? AnyObject the code won't compile and I get the error Contextual type 'AnyObject' cannot be used with dictionary literal.

Any suggestions?

NSdata decryption

Hi,
How can I use this library for decrypt nsdata file ?

Thanks,
vafa

Convert crypto.js algorithm in swift

Hi, I would like to know how to use the below js method to work in Swift:

let key = "Q8pTgdOC7h25qKVncJlzDA=="
let message = "TestAnitaa009"
let base64Key = CryptoJS.enc.Base64.parse(key);

var iv = base64Key

let encryptedValue = CryptoJS.AES.encrypt(message, base64Key, {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: base64Key
});

//The above code outputs = Oz9b31UvNTW8rFfGUOfB5A==

I tried using this library to help to do the same in Swift. But I am not able to. Any help is appreciated. Thanks a lot

Not so much an issue as I need help

Hi first of all I want to thank you for your library. I'm complete noob to data encryption and just need help with it.

Back-end API uses CryptoJS for encrypting their responses. I've tried your library and looks like it works perfect locally. It was easy. But when I try to decrypt server response it returns result "undefined"

Here is the sample of response:
"{\"ct\":\"clK4DaN8jn6uep8Uj42usi/ZfoyWBVjIK8Kx8kds5uf9cwp7l0dUheIrdcJwtgUSw70TKD/zrTYAoiRQQ6f1VxoYFroLzeKn+VWlkmqam69tHA66BOc6POnLChBxa9dOhm3Vju+y5IPTPwcclJvgFHKBsnUCdka0yfg6QvELAHef6J6hIYXnithqcSXgJZYMqlSPI0Y5qhysSj8CT9/EINC/LsHCaVEEi1q7x0BoYZ2VaZHXDKmI7Rpdksca5/BB6VoRP3Bno9r095ymoeljGBgpaY61cJLDCbTC2H500ChuwXz3HtacdtFcz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h5yc9HxLtfHZ2OwkMqmOf9lOY6uxFA1Jbt/wt/II4I69inPUIwYGPP9EhM/Wzc9oeyuJb2epvriTSxyJXb+hM351kBJ9qUrI3+A6dr0gDKLRnFNlMy0RbeOal5+a7C4YG5fOA8Huq8fmJDqhDHsTuhZcQC3IqL+PTMGEv08Nxz3y80kAEuPvBpCHr47O8FfwgXU2KrMLfxXP0W/2mYP3O2neS8fL7eiMx4Svzh1fMuha2i0QzntD5mJOyWzBUZnK5x5XOGo8dd8H9/lhY+EEbrHHgTpXTJWdhv5cD7kOPdqthjcZsj/Rh4JSBrV9Yk3nghO9YQD46mif4B2PTPbC00SQ8hpFHt2EZWDJ/WR3cylKKTyzy293R0MHlpKSdKXu1sB/ao6QRTyIn8enxtv1VD1M52K2PgGaUJgm3O9vuKNhTv/bTcMux0ZtrHm828nL2YkOTW7gJtQQh0OESVRgD6thsYvgja7DZC97nMl9fDeaUaOiU5KlM4FTYF09ASSzG1OvheJ+tYDHfQXg\",\"iv\":\"1312f7120c39478c3c1a8695835608aa\",\"s\":\"f63c9a8b42209432\"}"

and a key that suppose to help:

8aa1ec1e6948b481d1ee450c94ffb2edc774877325df4d05aca2e5827497ed33

Swift 3 compiler warnings

Using xcode 8.2 I get compiler warnings from the evaluateScript calls, e.g.

cryptoJScontext?evaluateScript(cryptoJS)
the warning is: Expression of type JSValue? is unused

the fix seems to be to change the lines to

let _ = cryptoJScontext?evaluateScript(cryptoJS)

Any comments?

Thanks

DES Mode

Hi,
Do this library decrypt DES files?
Thanks,
Rafa

DES encryption does not work properly

If you comment out CryptoJS.AES then the DES method will not work. Example: Comment out the ViewController class 16-38 in the project, then CryptoJS.TripleDES() will not work.

Does it work with Swift 3?

Hi. I've converted CryptoJS.swift to Swift 3 using Xcode 8 converter and the converted file seems to be ok but I can't make it work.

When I try

    // Load the TripleDES module
     let TripleDES = CryptoJS.TripleDES()

    // Basic TripleDES encryption
    let encrypted = TripleDES.encrypt("secretMessage", secretKey: "password123")
    print("SECRETMESSAGE: " + encrypted)

    // Basic TripleDES decryption
    let decrypted = TripleDES.decrypt(encrypted, secretKey: "password123")
    print("RESULT: " + decrypted)

I get:

Loaded tripledes.js
SECRETMESSAGE: Optional(undefined)
RESULT: Optional(undefined)

What am I missing?

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.