可以加密和解密。汉字和英文都要可以。

解决方案 »

  1.   


    package com.sxzlc.test.rc4;public class RC4 {
    public static byte[] RC42(byte[] aInput, byte[] aKey) {
    int[] iS = new int[256];
    byte[] iK = new byte[256];for (int i = 0; i < 256; i++)
    iS[i] = i;int j = 1;for (short i = 0; i < 256; i++) {
    iK[i] = (byte) aKey[i%aKey.length];
    }j = 0;for (int i = 0; i < 256; i++) {
    j = (j + iS[i] + iK[i]) &0xff;
    int temp = iS[i]&0xff;
    iS[i] = iS[j]&0xff;
    iS[j] = temp;
    }int i = 0;
    j = 0;for (int x = 0; x < aInput.length; x++) {
    i = (i + 1) &0xff;
    j = (j + iS[i]) &0xff;
    int temp = iS[i]&0xff;
    iS[i] = iS[j]&0xff;
    iS[j] = temp;
    int t = ((iS[i] + iS[j] ))&0xff;
    int iY = iS[t];
    byte iCY = (byte) (iY&0xff);
    aInput[x] = (byte) ((aInput[x]^iCY)&0xff);
    }return aInput;}
        public static void main(String[] args)  
    {  
    String inputStr = "你们好啊";  
    String key = "961633";  
    byte[] bytes = inputStr.getBytes();
    //打印加密后的字符串  
    bytes = RC4.RC42(inputStr.getBytes(), key.getBytes());
    inputStr = new String(bytes);
    System.out.println( inputStr );  
    //打印加密后的字符串  
    bytes = RC4.RC42(inputStr.getBytes(), key.getBytes());
    inputStr = new String(bytes);
    System.out.println( inputStr );  
    }}
      

  2.   

    附送MD5import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;public class MD5Digest
    {    private MessageDigest md5 = null;
        private StringBuffer digestBuffer = null;    public MD5Digest()
            throws NoSuchAlgorithmException
        {
            md5 = MessageDigest.getInstance("MD5");
            digestBuffer = new StringBuffer();
        }    public String md5crypt(String s)
        {
            digestBuffer.setLength(0);
            byte abyte0[] = md5.digest(s.getBytes());
            for(int i = 0; i < abyte0.length; i++)
                digestBuffer.append(toHex(abyte0[i]));        return digestBuffer.toString();
        }
        public String toHex(byte one){
       String HEX="0123456789ABCDEF";
       char[] result=new char[2];
       result[0]=HEX.charAt((one & 0xf0) >> 4);
       result[1]=HEX.charAt(one & 0x0f);
       String mm=new String(result);
       return mm;
      }
    }