说明
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