Posts Tagged 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 Enumerari de tip enum

Exista situatii in care o variabila trebuie sa aiba valori limitate la o anumita multime, definita in specificatiile solutiei. Sa presupunem ca trebuie dezvoltata o aplicatie Java care gestioneaza Vehicule iar tipul de motor trebuie sa ia o valoare din multimea {BENZINA, DIESEL, HYBRID, ELECTRIC}. Pentru a implementa cerinta se poate defini atributul asociat tipului de motor ca String sau ca int si se valideaza de fiecare data valoarea de intrare. Pentru siruri de caractere se poate compara valoarea de intrare cu "BENZINA", "DIESEL", si asa mai departe. Pentru int se poate face asocierea BENZINA este 1, DIESEL este 2, … si se verifica valorile pe baza acesti abordari. Aceasta este o solutie posibila, dar nu e eficienta pentru ca se pot face cu usurinta greseli si pentru ca se complica o procedura care ar trebui sa fie simpla.

Read the rest of this entry »

1 Comment

Tutorial Java SCJP – # 18 Blocuri de initializare

Pentru a face prelucrari si pentru a obtine rezultate este nevoie de date. Si aceste valori de intrare sunt de obicei stocate in variabile statice, variabilele locale (definite în metode) sau variabile de instanta (variabile nonstatice definite în clase). Pentru a initializa o variabila o poti face la definitia sau mai târziu într-o metoda (constructor sau nu). In ciuda acestor doua solutii comune, exista o alta cale folosind blocuri de initializare.

Blocurile de initializarea sunt blocuri de cod definite intre { si }. Din acest punct de vedere, ele seamana cu blocuri/corpuri de metode, dar principala diferenta este ca blocurile de initializare nu au un nume. Ele sunt ca metodele, insa fara antet/header (tip returnat, nume metoda, lista de parametri).

Read the rest of this entry »

1 Comment

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 – #15 Clase Wrapper pentru tipurile primitive

In Java exista 2 categorii importante de tipuri de date: referinte si primitive. De cele mai multe ori, valorile numerice, caracterele si valorile logice sunt utilizate ca primitive, deoarece sunt mai eficiente, ca viteza de procesare, sintaxa si efecte asupra memoriei. In ciuda acestui fapt, exista scenarii (cand folosesti Collections), când este necesar sa stochezi valori primitive in interiorul unor obiecte. Pentru aceasta nevoie, Java ofera un set de clase folosite la ambalarea (wrap) valorilor primitive intr-un obiect.

Read the rest of this entry »

1 Comment

Tutorial Java SCJP – #16 Constructori

Intr-o clasa se pot defini metode, dar exista un tip special de metode care sunt folosite pentru a rezolva o anumita problema, aceea de a construi obiecte. Constructori sunt metode speciale datorita rolului lor si pentru ca au o multime reguli privind declararea si utilizare.

1 Comment

Tutorial Java SCJP – #17 Modificatori de acces pentru metode si variable, atribute

Modificatorii de acces reprezinta modalitati prin care programatorul poate controla (acorda sau restrictioneaza) accesul la metodele si atributele definite intr-o clasa. Un motiv pentru a utiliza acesti modificatori de access este conceptul POO numit incapsulare, care cere ca atributele sa nu poata si accesate (citire/scriere) direct, ci doar prin metode accesor (cele prefixate cu get si set). Un alt motiv este necesitatea de a controla cum este folosita clasa si cum si ce valori ajung sa fie stocate in variabilele de instanta (atribute). De exemplu, daca se implementeaza clasa Persoana care defineste atributul varsta, nu este de dorit ca un alt programator sa poata initializa direct (cu orice valoare) acest atribut, deoarece poate folosi o valoare negativa.

Read the rest of this entry »

2 Comments