對稱式加密解密

來源:互聯網
上載者:User

import javax.crypto.*;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;

/**
 * Created by IntelliJ IDEA.
 * User: LEO
 * Date: 2005-11-30
 * Time: 11:19:23
  */
public class EncryptDecryptData{
    static String str = "南京金脈博通";
    public static void main(String[] args)  {
        System.out.println("明文");
        System.out.println(str);
        byte[] str1 = new byte[0];

        KeyGenerator keygen = null;
        SecretKey secretkey = null;
        try {
            str1 = str.getBytes("gb2312");
            keygen = KeyGenerator.getInstance("DESede");
            keygen.init(168);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        secretkey = keygen.generateKey();
        byte[] kb = secretkey.getEncoded();
        int i=0;
        System.out.println("密鑰");
        for(i=0;i<kb.length;i++)
        {
            System.out.print(kb[i]+",");
        }
        Cipher cipher = null;
        byte[] encrypted = new byte[0];
        try {
            cipher = Cipher.getInstance("DESede");
            cipher.init(Cipher.ENCRYPT_MODE,secretkey);
            encrypted = cipher.doFinal(str1);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        System.out.println();
        System.out.println("密文");
        for(i=0;i<encrypted.length;i++)
        {
            System.out.print(encrypted[i]+",");
        }
        byte[] decrypted = new byte[0];
        try {
            cipher.init(Cipher.DECRYPT_MODE,secretkey);
            decrypted = cipher.doFinal(encrypted);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        String str2 = new String(decrypted);
        System.out.println("解密後的原文");
        System.out.println(str2);
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.