JAVA Cryptography

JAVA Cryptography

Hey Guys am new in here so take it easy on me ... i wanna ask you about java

import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec;

/**
* Basic symmetric encryption example
*/
public class SimpleSymmetricExample
{
public static void main(String[] args) throws Exception
{
byte[] input = new byte[] {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
(byte)0x88, (byte)0x99, (byte)0xaa, (byte)0xbb,
(byte)0xcc, (byte)0xdd, (byte)0xee, (byte)0xff };
byte[] keyBytes = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");

System.out.println("input text : " + Utils.toHex(input));

// encryption pass

byte[] cipherText = new byte[input.length];
cipher.init(Cipher.ENCRYPT_MODE, key);
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("cipher text: " + Utils.toHex(cipherText)
+ " bytes: " + ctLength);
//decryption pass
byte[] plainText = new byte[ctLength];
cipher.init(Cipher.DECRYPT_MODE, key);
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println("plain text : " + Utils.toHex(plainText)
+ " bytes:"  + ptLength);
}
}

its about this error after running the code :

Exception in thread "main" java.security.NoSuchProviderException: No such provider: BC
    at javax.crypto.Cipher.getInstance(Cipher.java:576)
    at chapter1.SimpleSymmetricExample.main(SimpleSymmetricExample.java:24)

can anyone explain that to me ,, and how can i fix this ?? thx for you kindness :D

View Answers

March 21, 2012 at 5:18 PM

   import java.security.*;
   import javax.crypto.*;
   import javax.crypto.spec.*;
   import java.io.*;

   public class EncryptAndDecrypt {
     public static String asHex (byte buf[]) {
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
      int i;

      for (i = 0; i < buf.length; i++) {
       if (((int) buf[i] & 0xff) < 0x10)
        strbuf.append("0");

       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
      }
      return strbuf.toString();
     }

     public static void main(String[] args) throws Exception {
       String message="Welcome";
       KeyGenerator kgen = KeyGenerator.getInstance("AES");
       kgen.init(128); 
       SecretKey skey = kgen.generateKey();
       byte[] raw = skey.getEncoded();

       SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
       Cipher cipher = Cipher.getInstance("AES");

       cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

       byte[] encrypted = cipher.doFinal((args.length == 0 ?"Welcome" : args[0]).getBytes());
       System.out.println("encrypted string: " + asHex(encrypted));

       cipher.init(Cipher.DECRYPT_MODE, skeySpec);
       byte[] original = cipher.doFinal(encrypted);
       String originalString = new String(original);
       System.out.println("Original string: " +originalString + " " + asHex(original));
     }
   }









