Basic symmetric encryption example with CTR using DES : Encryption « Security « Java






Basic symmetric encryption example with CTR using DES

     
import java.security.Security;

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

/**
 * Basic symmetric encryption example with CTR using DES
 */
public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());        
    byte[] input = "www.java2s.com".getBytes();
    byte[] keyBytes = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd,
        (byte) 0xef };
    byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x01 };

    SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    Cipher cipher = Cipher.getInstance("DES/CTR/NoPadding", "BC");
    System.out.println("input : " + new String(input));

    // encryption pass
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength);

    // decryption pass
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    ptLength += cipher.doFinal(plainText, ptLength);
    System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength);
  }
}

           
         
    
    
    
    
  








BasicSymmetricEncryptionExampleWithCTRusingDES.zip( 1,199 k)

Related examples in the same category

1.Basic symmetric encryption example
2.Encryption and decryption with AES/ECB/PKCS7Padding
3.Cipher with AESECBPKCS7Padding BC
4.Basic symmetric encryption example with padding and CBC using DES
5.Basic symmetric encryption example with padding and ECB using DES
6.CBC using DES with an IV based on a nonce: a hypothetical message number
7.Example of using PBE with a PBEParameterSpec
8.Get Cipher Instance Blowfish
9.Message without tampering with MAC (DES), encryption AES in CTR mode
10.Example of using PBE without using a PBEParameterSpec
11.Getting the Bytes of a Generated Symmetric Key
12.Encryption and Decryption using Symmetric Keys
13.Encrypt a password
14.Cryptography Streams: True Mirror
15.Get the formats of the encoded bytes
16.Create an encrypted string for password
17.Cryptography Streams: URLDigest
18.Easy Blowfish encryption
19.This program tests the AES cipher
20.This program tests the RSA cipher
21.Crypt demo
22.Encrypt User name
23.Crypt Utils
24.Encode a string using algorithm specified in web.xml and return the resulting encrypted password.
25.Encrypts the string along with salt, Decrypts the string and removes the salt