Developer(s) | Legion of the Bouncy Castle Inc. | ||||||||
---|---|---|---|---|---|---|---|---|---|
Stable release(s)[±] | |||||||||
| |||||||||
Repository | github.com/bcgit/ | ||||||||
Written in | C# and Java | ||||||||
Platform | .NET Framework and Java SE | ||||||||
Type | |||||||||
License | MIT License[5] | ||||||||
Website | bouncycastle.org |
May 20, 2016 Here we demonstrate simple Java code for ECDH key agreement on the command line. We only demonstrate. Bouncy Castle). The only trickiness is to ensure that we feed the public keys into the hash in the same order on both sides of the agreement protocol. And then using the derived secret as a 256-bit key for AES. Sep 17, 2018 You are new to encryption or not sure how to use the JCA/Bouncy Castle to do encryption in Java. You face some issues with key length using the JCA. You are not sure which types of encoding to use for encryption. You use the JCE library with Bouncy Castle as the security provider to encrypt a plaintext into a byte array.
Bouncy Castle is a collection of APIs used in cryptography. It includes APIs for both the Java and the C# programming languages. The APIs are supported by a registered Australiancharitable organization: Legion of the Bouncy Castle Inc.
Bouncy Castle is Australian in origin and therefore American restrictions on the export of cryptography from the United States do not apply to it.
Bouncy Castle started when two colleagues were tired of having to re-invent a set of cryptography libraries each time they changed jobs working in server-side Java SE. One of the developers was active in Java ME (J2ME at that time) development as a hobby and a design consideration was to include the greatest range of Java VMs for the library, including those on J2ME. This design consideration led to the architecture that exists in Bouncy Castle.[6]
The project, founded in May of 2000, was originally written in Java only, but later added a C# API in 2004. The original Java API consisted of approximately 27,000 lines of code, including test code and provided support for J2ME, a JCE/JCA provider, and basic X.509 certificate generation. In comparison, the 1.53 release consists of 390,640 lines of code, including test code. It supports the same functionality as the original release with a larger number of algorithms, plus PKCS#10, PKCS#12, CMS, S/MIME, OpenPGP, DTLS, TLS, OCSP, TSP, CMP, CRMF, DVCS, DANE, EST and Attribute Certificates. The C# API is around 145,000 lines of code and supports most of what the Java API does.
Some key properties of the project are:
On 18 October 2013, a not-for-profit association, the Legion of the Bouncy Castle Inc. was established in the state of Victoria, Australia, by the core developers and others to take ownership of the project and support the ongoing development of the APIs. The association was recognised as an Australian charity with a purpose of advancement in education and a purpose that is beneficial to the community by the Australian Charities and Not-For-Profits Commission on 7 November 2013.[7] The association was authorised to fundraise to support its purposes on 29 November 2013 by Consumer Affairs Victoria.
The Bouncy Castle architecture consists of two main components that support the base cryptographic capabilities. These are known as the 'light-weight' API, and the Java Cryptography Extension (JCE) provider. Further components built upon the JCE provider support additional functionality, such as PGP support, S/MIME, etc.
The low-level, or 'light-weight', API is a set of APIs that implement all the underlying cryptographic algorithms. The APIs were designed to be simple enough to use if needed, but provided the basic building blocks for the JCE provider. The intent is to use the low-level API in memory constrained devices (JavaME) or when easy access to the JCE libraries is not possible (such as distribution in an applet). As the light-weight API is just Java code, the Java virtual machine (JVM) does not impose any restrictions on the operation of the code, and at early times of the Bouncy Castle history it was the only way to develop strong cryptography that was not crippled by the Jurisdiction Policy files that prevented JCE providers from performing 'strong' encryption.
The JCE-compatible provider is built upon the low-level APIs. As such, the source code for the JCE provider is an example of how to implement many of the 'common' crypto problems using the low-level API. Many projects have been built using the JCE provider, including an Open Source Certificate Authority EJBCA.
The C# and Java releases now[when?] have FIPS 140-2 Level 1 certified streams as well. These differ from the regular releases in that, while the modules are designed in a similar fashion to the regular releases, the low-level APIs are quite different – largely to support the enforcement of controls that FIPS requires when an algorithm is used. In the case of the JCE level of the Java API, the provider is still largely a drop-in replacement for the regular release. The first FIPS-certified releases were made available in November 2016, with the Java version being assigned certification number 2768 and the C# version being assigned certification number 2792.
The Android operating system, as of early 2014, includes a customized version of Bouncy Castle.[8] Due to class name conflicts, this prevents Android applications from including and using the official release of Bouncy Castle as-is. A third-party project called Spongy Castle distributes a renamed version of the library to work around this issue.[9].
Originally, it was assumed a FIPS 140-2 version of Spongy Castle could also be done. It turned out due to Android's DEX file processing that for FIPS purposes the provider needs to be installed on the device separate from the application. The FIPS 140-2 release for Android is now called Stripy Castle and is packaged under org.stripycastle. This was needed in order to avoid clashes with Android's version of Bouncy Castle as well as clashes for applications that might be using Spongy Castle and not requiring FIPS 140-2 certified services.
FIPS (Federal Information Processing Standards) are a set of standardsfor describing document processing and encryption algorithms. Any applicationinvolved in transmission of sensistive data in US government departmentsand agencies must adhere to FIPS 140-2 standards.
There are couple of different ways to configure Bouncy Castle FIPS Java provider:
Place the bc-fips-1.0.0.jar
in the jre/lib/ext
folder.
Make the following changes to jre/lib/security/java.security
file:
security.provider.4=com.sun.net.ssl.internal.ssl.Provider
to
security.provider.4=com.sun.net.ssl.internal.ssl.Provider BCFIPS
security.provider.11=org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider
11
is the priority number for the Bouncy Castle FIPS Java provider.
Please make sure to you use right numbering as they should be consecutive. Hereis an example of list of providers in jre/lib/security/java.security
fileafter the changes:
Once the provider is added, it can be referenced in your codeusing the provider name as BCFIPS
.
By adding the provider during the application execution:
In this project, the Bouncy Castle FIPS Java provider is added duringruntime. No changes needed in your JRE.
Examples here relate to Bouncy Castle implementation ofJava FIPS API.
These examples can found in The Bouncy Castle FIPS Java API in 100 Examples.
Secured random is very important in cryptography as random values are used inkey and IV generation.
SecureRandomNumberExample.java
contains examples of generating securerandom numbers. (Example 1, 2, and 3)BC FIPS API offers both approved mode symmetric ciphers, AESand TripleDES, and also a number of other symmetric ciphers such as ARC4,Blowfish, Camellia, CAST5, DES, GOST28147, IDEA, RC2, SEED, Serpent,SHACAL2, and Twofish.
KeyCreationExample.java
contains examples related to creating symmetrickeys. (Example 4 and 5)
ECBModeEncryptionExample.java
contains examples of encrypting andblock cipher modes is unpadded. The input has to be aligned on theblock boundaries of the cipher - in this case 128 bits. (Example 6)
CBCModeEncryptionExample.java
contains examples of encrypting anddecrypting in CBC (Cipher Block Chaining) mode. Padding needs to bespecified as the CBC mode is block aligned. CBC mode has an extraparameter, the initialization vector (IV), which is used with the mode toprevent any similarities in two plain texts from showing up in theencrypted results. Make sure the IV is reliably random or unique. (Example 7)
CFBModeEncryptionExample.java
contains examples of encrypting anddecrypting in CFB (Cipher Feedback) mode. It is similar to CBC whileusing a streaming block mode. However, padding is no longer requiredas the cipher generates a stream of 'noise' which is XOR'd with the datato be encrypted. (Example 8)
CTRModeEncryptionExample.java
contains examples of encrypting anddecrypting in CTR (Counter) mode. It is a block streaming mode with morecontrol than CFB (Cipher Feedback) mode. The IV (initialization vector) isbroken up into two parts: a random nonce, and a counter.It differs from CFB mode in the way cipher stream is gerneratedby encrypting the nonce and counter. The use of the nonce and countermeans that the cipher stream can be generated in a random access fashion.(Example 9)
Openssl generate self signed certificate with existing keyboard. Since a self-signed certificate won’t be used publicly, this information isn’t necessary. Here we’ll use /root/certs: su - rootmkdir /root/certs && cd /root/certs.Create the certificate: openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out MyCertificate.crt -keyout MyKey.keyYou will be prompted to add identifying information about your website or organization to the certificate. That location will vary depending on your needs.
CBCModeWithCTSEncryptionExample.java
contains examples of encrypting anddecrypting in CBC (Cipher Block Chaining) mode with CTS (Ciphertext Stealing).CTS is used in conjunction with CBC mode and can be used where there is atleast 2 blocks of data. It requires no padding, as the “stealing” processallows it to produce a cipher text which is the same length as the plaintext. The most popular one is CS3. (Example 10) Encountered the followingexception while testing: javax.crypto.BadPaddingException: Error closing stream
Unlike basic modes, authenticated modes (GCM, CCM) provides a cryptographicchecksum that can be used to help validate a decryption.
These modes are also known as Authenticated Encryption with AssociatedData (AEAD) modes since they provide ways to add extra clear text or associateddata into the tag used for validation.
GCMAuthModeEncryptionExample.java
contains examples of encrypting anddecrypting in authenticated GCM (Galois/Counter Mode) mode. It is based onCTR (Counter) mode and has its own hashing function. (Example 11)Execute the following command from the parent directory: