maher.bahjat.layous
JAVA Cryptography
1 Answer(s)      5 years and a month 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");

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

```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

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

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
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
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 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
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
java  diff bt core java and java
Java API
Java Cryptography Extension JCE... Java API       What is Java API? Java API is not but a set of classes and interfaces
java
java  what is java
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
Java
Java   Whether Java is pure object oriented Language
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
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   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
Tutorials