JAVA Cryptography

Ads

 
 

Share on Google+Share on Google+

maher.bahjat.layous
JAVA Cryptography
1 Answer(s)      5 years and 7 months ago
Posted in : Java Beginners

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

Ads
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));
     }
   }

Ads









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
parallel cryptography
parallel cryptography  how to build java code for encrypting data
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...! abcd1234 we are studying java cryptography // demo6.java import... BOOKS Mastering Java Security (Cryptography, Algorithms & Architecture
java
java  diff bt core java and java
java
java  what is 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  how the name came for java language as "JAVA
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
java
java   What is ?static? keyword
java
java  RARP implementation using java socket
java
java  sample code for RARP using java
java
java  Does java allows multiline comments
Java
Java  how to do java in command prompt
java
java  give a simple example for inheritance in 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

Ads

 
Advertisement null

Ads