数据加密解密之DES与AES

说明

DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级政府通信中使用,随后该算法在国际上广泛流传开来。

AES是高级加密标准(英语:Advanced Encryption Standard),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

快速开始

直接上代码

BASE64(使用自己编写的BASE64可以避免每76个字符就换行的问题,换行是国际标准。。。)
/** A simple base64 encoding and decoding utility class
 * it can also encode and decode non ASII characters such as 
 * Chinese
 */

/**
 * This software is provided "AS IS," without a warranty of any kind.
 * anyone can use it for free,emails are welcomed concerning bugs or
 * suggestions.
 */

package dao;

public final class Base64
   {

    private final char[] base64Map =  //base64 character table

         { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
           'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
           'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
           'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
           '4','5','6','7','8','9','+','/' } ;
 
  /** convert the platform dependent string characters to UTF8 which can 
    * also be done by calling the java String method getBytes("UTF-8"),but I
	* hope to do it from the ground up.
	*/

    private byte[] toUTF8ByteArray(String s) 
	{
		 int ichar; 
		 byte buffer[]=new byte[3*(s.length())];
         byte hold[];
		 int index=0;
	     int count=0; //count the actual bytes in the
	                      //buffer array 		 
			 
		 for(int i=0;i<s.length();i++)
		 { 
		       ichar = (int)s.charAt(i);

		  	   //determine the bytes for a specific character
			   if((ichar>=0x0080)&(ichar<=0x07FF))
				 {
				   buffer[index++] = (byte)((6<<5)|((ichar>>6)&31));
				   buffer[index++] = (byte)((2<<6)|(ichar&63));
				   count+=2;
				 }
			  
			   //determine the bytes for a specific character
			   else if((ichar>=0x0800)&(ichar<=0x0FFFF))
				 {
				   buffer[index++] = (byte)((14<<4)|((ichar>>12)&15));
				   buffer[index++] = (byte)((2<<6)|((ichar>>6)&63));
				   buffer[index++] = (byte)((2<<6)|(ichar&63));
				   count+=3;
				 }
				 
		      //determine the bytes for a specific character
			   else if((ichar>=0x0000)&(ichar<=0x007F))
				 {
                   buffer[index++] = (byte)((0<<7)|(ichar&127));  
				   count+=1;
				 }

              //longer than 16 bit Unicode is not supported
			   else throw new RuntimeException("Unsupported encoding character length!\n");
		 }
         hold = new byte[count];
         System.arraycopy(buffer,0,hold,0,count); //trim
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值