Cross-platform cryptography

General information and overview

This is a series of cryptography articles that will show how cryptography is done between different programming platforms. The sample programs do not attempt to be "secure" but explain how the crypto routines are compatible with other platforms.

I'm trying to serve a broad spectrum of programming languages and actually I have solutions for these frameworks: Java, PHP, C#, Javascript - CryptoJs, NodeJs Crypto, NodeJs node-forge, Python (for selected programs), Golang (for selected programs) and Dart (for selected programs) as long the functionality is available on the platform. You can test all programs with online compilers (except Dart as no online compiler can consume external libraries) to see what the output is and what happens when you change some parameters. For selected assignments there is a WebCrypto solution available, that will run in your (modern) browser. For very rare programs I provide an OpenSSL solution.

Wouldn't it be nice having a library that could be used cross-platform wide to get cryptographic tasks done? Yes and the library is still there, it is the Libsodium project and there are a lot of bindings available for all major frameworks. The algorithms in Libsodium differ from the other ones used in the cross-platform-project, therefore the Libsodium solutions are in an own section.

Sometimes you may have read about a "JWT" or "JWT token", that is short form of a JSON web token or JWT. That are standardized signature and encryption schemes for a defined data exchange between different systems and frameworks. I provide some JWT web token solutions.

A special chapter deals with the future of cryptography; and that is the Post Quantum Cryptography. Although the quantum computers are not on the market at the moment they will be there and cause catastrophic impacts on cryptography as they may been able to solve the RSA- and EC-curve problems and subsequently all RSA- and EC-curve encryption and signature become worthless. Visit the chapter PQC Post Quantum Cryptography at the end.

I'm providing a special article series about the migration of the (former) standard encryption module MCRYPT in PHP, that was available until version 5.6.x of PHP - newer versions of PHP come with OpenSSL as standard cryptography module. I'm giving examples how to migrate the algorithms Rijndael-128 (better known as "AES"), Rijndael-256, DES and Triple DES using the modes ECB, CBC and CTR and the zero padding or PKCS#7 padding. Visit the chapter MCRYPT migration at the end.

A lot of solutions run with the built-in cryptographic modules but especially the Python and Javascript-ones may need external libraries (pure Javascript will need e.g. "CryptoJs", NodeJs has a built-in "Crypto" library or you can use "node-forge").

General routines

Solution Jav PHP C# CJs Ndc Nfo Web Pyt Go Dar
generate a 32 byte long AES key
generate a 16 byte long initvector
generate a 12 byte long nonce
Base64 encoding & decoding
binary data to a hex string & back
SHA256 hashing
MD-5 hashing
HMAC 256 calculation
PBKDF2 key derivation
Generate RSA keys see explanation
Generate Curve 25519 keys
Generate Elliptic keys see explanation

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

AES string encryption

Solution Jav PHP C# CJs Ndc Nfo Web Pyt Go Dar
CBC-mode String Encryption
CBC-mode String Decryption only
CBC-mode PBKDF2 String Encryption
CBC-mode PBKDF2 String Decryption only
CBC-mode PBKDF2 HMAC String Encryption
CBC-mode passphrase String Encryption
GCM-mode String Encryption
GCM-mode PBKDF2 String Encryption
GCM-mode PBKDF2 AAD String Encryption 🔜 🔜 🔜 🔜 🔜 🔜 🔜 🔜 🔜

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

AES CBC special: tampering

Solution Java Explanation
AES CBC-mode tampering written in Java as it is for demonstration only

ChaCha20-Poly1305 encryption

Solution Jav PHP C# CJs Ndc Nfo Web Pyt Go Dar
ChaCha20-Poly1305 String encryption with random key

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

DES and TripleDES encryption

Solution Jav PHP C# CJs Ndc Nfo Web Pyt Go Dar
DES CBC mode String encryption 🔜
DES ECB mode String encryption 🔜
TripleDES CBC mode String encryption 🔜
TripleDES ECB mode String encryption 🔜

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

RSA encryption

Solution Jav PHP C# CJs Ndc Nfo Web Pyt Go Dar
RSA PKCS 1.5 padding String
RSA OEAP padding SHA-1 String
RSA OEAP padding SHA-1 String decryption only
RSA OEAP padding SHA-256 String

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

RSA signature with different padding modes

