java_加密解密

来源:互联网 发布:hadoop与java 编辑:程序博客网 时间:2024/06/13 20:52
/** * 加密解密(使用方法直接看最下面的main方法) * @author liupeng * */public class LiupengEnDe{private static final String liupengKey = "lp_294333475";private static boolean InitRC4(byte[] pRC4Table, byte[] pKeyTable){int i;int b=0;int Temp;int KeyLen = pKeyTable.length;//置初值for(i=0;i<256;i++){pRC4Table[i]= (byte)i;}//变值for(i=0;i<256;i++){    b = (b + (pRC4Table[i]& 0x0FF) + (pKeyTable[i % KeyLen]& 0x0ff)) % 256;    Temp = (int)pRC4Table[i];    pRC4Table[i] = pRC4Table[b];    pRC4Table[b] = (byte)Temp;}return true;}private static boolean InitBase64(byte[] pDecodeTable, byte[] pBase64Tab){byte[] tDecodeTable ={(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, 62 , (byte)255, (byte)255, (byte)255, 63 , 52 , 53 ,54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , (byte)255, (byte)255,(byte)255, 64 , (byte)255, (byte)255, (byte)255, 0  , 1  , 2  , 3  , 4  ,5  , 6  , 7  , 8  , 9  , 10 , 11 , 12 , 13 , 14 ,15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 ,25 , (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, 26 , 27 , 28 ,29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 ,39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 ,49 , 50 , 51 , (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255,(byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)55, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255, (byte)255,(byte)255, (byte)255, (byte)255, (byte)255};int iLoop;for(iLoop=0;iLoop<234;iLoop++)pDecodeTable[iLoop] = tDecodeTable[iLoop];for(iLoop=65;iLoop<=90;iLoop++)pBase64Tab[iLoop - 65] = (byte)iLoop;for(iLoop=97;iLoop<=122;iLoop++)pBase64Tab[iLoop - 71] = (byte)iLoop;for(iLoop=0;iLoop<=9;iLoop++)pBase64Tab[iLoop + 52] =(byte)(48+iLoop);pBase64Tab[62] = 43;pBase64Tab[63] = 47;return true;}public static byte[] Base64EncryptByte(byte[] DataIn){byte[] Base64Tab=new byte[64];byte[] DecodeTable=new byte[234];InitBase64(DecodeTable,Base64Tab);byte[] DataTemp = new byte[4];int iTemp=0;int iLoop=0;int BytesDataIn=0;int BytesDataOut=0;int ExtraBytes=0;byte[] DataOut = null;//BytesDataIn = _mbslen(DataIn);BytesDataIn = DataIn.length;ExtraBytes = BytesDataIn % 3;if(ExtraBytes == 0)      BytesDataOut = (BytesDataIn / 3) * 4;else      BytesDataOut = ((int)(BytesDataIn / 3) + 1) * 4;    DataOut = new byte[BytesDataOut];for(iLoop = 0;iLoop<=(BytesDataIn - ExtraBytes - 1);iLoop+=3){DataOut[iTemp] = Base64Tab[((int)((DataIn[iLoop] & 0x0ff) / 4) & 0x03F)];DataOut[iTemp + 1] = Base64Tab[(DataIn[iLoop] & 0x003) * 16 | ((int)((DataIn[iLoop + 1]&0x0ff) / 16)) & 0x0F];DataOut[iTemp + 2] = Base64Tab[(DataIn[iLoop + 1] & 0x00F) * 4 | ((int)((DataIn[iLoop + 2]&0x0ff) / 64)) & 0x03];DataOut[iTemp + 3] = Base64Tab[DataIn[iLoop + 2] & 0x03F];iTemp += 4;}if(ExtraBytes == 1){DataTemp[0] = DataIn[BytesDataIn-1];DataTemp[1] = 0;DataTemp[2] = 0;DataOut[BytesDataOut - 4] = Base64Tab[(int)((DataTemp[0]& 0x0ff) / 4) & 0x3F];DataOut[BytesDataOut - 3] = Base64Tab[(DataTemp[0] & 0x003) * 16 | (int)((DataTemp[1] & 0x0ff) / 16) & 0x0F];DataOut[BytesDataOut - 2] = 61;DataOut[BytesDataOut - 1] = 61;}else if (ExtraBytes == 2){DataTemp[0] = DataIn[BytesDataIn - 2];DataTemp[1] = DataIn[BytesDataIn - 1];DataTemp[2] = 0;DataOut[BytesDataOut - 4] = Base64Tab[(int)((DataTemp[0]&0x0ff)/ 4) & 0x3F];DataOut[BytesDataOut - 3] = Base64Tab[(DataTemp[0] & 0x003) * 16 | (int)((DataTemp[1] & 0x0ff) / 16) & 0x0F];DataOut[BytesDataOut - 2] = Base64Tab[(DataTemp[1] & 0x00F) * 4 | (int)((DataTemp[2]&0x0ff) / 64) & 0x03];DataOut[BytesDataOut - 1] = 61;}return DataOut;} public static byte[] Base64DecryptByte(byte[] DataIn) throws Exception { byte[] Base64Tab=new byte[64]; byte[] DecodeTable=new byte[234]; byte[] DataOut = null; InitBase64(DecodeTable,Base64Tab); byte[] DataTemp = new byte[4]; int iTemp=0; int iLoop=0; int BytesDataIn=0; int ExtraBytes=0; BytesDataIn = DataIn.length; if(DataIn[BytesDataIn-1] == 61){ ExtraBytes = 1; if(DataIn[BytesDataIn-2] == 61) ExtraBytes = 2; } int BytesDataOut = (int)((BytesDataIn * 0.75) - ExtraBytes);    DataOut = new byte[BytesDataOut];    try{ for(iLoop = 0;iLoop<(BytesDataIn - 5);iLoop+=4){ //DataTemp[0] = DecodeTable[DataIn[iLoop]]; DataTemp[1] = DecodeTable[DataIn[iLoop + 1]]; DataTemp[2] = DecodeTable[DataIn[iLoop + 2]]; //DataTemp[3] = DecodeTable[DataIn[iLoop + 3]];         DataOut[iTemp] = (byte) ((DecodeTable[DataIn[iLoop]] * 4) | ((int)(DataTemp[1] / 16) & 0x03)); DataOut[iTemp + 1] = (byte) (((DataTemp[1] & 0x0F) * 16) | ((int)(DataTemp[2] / 4) & 0x0F)); DataOut[iTemp + 2] = (byte) (((DataTemp[2] & 0x03) * 64) | DecodeTable[DataIn[iLoop + 3]]); iTemp += 3; }    } catch (Exception e){    throw new Exception("It is not BASE64 encode string");    }    DataTemp[0] = DecodeTable[DataIn[iLoop]]; DataTemp[1] = DecodeTable[DataIn[iLoop + 1]]; DataTemp[2] = DecodeTable[DataIn[iLoop + 2]]; DataTemp[3] = DecodeTable[DataIn[iLoop + 3]];    if(DataTemp[3] == 64 || DataTemp[2] == 64){ if(DataTemp[3] == 64 && DataTemp[2] != 64){ DataTemp[0] = DecodeTable[DataIn[iLoop]]; DataTemp[1] = DecodeTable[DataIn[iLoop + 1]]; DataTemp[2] = DecodeTable[DataIn[iLoop + 2]]; DataOut[iTemp] = (byte) ((DataTemp[0] * 4) | ((int)(DataTemp[1] / 16) & 0x03)); DataOut[iTemp + 1] = (byte) (((DataTemp[1] & 0x0F) * 16) | ((int)(DataTemp[2] / 4) & 0x0F)); return DataOut; }else if(DataTemp[2] == 64){ DataTemp[0] = DecodeTable[DataIn[iLoop]]; DataTemp[1] = DecodeTable[DataIn[iLoop + 1]]; DataOut[iTemp] = (byte) ((DataTemp[0] * 4) | ((int)(DataTemp[1] / 16) & 0x03)); return DataOut; } }    DataOut[iTemp] = (byte) ((DataTemp[0] * 4) | ((int)(DataTemp[1] / 16) & 0x03)); DataOut[iTemp + 1] = (byte) (((DataTemp[1] & 0x0F) * 16) | ((int)(DataTemp[2] / 4) & 0x0F)); DataOut[iTemp + 2] = (byte) (((DataTemp[2] & 0x03) * 64) | DataTemp[3]); return DataOut; }  public static boolean RC4EnDecryptByte(byte[] Key, byte[] EnDecryptData) { //初始化 byte[] pRC4Table=new byte[256]; InitRC4(pRC4Table,Key); int i,j; int iLoop; int Temp; int EnDecryptDataLen = EnDecryptData.length; //加密数据 i=0; j=0; for(iLoop=0;iLoop<(int)EnDecryptDataLen;iLoop++) { i = (i + 1) % 256; j = (j + pRC4Table[i] & 0x0ff) % 256; Temp = (int)pRC4Table[i]; pRC4Table[i]=pRC4Table[j]; pRC4Table[j]=(byte)Temp; EnDecryptData[iLoop]=(byte)( EnDecryptData[iLoop] ^ (pRC4Table[((pRC4Table[i] & 0x0ff) + (pRC4Table[j] & 0x0ff)) % 256])); } return true; }  public static String LiupengEncryptString(String dataIn) { byte[] dataInBytes = dataIn.getBytes(); RC4EnDecryptByte(liupengKey.getBytes(), dataInBytes); byte[] base64Bytes = Base64EncryptByte(dataInBytes); String retString = new String(base64Bytes); return retString;  }  public static String LiupengEncryptBytes(byte[] dataIn) { RC4EnDecryptByte(liupengKey.getBytes(), dataIn); byte[] base64Bytes = Base64EncryptByte(dataIn); String retString = new String(base64Bytes); return retString;  }  public static String LiupengDecryptString(String dataIn) throws Exception { byte[] dataInBase64 = Base64DecryptByte(dataIn.getBytes()); RC4EnDecryptByte(liupengKey.getBytes(), dataInBase64); String retString = new String(dataInBase64); return retString; }  public static byte[] LiupengDecryptToBytes(String dataIn) throws Exception { byte[] dataInBase64 = Base64DecryptByte(dataIn.getBytes()); RC4EnDecryptByte(liupengKey.getBytes(), dataInBase64); return dataInBase64; }  public static String LiupengEncryptString(String dataIn, String userKey) { byte[] dataInBytes = dataIn.getBytes(); StringBuilder sb = new StringBuilder(); sb.append(userKey).append(liupengKey).append(userKey); RC4EnDecryptByte(sb.toString().getBytes(), dataInBytes); byte[] base64Bytes = Base64EncryptByte(dataInBytes); String retString = new String(base64Bytes); return retString;  }  public static String LiupengDecryptString(String dataIn, String userKey) throws Exception { byte[] dataInBase64 = Base64DecryptByte(dataIn.getBytes()); StringBuilder sb = new StringBuilder(); sb.append(userKey).append(liupengKey).append(userKey); RC4EnDecryptByte(sb.toString().getBytes(), dataInBase64); String retString = new String(dataInBase64); return retString; }  public static void main(String[] args) throws Exception{ String JPwd = LiupengEnDe.LiupengEncryptString("1234", "liupeng");System.out.println(JPwd);//eI8snA==String OPwd = LiupengEnDe.LiupengDecryptString("eI8snA==", "liupeng");System.out.println(OPwd);//1234String test = LiupengEnDe.LiupengEncryptString("1234");System.out.println(test);//WOGKxg==String test1 = LiupengEnDe.LiupengDecryptString("WOGKxg==");System.out.println(test1);//1234 }}

0 0
原创粉丝点击