Java DES Encryption and decryption source code

Source: Internet
Author: User
Tags decrypt

The Java Cryptography architecture is designed to follow two principles:

1) The independence and reliability of the algorithm.

2) Independence and interaction of the implementation.

The independence of the algorithm is obtained by defining the password service class. Users only need to understand the concept of cryptographic algorithms without having to care about how to implement these concepts. The independence and interaction of the implementation is achieved through a cryptographic service provider. A password service provider is one or more packages that implement one or more password services. Software developers based on a certain interface, the implementation of various algorithms, packaged into a provider, the user can install a different provider. Install and configure the provider to place the zip and jar files containing the provider under Classpath, and then edit the Java security properties file to set the definition of a provider.

Des algorithm and the steps of how to encrypt and decrypt class files using des algorithm:

Introduction to DES algorithms
DES (Data Encryption Standard) is the earliest invention of the most widely used packet symmetric encryption algorithm. The DES algorithm has three entry parameters: Key, Data, Mode. Where key is 8 bytes A total of 64 bits, is the working key of the DES algorithm, the data is 8 bytes 64 bits, is to be encrypted or decrypted, the mode for DES works, there are two kinds: encryption or decryption.

Tags:< no > Code Snippets (1)
1. [Code]java encryption and decryption source
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 package com.afreon.util;import java.io.IOException;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class DesUtil {    private final static String DES = "DES";    public static void main(String[] args) throws Exception {        String data = "123 456";        String key = "[email protected]#$%";        System.err.println(encrypt(data, key));        System.err.println(decrypt(encrypt(data, key), key));    }        /**     * Description 根据键值进行加密     * @param data      * @param key  加密键byte数组     * @return     * @throws Exception     */    public static String encrypt(String data, String key) throws Exception {        byte[] bt = encrypt(data.getBytes(), key.getBytes());        String strs = new BASE64Encoder().encode(bt);        return strs;    }    /**     * Description 根据键值进行解密     * @param data     * @param key  加密键byte数组     * @return     * @throws IOException     * @throws Exception     */    public static String decrypt(String data, String key) throws IOException,            Exception {        if (data == null)            return null;        BASE64Decoder decoder = new BASE64Decoder();        byte[] buf = decoder.decodeBuffer(data);        byte[] bt = decrypt(buf,key.getBytes());        return new String(bt);    }    /**     * Description 根据键值进行加密     * @param data     * @param key  加密键byte数组     * @return     * @throws Exception     */    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {        // 生成一个可信任的随机数源        SecureRandom sr = new SecureRandom();        // 从原始密钥数据创建DESKeySpec对象        DESKeySpec dks = new DESKeySpec(key);        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        SecretKey securekey = keyFactory.generateSecret(dks);        // Cipher对象实际完成加密操作        Cipher cipher = Cipher.getInstance(DES);         // 用密钥初始化Cipher对象        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);        return cipher.doFinal(data);    }            /**     * Description 根据键值进行解密     * @param data     * @param key  加密键byte数组     * @return     * @throws Exception     */    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {        // 生成一个可信任的随机数源        SecureRandom sr = new SecureRandom();         // 从原始密钥数据创建DESKeySpec对象        DESKeySpec dks = new DESKeySpec(key);        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        SecretKey securekey = keyFactory.generateSecret(dks);        // Cipher对象实际完成解密操作        Cipher cipher = Cipher.getInstance(DES);        // 用密钥初始化Cipher对象        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);        return cipher.doFinal(data);    }}来源地址:http://www.yoodb.com/article/display/266

Java DES Encryption and decryption source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.