Cryptography

Cryptography

I am trying to make an algo for encryption and decrytion.But I dont want to use the existing Algo's like dES,AES,RC4 etc.I want help for logic in writing an algo of my own.

View Answers

March 21, 2012 at 3:14 PM

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

class EncryptAndDecrypt {

public static void main (String[] args) throws Exception{
KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keygenerator.initialize(1024, random);

KeyPair keypair = keygenerator.generateKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);
BufferedReader br=new BufferedReader(new FileReader(new File("data.txt")));
String st,str;
while((st=br.readLine()) != null)   {
    str+=st+" ";
}
byte[] cleartext = null;
cleartext = str.getBytes();
byte[] ciphertext = null;
ciphertext = cipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + ciphertext.toString());

cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cleartext1 = cipher.doFinal(ciphertext);
System.out.println("the decrypted cleartext is: " + new String(cleartext1));
}
}

March 21, 2012 at 3:30 PM

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

class EncryptAndDecrypt {

public static void main (String[] args) throws Exception{ 
    SecretKeyFactory key = SecretKeyFactory.getInstance("DES");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

    byte[] desKeyData = { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08 }; 
    DESKeySpec desKeySpec = new DESKeySpec(desKeyData);

    Key keys = key.generateSecret(desKeySpec);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, keys);
    String st= "Welcome";
    byte[] cleartext = null;
    cleartext = st.getBytes();
    byte[] ciphertext = null;
    ciphertext = cipher.doFinal(cleartext); 
    System.out.println("the encrypted text is: " + ciphertext.toString());

    cipher.init(Cipher.DECRYPT_MODE, keys); 
    byte[] cleartext1 = cipher.doFinal(ciphertext);
    System.out.println("the decrypted cleartext is: " + new String(cleartext1));
    }
}

March 21, 2012 at 5:19 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:
Cryptography
cryptography
Advertisements
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
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
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-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 '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'
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  
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
parallel cryptography
Java Security
) In this tutorial, the author explains the cryptography-related concepts... learnt will be useful in understanding the Cryptography API in MFC also...; JCA/JCE (Java Cryptography Architecture & Java Cryptography Extensions
Hi friends
Hi friends  How to create a guy based application for cryptography(encryption and decryption) with multiple algorithms like caesar, hash ..etc
Who invented Blockchain technology
uses cryptography for the data into Ledger. Data stored in Ledger... discussed this technology to save digital record using cryptography with timestamp
What is blockchain technology
is a technology which uses cryptography and other IT technologies to store data
What is blockchain technology
is a technology which uses cryptography and other IT technologies to store data
What is blockchain technology
is a technology which uses cryptography and other IT technologies to store data
library
(); Course c1 = new Course("Cryptography"); Course c2 = new Course("Java
What is Blockchain?
. This technology of tamper-proof data block is achieved through cryptography. The contents... summarize it as the decentralized, consensus-driven, immutable, cryptography
What is Blockchain?
. This technology of tamper-proof data block is achieved through cryptography. The contents... summarize it as the decentralized, consensus-driven, immutable, cryptography
Other Base Packages
and authorization, configurable access control, digital signing, cryptography
Java SE 7
: Elliptic Curve Cryptography (ECC), CertPath Algorithm Disabling  etc Swing
What are New Features in JAVA SE 6
of security APIs. These Security APIs span a wide range of areas, including cryptography
What is Applet in Java with Example?
including animation applets. Applet program can be signed with the cryptography
Blockchain Tutorial
of Blockchain technology. To work with the cryptography and cryptocurrency

Ads