GithubHelp home page GithubHelp logo

00mjk / crypto-utils Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cadamsdev/crypto-utils

0.0 0.0 0.0 16 KB

AES and RSA encryption for Java made simple

License: GNU General Public License v3.0

Java 100.00%

crypto-utils's Introduction

Crypto Utils

A simple library for easily encrypting and decrypting data using either AES, RSA or combined algorithms.

AESUtils

A simple library to easily encrypt and decrypt data using the AES algorithm.

Features

  • Use a random key
  • Create a key from a password
  • Can specify a salt with a password for better encryption
  • Can decrypt data with a password
  • Write key to file
  • Retrieve a key from a file
  • Super easy to use

Examples

Using a random key

		// the bytes you want to encrypt
		byte[] message = "Hello world!".getBytes();

		// create a random key
		SecretKey secretKey = AESUtils.generateKey();
	
		// encrypt the message using the key that was generated
		byte[] encrypted = AESUtils.encrypt(secretKey, message);

		// decrypt the message by using the key again
		byte[] decrypted = AESUtils.decrypt(secretKey, encrypted);

		// results
		System.out.println("original: " + new String(message));
		System.out.println("encrypted: " + new String(encrypted));
		System.out.println("decrypted: " + new String(decrypted));

Result

original: Hello world!
encrypted: ù2è–®ÃѦDçÆÔ/šÙÆ
decrypted: Hello world!

Using your own password as the key

		// the bytes you want to encrypt
		byte[] message = "Hello world!".getBytes();

		// create a key by using your own password
		SecretKey secretKey = AESUtils.createKey("password");
	
		// encrypt the message using the key that was generated
		byte[] encrypted = AESUtils.encrypt(secretKey, message);

		// decrypt the message by entering a password
		byte[] decrypted = AESUtils.decrypt("password", encrypted);

		// results
		System.out.println("original: " + new String(message));
		System.out.println("encrypted: " + new String(encrypted));
		System.out.println("decrypted: " + new String(decrypted));

Result

original: Hello world!
encrypted: �O¥e†Å8��Qúµd�³Ç
decrypted: Hello world!

RSAUtils

A simple library to easily encrypt and decrypt data using the RSA algorithm.

Features

  • Generates both random public and private keys
  • 2048 size
  • Get public key object from file
  • Get private key object from file
  • Super easy to use

Example

	public static void main(String[] args) throws Exception {
	
		byte[] data = "hello cryptography".getBytes();
		
		RSAUtils.generateKey("./public.key", "./private.key");		

		PublicKey publicKey = RSAUtils.getPublicKey("./public.key");

		byte[] encrypted = RSAUtils.encrypt(publicKey, data);		

		PrivateKey privateKey = RSAUtils.getPrivateKey("./private.key");

		byte[] decrypted = RSAUtils.decrypt(privateKey, encrypted);		

		System.out.println("original: " + new String(data));
		System.out.println("encrypted: " + new String(encrypted));
		System.out.println("decrypted: " + new String(decrypted));

	}

crypto-utils's People

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.