博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 加密解密
阅读量:4048 次
发布时间:2019-05-25

本文共 5307 字,大约阅读时间需要 17 分钟。

/
*
*
  
*
@
(
#
)
Eryptogram.java
1.00
04
/
03
/
11
  
*
  
*
Copyright
(
c
)
2003
-
2004
Abacus
,
Ltd.
  
*
  
*
加密解密类
  
*
  
*
  
*
  
*
/
  
package  com.abacus.common ;
  
import  java.security. *;
  
import  javax.crypto. *;
  
/
*
*
  
*
加密解密类
  
*
@
author
WangHu
  
*
@
version
1.00
2004年03月18日
  
*
/
  
public 
class  Eryptogram 
  
{
  
     private  static  String  Algorithm ="DES";
       //定义 加密算法,可用 DES,DESede,Blowfish
       static  boolean  debug  = false ;
       /**
       * 构造子注解.
       */
       public  Eryptogram ()
      {
          
  
    } /**
       * 生成密钥
       * @return byte[] 返回生成的密钥
       * @throws exception 扔出异常.
       */
       public  static  byte [] getSecretKey () throws  Exception 
      {
           KeyGenerator  keygen  = KeyGenerator.getInstance (Algorithm );
           SecretKey  deskey  = keygen.generateKey ();
           if  (debug ) System.out.println ("生成密钥:"+byte2hex (deskey.getEncoded ()));
           return  deskey.getEncoded ();
          
  
    } /**
       * 将指定的数据根据提供的密钥进行加密
       * @param input 需要加密的数据
       * @param key 密钥
       * @return byte[] 加密后的数据
       * @throws Exception
       */
       public  static  byte [] encryptData (byte [] input ,byte [] key ) throws  Exception 
      {
           SecretKey  deskey  = new  javax.crypto.spec.SecretKeySpec (key ,Algorithm );
           if  (debug )
          {
               System.out.println ("加密前的二进串:"+byte2hex (input ));
               System.out.println ("加密前的字符串:"+new  String (input ));
              
  
        } Cipher  c1  = Cipher.getInstance (Algorithm );
           c1.init (Cipher.ENCRYPT_MODE ,deskey );
           byte [] cipherByte =c1.doFinal (input );
           if  (debug ) System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));
           return  cipherByte ;
          
  
    } /**
       * 将给定的已加密的数据通过指定的密钥进行解密
       * @param input 待解密的数据
       * @param key 密钥
       * @return byte[] 解密后的数据
       * @throws Exception
       */
       public  static  byte [] decryptData (byte [] input ,byte [] key ) throws  Exception 
      {
           SecretKey  deskey  = new  javax.crypto.spec.SecretKeySpec (key ,Algorithm );
           if  (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
           Cipher  c1  = Cipher.getInstance (Algorithm );
           c1.init (Cipher.DECRYPT_MODE ,deskey );
           byte [] clearByte =c1.doFinal (input );
           if  (debug )
          {
               System.out.println ("解密后的二进串:"+byte2hex (clearByte ));
               System.out.println ("解密后的字符串:"+(new  String (clearByte )));
              
  
        } return  clearByte ;
          
  
    } /**
       * 字节码转换成16进制字符串
       * @param byte[] b 输入要转换的字节码
       * @return String 返回转换后的16进制字符串
       */
       public  static  String  byte2hex (byte [] b )
      {
           String  hs ="";
           String  stmp ="";
           for  (int  n =0 ;n <b.length ;n ++)
          {
               stmp =(java.lang.Integer.toHexString (b [n ] &  0XFF ));
               if  (stmp.length ()==1 ) hs =hs +"0"+stmp ;
               else  hs =hs +stmp ;
               if  (n <b.length -1 ) hs =hs +":";
              
  
        } return  hs.toUpperCase ();
          
  
    } public  static  void  main (String [] args )
      {
           try 
          {
               debug  = false ;
               Eryptogram  etg  = new  Eryptogram ();
               byte [] key  = etg.getSecretKey ();
               System.out.println ("key = "+key );
               String  aa  = "1234567";
               byte [] data  = aa.getBytes ();
               System.out.println (data );
               byte [] en  = etg.encryptData (data ,key );
               System.out.println ("encryptData = "+new  String (en ));
               byte [] de  = etg.decryptData (en ,key );
               System.out.println ("decryptData = "+new  String (de ));
              
  
        }catch (Exception  e )
          {
               e.printStackTrace ();
              
  
        }
  
    }
  
}

 

 

[code]/** * @(#)Eryptogram.java 1.00 04/03/11 * * Copyright (c) 2003-2004 Abacus,Ltd. * * 加密解密类 * * * */ package com.abacus.common; import java.security.*; import javax.crypto.*; /** * 加密解密类 * @author WangHu * @version 1.00 2004年03月18日 */ public class Eryptogram{ private static String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish static boolean debug = false; /** * 构造子注解. */ public Eryptogram(){ } /** * 生成密钥 * @return byte[] 返回生成的密钥 * @throws exception 扔出异常. */ public static byte[] getSecretKey() throws Exception{ KeyGenerator keygen = KeyGenerator.getInstance(Algorithm); SecretKey deskey = keygen.generateKey(); if (debug) System.out.println("生成密钥:"+byte2hex(deskey.getEncoded())); return deskey.getEncoded(); } /** * 将指定的数据根据提供的密钥进行加密 * @param input 需要加密的数据 * @param key 密钥 * @return byte[] 加密后的数据 * @throws Exception */ public static byte[] encryptData(byte[] input,byte[] key) throws Exception{ SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm); if (debug){ System.out.println("加密前的二进串:"+byte2hex(input)); System.out.println("加密前的字符串:"+new String(input)); } Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE,deskey); byte[] cipherByte=c1.doFinal(input); if (debug) System.out.println("加密后的二进串:"+byte2hex(cipherByte)); return cipherByte; } /** * 将给定的已加密的数据通过指定的密钥进行解密 * @param input 待解密的数据 * @param key 密钥 * @return byte[] 解密后的数据 * @throws Exception */ public static byte[] decryptData(byte[] input,byte[] key) throws Exception{ SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm); if (debug) System.out.println("解密前的信息:"+byte2hex(input)); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE,deskey); byte[] clearByte=c1.doFinal(input); if (debug){ System.out.println("解密后的二进串:"+byte2hex(clearByte)); System.out.println("解密后的字符串:"+(new String(clearByte))); } return clearByte; } /** * 字节码转换成16进制字符串 * @param byte[] b 输入要转换的字节码 * @return String 返回转换后的16进制字符串 */ public static String byte2hex(byte[] b) { String hs=""; String stmp=""; for (int n=0;n 

转载地址:http://wabci.baihongyu.com/

你可能感兴趣的文章
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
Android/Linux 内存监视
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
android raw读取超过1M文件的方法
查看>>
MPMoviePlayerViewController和MPMoviePlayerController的使用
查看>>
CocoaPods实践之制作篇
查看>>
[Mac]Mac 操作系统 常见技巧
查看>>
苹果Swift编程语言入门教程【中文版】
查看>>