Archive for category Java

How to encrypt/decrypt files in Java with AES in CBC mode using Bouncy Castle API and NetBeans or Eclipse

The Bouncy Castle Crypto API for Java provides a lightweight cryptography API that is an alternative to the standard Sun Java Cryptographic Architecture (JCA) and Java Cryptographic Extension (JCE) bundled in the JDK. The API can be used in J2ME MIDlet applications or in other Java applications up to the 1.7 platform.

The Bouncy Castle lightweight cryptographic API can be used as a:

  • Cryptographic Service Provider (CSP) for the JCA;
  • external library.

In this post we will see how to use the the Bouncy Castle lightweight cryptographic API in both situations, as the syntax differs from one approach to the other. To highlight the differences, the advantages and the disadvantages of the two solutions, the Bouncy Castle API is used in a console Java application to encrypt/decrypt files with the AES (Advanced Encryption Standard) or Rijndael algorithm in Cipher Block Chaining (CBC) mode.

Read the rest of this entry »

, , , , ,

3 Comments

How to convert a byte array to a Hex String in Java

When processing binary values it is very difficult to read or to display them because any printing function generates a String value.  The problem with this approach is that not all byte values can be interpreted as a printable char (i.e. the 0 binary value represents the NUL symbol; for more details check ASCII Codes + HTML Codes and Special Characters) and the resulting String will not contain all the byte values or it will not be accurate. Moreover in Java a char is stored on 2 bytes.

In this post we will see how to convert a byte array to a Hex String in a Java application. The solution is useful because:

  • printing binary values in base 2 or base 10 format can become difficult to read as the value can have multiple digits;
  • is easier to read values in hexadecimal base;
  • it is easier to check or compare values in hexadecimal base.

Read the rest of this entry »

,

1 Comment

How to use Bouncy Castle Cryptographic API in NetBeans or Eclipse for Java JSE projects

The Bouncy Castle Crypto API for Java provides a lightweight cryptography API that works with everything from the J2ME to the JDK 1.6 platform and also a provider for the Java Cryptography Extension JCE (provides an implementation for JCE 1.2.1) and the Java Cryptography Architecture, JCA.

The API provides cryptographic functions for Java JDK 1.1 to 1.6 applications and for J2ME (mobile applications) MIDlets. The API can be downloaded from the Bouncy Castle latest releases page.

In this post we will see how to use Bouncy Castle Cryptographic API either as a JCA provider or as a lightweight API to develop Java J2SE projects in NetBeans 7 (works also on older versions) or Eclipse IDE.

If you want to develop Java applications based on the JSE framework that provide cryptographic services as:

  • generating hash values to check the integrity of the message or file;
  • encryption/decryption using symmetric key algorithms;
  • encryption/decryption using public certificates in a public key infrastructure;
  • generating message authentication codes for messages;

you must use a cryptographic API which provides the necessary classes and methods. Read the rest of this entry »

, , ,

2 Comments

Tutorial Java SCJP – #19 Enumerated lists or Enums

There are logically situations in which a variable must have values restricted to a specific range or set that is defined in the solution specifications. Let’s imagine that you must develop a Java applications that manages Vehicles and the engine type must take one value from the {GASOLINE, DIESEL, HYBRID, ELECTRICAL} set. You can define the engine type as a String or as an int and you can validate every time the input value. For strings you can compare the input value with “gasoline”, “diesel” and so on. For integer you can assume that GASOLINE is 1, DIESEL is 2, … and check the values based on this logic. This is a possible approach but it’s not ok because you can make easily mistakes and because you complicate a really simple procedure.

Read the rest of this entry »

1 Comment

Tutorial Java SCJP – #18 Initialization blocks

In order to process something you need values. And values are usually stored in static variables, local variables (defined in methods) or instance variables (nonstatic variables defined in classes). In order to initialize a variable you can do it at definition or later in a method (constructor or not). Despite these two common solutions, there is another way using initialization blocks.

Initialization blocks are blocks of codes defined between { and }. At this point they are like methods blocks, but the main difference is that initialization blocks don’t have  a name. They are like methods but without the method header (return type, name, parameter list).

Read the rest of this entry »

1 Comment

Tutorial Java SCJP – #17 Access modifiers for methods and attributes

Access modifiers represents ways to give or to restrict access to class variables and methods. One reason is the encapsulation concept that states that instance variables are not accessed directly, but only through access methods (prefixed with get or set). Another reason is to control how the class is used and how and what values are getting into instance variables (ex. if you implement the Person class that has a age instance variable you don’t want to allow other programmers to initialize it directly with any value because they can put a negative one)

Read the rest of this entry »

2 Comments

How to parse XML, RSS feed with kXML in a J2ME MIDlet

More an more mobile devices are used to access Internet and connected services. One efficient way to transfer data between different platforms and technologies is to use XML files, which are simple text files formatted accordingly to the XML rules.

IN order to process an XML file and to extract needed data you need a XML parser which is a text process engine.

Read the rest of this entry »

, ,

7 Comments

Tutorial Java SCJP – #16 Constructors

In a class you can define methods, but there is a special type of methods which are used to solve a particular problem, to construct objects. Constructor methods are special because of their role and because they have a lot rules regarding declaration and usage.

Read the rest of this entry »

6 Comments

Tutorial Java SCJP – #15 Wrapper classes for primitive types

In Java there are 2 important categories of data types: references and primitives. Most of the time, numeric values, chars and boolean values are used as primitives because it is more efficient as processing speed and memory requirements. Despite that, there are scenarios (like using Collections) when it is needed to store primitive values inside objects. For that, Java provides a set of classes used to wrap primitive values in an object.

Read the rest of this entry »

2 Comments

Tutorial Java SCJP – #14.2 How to define methods with variable argument lists – var-args

Starting with Java 5.0 you can define methods with variable argument lists (var-args).  These methods can be used in situations when you don’t know the exact number of arguments. These is also available in other programing languages and even I is not intensively used, it is a topic for the SCJP exam. Read the rest of this entry »

1 Comment