Solution Jav PHP C# CJs Ndc Nfo Web OSL Pyt Go Dar
RSA String Signature PKCS#1.5 (full)
RSA String Signature PKCS#1.5 Verification only
RSA String Signature PSS (full)
RSA encrypted private key example using signature with PKCS#1.5 padding

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, OSL=OpenSSL, Go=Golang, Dar=Dart

RSA keys

Working with RSA key pairs can get tricky sometimes so I'm providing some more information about RSA keys:

page information
How to generate RSA key pairs creation of RSA key pairs with OpenSSL
How to convert RSA keys convert keys between XML- and PEM-format
RSA sample keys for my encryption and signature examples I'm using these keys
RSA key formats explanation of the most used key formats
RSA encrypted private key as you should protect your keys I recommend to work with encrypted private keys
RSA encrypted private key example using signature with PKCS#1.5 padding example for the practical usage of encrypted keys
RSA encrypted private key - decrypt manually how to parse an encrypted private key

RSA AES hybrid encryption

Solution Jav PHP C# CJs Ndc Nfo Web Pyt Go Dar
RSA AES CBC 256 hybrid encryption
RSA AES GCM 256 hybrid encryption

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

Curve 25519 key exchange

Solution Jav PHP C# CJS Ndc Nfo Pyt Go
Curve 25519 key exchange & AES CBC mode 256 string encryption

Note: you need external libraries for this feature!

Elliptic curve keys

page information
EC key generation creation of EC key pairs with OpenSSL
EC sample keys sample key pair for curve SECP256R1 = P-256 = PRIME256V1

Elliptic curve signature

Solution Jav PHP C# CJs NdC Nfo Web OSL Pyt Go Dar
ECDSA String Signature IEEE-P1363 encoding
ECDSA String Signature DER encoding
ECDSA signature converter DER ← → IEEE P1363 encoding

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, OSL=OpenSSL, Go=Golang, Dar=Dart

Libsodium solutions

Solution Description Jav PHP C# Ndc Web Pyt Go Dar
Libsodium overview general information about Libsodium ("NaCl", "Sodium") cryptography
generate a random key see the examples in authenticated string encryption with a random key
generate a curve 25519 key pair generate a curve X25519 private key and public key pair in Base64 encoding
derive the curve X25519 public key from a secret key generate the X25519 public key from a secret key in Base64 encoding 🔜 🔜 🔜 🔜
generate a curve ED25519 key pair generate a curve ED25519 private key and public key pair in Base64 encoding
generate a curve ED25519 public key from a private key generate an ED25519 public key from a private key in Base64 encoding
Argon 2 password derivation function derives a password from a passphrase using the "modern" Argon2id algorithm
Argon 2 parameter describes the parameters used in Argon 2 algorithm
ChaCha20-Poly1305 String encryption with random key encrypts a string on basis of a randomly generated encryption key, using the ChaCha20 cipher for encryption and the Poly1305 MAC for authentication
Libsodium authenticated string encryption with a random key (secret box) encrypts a string on basis of a randomly generated encryption key, using the XSalsa20 stream cipher for encryption and the Poly1305 MAC for authentication
Libsodium crypto box authenticated hybrid string encryption encrypts a string on basis of a X25519 curve key exchange, using the XSalsa20 stream cipher for encryption and the Poly1305 MAC for authentication
Libsodium sealed box authenticated hybrid string encryption encrypts a string on basis of a X25519 curve key exchange, using the XSalsa20 stream cipher for encryption and the Poly1305 MAC for authentication but without key exchange between the two partners
Libsodium string signature (detached) with a private-public key uses an ED25519 curve signature

Jav=Java, PHP=PHP, C#=Csharp, CJs=Javascript with Crypto-JS, Ndc=NodeJs with Crypto, Nfo=NodeJs with forge, Web=WebCrypto, Pyt=Python, Go=Golang, Dar=Dart

OpenSSL solutions

Solution Description
OpenSSL overview general information about OpenSSL cryptography
Generate a RSA key pair generate a RSA private key and public key in PEM encoding
Generate an EC key pair generate an Elliptic curve (EC) private key and public key in PEM encoding
RSA file signature with PKCS#1.5 padding the signature and verification is compatible with solutions in RSA string signature with PKCS#1.5 padding
RSA file signature with PSS padding the signature and verification is compatible with solutions in RSA string signature with PSS padding
Elliptic curve file signature DER encoding the signature and verification is compatible with solutions in ECDSA String Signature DER encoding

JWT JSON web token solutions

Solution Description Java PHP C# NodeJS Browser Python Go
JSON web token (JWT) overview general information about JSON web token
structure of a JSON web token (JWT) explains the general structure of a JWT
JSON web token JWA algorithms standardized algorithms for JWT
JSON Web JWK keys standardized key format for JWT
JSON web signature (JWS) using RS256 algorithm sign a JWT with a RSA key, PKCS1.5 padding and SHA-256 hashing
JSON web signature (JWS) using PS256 algorithm sign a JWT with a RSA private key, RSASSA-PSS + MGF1 with SHA-256 and SHA-256 hashing
JSON web signature (JWS) using RSxxx & PSxxx algorithms (verify only) verify a JWT with a RSA public key, PKCS1.5 & SSA-PSS padding and SHA-256/384/512 hashing 🔜
JSON web encryption (JWE) using RSA-OAEP-256 with A256GCM algorithm encrypt a JWT with a RSA key RSA-OAEP-256 and AES-256-GCM algorithm

PQC Post Quantum Cryptography

Purpose algorithm Language
PQC overview several
algorithms facts overview several
PQC with Liboqs library overview general overview about the OpenQuantumSafe library
liboqs algorithms facts overview information about key, ciphertext & signature sizes Java binding
Public key encryption McEliece Java
Public key encryption NTRU Java
key exchange (KEM) Chrystals-Kyber NodeJs
key exchange (KEM) Sike Java
key exchange (KEM) Sike with Liboqs Java
signature FALCON Python
signature FALCON with Liboqs Java binding
signature Rainbow Java
signature Sphincs Java

MCRYPT migration

Solution mode PHP OpenSSL PHP phpseclib 3
MCRYPT migration overview
MCRYPT Rijndael-128 ECB ECB mode
MCRYPT Rijndael-128 CBC CBC mode
MCRYPT Rijndael-128 CTR CTR mode
MCRYPT Rijndael-256 ECB ECB mode
MCRYPT Rijndael-256 CBC CBC mode
MCRYPT Rijndael-256 CTR CTR mode
MCRYPT DES ECB ECB mode
MCRYPT DES CBC CBC mode
MCRYPT DES CTR CTR mode
MCRYPT TDES ECB ECB mode
MCRYPT TDES CBC CBC mode
MCRYPT TDES CTR CTR mode

Elliptic curve encryption

Solution Java PHP C# CryptoJS NodeJS

not planned in future

Which platforms are supported at the moment?

I'm using an IDE for Java and PHP but for the other ones I'm using online compiler like https://replit.com/. Please don't ask if the programs will run on your system with a lower or higher version - just try it on your own.

A lot of solutions run with the built-in cryptographic modules but especially the Javascript-ones may need external libraries (pure Javascript will need e.g. "CryptoJs", NodeJs has a built-in "Crypto" or you can use "node-forge").

The few Javascript Webcrypto examples will run directly in your browser so you will need an actual one and Javascript is enabled.

Language Online-compiler framework version
C# replit.com Mono C# compiler version 6.8.0.123
C# dotnetfiddle.net .NET 4.7.2
C# dotnetfiddle.net .NET 5
C# Bouncy Castle version 1.89
Java replit.com OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
Javascript CryptoJs replit.com node v12.16.1, crypto-js version 4.0.0
NodeJS Crypto replit.com node v12.16.1, openssl 1.1.1g
NodeJS node-forge replit.com node v12.16.1, node-forge version 0.10.0
OpenSSL version 1.1.1g Windows x64
PHP replit.com PHP CLI 7.2.17-0ubuntu0.18.04.1 (cli) (built: Apr 18 2019 14:12:38)
PHP phpseclib replit.com version 3
Python replit.com version 3.8.2 (default, Feb 26 2020, 02:56:10)
Webcrypto modern browser tested in Firefox 91 (x64), Google Chrome 84.0.4147.135 (x86), Opera 70.0.3728.119
Go replit.com version go 1.14 linux/amd64
Go goplay.space version go 1.16.2
Dart no online compiler available tested on 2.13.1

Last update: Oct. 20th 2021

The website is published under: https://java-crypto.github.io/cross_platform_crypto//

If you are interested in much more information on cryptography in Java (and in German language) then visit my webpages http://javacrypto.bplaced.net and http://java-crypto.bplaced.net.