Related Tutorials/Questions & Answers:
JAVA Cryptography
JAVA Cryptography   Hey Guys am new in here so take it easy on me ... i wanna ask you about java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * Basic symmetric encryption example */ public class
JAVA Cryptography
JAVA Cryptography   Hey Guys am new in here so take it easy on me ... i wanna ask you about java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * Basic symmetric encryption example */ public class
Advertisements
visual cryptography - Java Beginners
visual cryptography  iam doing M.E cse iam undergoing project phase doing project in Halftone visual cryptography may i know how to convert the single image into 2 shares by encoging in java
Java cryptography - Java Beginners
Java cryptography  Hello, I am trying to encrypt/decrypt pdf, image file. but no luck. Can you help me in this regard. - m
cryptography - Security
cryptography  What is cryptography
Cryptography
cryptography
ModuleNotFoundError: No module named 'cryptography'
ModuleNotFoundError: No module named 'cryptography'  Hi, My Python... 'cryptography' How to remove the ModuleNotFoundError: No module named 'cryptography' error? Thanks   Hi, In your python
parallel cryptography
parallel cryptography  how to build java code for encrypting data
ModuleNotFoundError: No module named 'django-cryptography'
ModuleNotFoundError: No module named 'django-cryptography'  Hi, My... named 'django-cryptography' How to remove the ModuleNotFoundError: No module named 'django-cryptography' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-cryptography'
ModuleNotFoundError: No module named 'django-cryptography'  Hi, My... named 'django-cryptography' How to remove the ModuleNotFoundError: No module named 'django-cryptography' error? Thanks   Hi
ModuleNotFoundError: No module named 'sdc-cryptography'
ModuleNotFoundError: No module named 'sdc-cryptography'  Hi, My... named 'sdc-cryptography' How to remove the ModuleNotFoundError: No module named 'sdc-cryptography' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'cryptography-vectors'
ModuleNotFoundError: No module named 'cryptography-vectors'  Hi...: No module named 'cryptography-vectors' How to remove the ModuleNotFoundError: No module named 'cryptography-vectors' error? Thanks   Hi
ModuleNotFoundError: No module named 'cryptography-with-deps'
ModuleNotFoundError: No module named 'cryptography-with-deps'  Hi...: No module named 'cryptography-with-deps' How to remove the ModuleNotFoundError: No module named 'cryptography-with-deps' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-cryptography'
ModuleNotFoundError: No module named 'django-cryptography'  Hi, My... named 'django-cryptography' How to remove the ModuleNotFoundError: No module named 'django-cryptography' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-field-cryptography'
ModuleNotFoundError: No module named 'django-field-cryptography'  Hi...: No module named 'django-field-cryptography' How to remove the ModuleNotFoundError: No module named 'django-field-cryptography' error? Thanks  
ModuleNotFoundError: No module named 'django-encrypted-id-cryptography'
ModuleNotFoundError: No module named 'django-encrypted-id-cryptography' ...: ModuleNotFoundError: No module named 'django-encrypted-id-cryptography' How to remove the ModuleNotFoundError: No module named 'django-encrypted-id-cryptography'
ModuleNotFoundError: No module named 'django-encrypted-id-cryptography'
ModuleNotFoundError: No module named 'django-encrypted-id-cryptography' ...: ModuleNotFoundError: No module named 'django-encrypted-id-cryptography' How to remove the ModuleNotFoundError: No module named 'django-encrypted-id-cryptography'
ModuleNotFoundError: No module named 'django-encrypted-id-cryptography'
ModuleNotFoundError: No module named 'django-encrypted-id-cryptography' ...: ModuleNotFoundError: No module named 'django-encrypted-id-cryptography' How to remove the ModuleNotFoundError: No module named 'django-encrypted-id-cryptography'
a competitive study on cryptography techniques over block cipher
a competitive study on cryptography techniques over block cipher  i need "a competitive study on cryptography techniques over block cipher" project source code... plz reply to my post... for more information about this project
Java One way Hashing
Java One way Hashing One way hash functions are a major tool in cryptography. It is used to create digital signatures, which in turn identify and authenticate the message. It can have other practical applications as well
Java Security
; JCA/JCE (Java Cryptography Architecture & Java Cryptography Extensions... are studying java cryptography encryption over now decrypting enter the password again! abcd1234 we are studying java cryptography // demo6.java
PHP Random Number
PHP Generate Random Numbers: A random number is becoming more useful these days, like captcha, statistical sampling, cryptography, computer simulation etc. To generate random number, PHP provides rand() function. 
Java SE 7
Java SE 7 Java SE was released on 28 July 2011. Its code name is Dolphin... is the list of new features added to the Java 7 : Java Virtual Support for dynamic languages Java HotSpot Virtual Machine Performance Enhancements Changes
how to produce halftone shares? - Java Beginners
the original image.  Hi friend, Visual cryptography encodes a secret... to achieve a higher level of security, halftone visual cryptography was proposed
What is Applet in Java with Example?
What is Applet in Java with Examples? In this section we are going to learn the Applet in Java. In Java Applet are special Java program that runs on the Java enabled web browser. Applet allows the developer to create GUI
What are New Features in JAVA SE 6
Here are the new features of the version 6 of the Java Standard Edition (SE... web services. The latest Java SE 6 provides new adds parsing and XML to Java.... Collections Framework Enhancement: With the new Java SE features, now
Regarding project - Applet
Regarding project  hi friend , iam doing project in Visual cryptography in Java so i need the Help regarding how to make a share of a original imahe into shares anu
New Features of JAVA SE 6.
New Features of JAVA SE 6.   .... Changes in I/O This is a new feature added in Java SE 6, which has...; Java Web Start enhancements in version 6 The cache format of Java
Java API
Java API       What is Java API? Java API is not but a set of classes and interfaces that comes with the JDK. Java API is actually a huge collection of library routines
Generate random numbers in Java
Generate random numbers in Java - How to use the java.util.Random class to generate desired random number in Java program? In this tutorial I will teach you how to write Java code using the java.util.Random class to generate single
Java Example Codes and Tutorials
Java Tutorials - Java Example Codes and Tutorials Java is great programming... the name to Java and modified the language to take advantage of the burgeoning World Wide Web. Java is an object-oriented language, and this is very similar
Java SE 6
Java SE 6       The latest news for the java programmers that the Sun MicroSystems has released the Java SE 6 on Monday December 11. So go
Hi friends
Hi friends  How to create a guy based application for cryptography(encryption and decryption) with multiple algorithms like caesar, hash ..etc
java
java  diff bt core java and java
java
java  what is java
JAVA
JAVA  how the name came for java language as "JAVA
java
java   why iterator in java if we for loop
java
java  explain technologies are used in java now days and structure java
java
java  different between java & core java
Java
Java   Whether Java is pure object oriented Language
java
java  is java open source
java
java  what is java reflection
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
library
(); Course c1 = new Course("Cryptography"); Course c2 = new Course("Java
java
java  give a simple example for inheritance in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java

Ads