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 Beginnersvisual 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 One way HashingJava 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 NumberPHP 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 7Java 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
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 6Here 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 - AppletRegarding 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 JavaGenerate 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 TutorialsJava 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 friendsHi friends How to create a guy based application for
cryptography(encryption and decryption) with multiple algorithms like caesar, hash ..etc
javajava diff bt core
java and
java JAVAJAVA how the name came for
java language as "
JAVA java java why iterator in
java if we for loop
javajava explain technologies are used in
java now days and structure
java javajava different between
java & core
java Java Java Whether
Java is pure object oriented Language
javajava is
java open source
javajava what is
java reflection
java java in
java does not pointers concept but what is nullpointers in
java?
nullpointer is a runtime Exception
javawhat 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 javajava give a simple example for inheritance in
java javajava why to set classpath in
java javajava why to set classpath in
java javajava why to set classpath in
java