`
cwmwss
  • 浏览: 4940 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

JAVA与.NET的同步DES加解密

阅读更多
.NET中DES加解密
  #region DESEnCode DES加密
        public static string DESEnCode(string pToEncrypt, string sKey)
        {
            pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = System.Security.Cryptography.CipherMode.ECB;
            byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);

            //建立加密对象的密钥和偏移量   
            //原文使用ASCIIEncoding.ASCII方法的GetBytes方法   
            //使得输入密码必须输入英文文本   
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
        }
        #endregion       


        #region DESDeCode DES解密
        public static string DESDeCode(string pToDecrypt, string sKey)
        {
            //    HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);  
            //    HttpContext.Current.Response.End();  
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = System.Security.Cryptography.CipherMode.ECB;
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
        }
        #endregion
 

 java与上面.NET同步的DES加解密

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class Encrypt {  
      public static String myDecrypt(String message, String key) throws Exception {

        byte[] bytesrc = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("US-ASCII"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("US-ASCII"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    public static String myEncrypt(String message, String key) throws Exception {
        //message = java.net.URLEncoder.encode(message, "ascii");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("US-ASCII"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("US-ASCII"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        byte[] encryptbyte = cipher.doFinal(message.getBytes());
        return toHexString(encryptbyte).toUpperCase();
       
    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }

        return digest;
    }
    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }

        return hexString.toString();
    }

}

 

JS单独的DES加解密

<html>
<head>
<title>DES加密</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb3212">
<script type="text/javascript">
<!--//des加解密函数
//key:加密用的密钥
//message:需要加密的字符串
//encrypt:加密还是解密,1为加密,0,解密
function des(key,message,encrypt,mode,iv){
 //declaring this locally speeds things up a bit
 var spfunction1=new Array(0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004);
 var spfunction2=new Array(-0x7fef7fe0,-0x7fff8000,0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-0x7fff7fe0,-0x7fffffe0,-0x7fef7fe0,-0x7fef8000,-0x80000000,-0x7fff8000,0x100000,0x20,-0x7fefffe0,0x108000,0x100020,-0x7fff7fe0,0,-0x80000000,0x8000,0x108020,-0x7ff00000,0x100020,-0x7fffffe0,0,0x108000,0x8020,-0x7fef8000,-0x7ff00000,0x8020,0,0x108020,-0x7fefffe0,0x100000,-0x7fff7fe0,-0x7ff00000,-0x7fef8000,0x8000,-0x7ff00000,-0x7fff8000,0x20,-0x7fef7fe0,0x108020,0x20,0x8000,-0x80000000,0x8020,-0x7fef8000,0x100000,-0x7fffffe0,0x100020,-0x7fff7fe0,-0x7fffffe0,0x100020,0x108000,0,-0x7fff8000,0x8020,-0x80000000,-0x7fefffe0,-0x7fef7fe0,0x108000);
 var spfunction3=new Array(0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200);
 var spfunction4=new Array(0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080);
 var spfunction5=new Array(0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100);
 var spfunction6=new Array(0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010);
 var spfunction7=new Array(0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002);
 var spfunction8=new Array(0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000);

 //create the 16 or 48 subkeys we will need
 var keys=des_createKeys(key);
 var m=0,i,j,temp,temp2,right1,right2,left,right,looping;
 var cbcleft,cbcleft2,cbcright,cbcright2
 var endloop,loopinc;
 var len=message.length;
 var chunk=0;
 //set up the loops for single and triple des
 var iterations=keys.length==32?3 :9;//single or triple des
 if(iterations==3){looping=encrypt?new Array(0,32,2):new Array(30,-2,-2);}
 else{looping=encrypt?new Array(0,32,2,62,30,-2,64,96,2):new Array(94,62,-2,32,64,2,30,-2,-2);}

 message+="\0\0\0\0\0\0\0\0";//pad the message out with null bytes
 //store the result here
 result="";
 tempresult="";

 if(mode==1){//CBC mode
 cbcleft=(iv.charCodeAt(m++)<<24)|(iv.charCodeAt(m++)<<16)|(iv.charCodeAt(m++)<<8)|iv.charCodeAt(m++);
 cbcright=(iv.charCodeAt(m++)<<24)|(iv.charCodeAt(m++)<<16)|(iv.charCodeAt(m++)<<8)|iv.charCodeAt(m++);
 m=0;
 }

 //loop through each 64 bit chunk of the message
 while(m<len){
 if(encrypt){/*加密时按双字节操作*/
 left=(message.charCodeAt(m++)<<16)|message.charCodeAt(m++);
 right=(message.charCodeAt(m++)<<16)|message.charCodeAt(m++);
 }else{
 left=(message.charCodeAt(m++)<<24)|(message.charCodeAt(m++)<<16)|(message.charCodeAt(m++)<<8)|message.charCodeAt(m++);
 right=(message.charCodeAt(m++)<<24)|(message.charCodeAt(m++)<<16)|(message.charCodeAt(m++)<<8)|message.charCodeAt(m++);
 }
 //for Cipher Block Chaining mode,xor the message with the previous result
 if(mode==1){if(encrypt){left^=cbcleft;right^=cbcright;}else{cbcleft2=cbcleft;cbcright2=cbcright;cbcleft=left;cbcright=right;}}

 //first each 64 but chunk of the message must be permuted according to IP
 temp=((left>>>4)^right)&0x0f0f0f0f;right^=temp;left^=(temp<<4);
 temp=((left>>>16)^right)&0x0000ffff;right^=temp;left^=(temp<<16);
 temp=((right>>>2)^left)&0x33333333;left^=temp;right^=(temp<<2);
 temp=((right>>>8)^left)&0x00ff00ff;left^=temp;right^=(temp<<8);
 temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);

 left=((left<<1)|(left>>>31));
 right=((right<<1)|(right>>>31));

 //do this either 1 or 3 times for each chunk of the message
 for(j=0;j<iterations;j+=3){
 endloop=looping[j+1];
 loopinc=looping[j+2];
 //now go through and perform the encryption or decryption
 for(i=looping[j];i!=endloop;i+=loopinc){//for efficiency
 right1=right^keys[i];
 right2=((right>>>4)|(right<<28))^keys[i+1];
 //the result is attained by passing these bytes through the S selection functions
 temp=left;
 left=right;
 right=temp^(spfunction2[(right1>>>24)&0x3f]|spfunction4[(right1>>>16)&0x3f]|spfunction6[(right1>>>8)&0x3f]|spfunction8[right1&0x3f]|spfunction1[(right2>>>24)&0x3f]|spfunction3[(right2>>>16)&0x3f]|spfunction5[(right2>>>8)&0x3f]|spfunction7[right2&0x3f]);
 }
 temp=left;left=right;right=temp;//unreverse left and right
 }//for either 1 or 3 iterations

 //move then each one bit to the right
 left=((left>>>1)|(left<<31));
 right=((right>>>1)|(right<<31));

 //now perform IP-1,which is IP in the opposite direction
 temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);
 temp=((right>>>8)^left)&0x00ff00ff;left^=temp;right^=(temp<<8);
 temp=((right>>>2)^left)&0x33333333;left^=temp;right^=(temp<<2);
 temp=((left>>>16)^right)&0x0000ffff;right^=temp;left^=(temp<<16);
 temp=((left>>>4)^right)&0x0f0f0f0f;right^=temp;left^=(temp<<4);

 //for Cipher Block Chaining mode,xor the message with the previous result
 if(mode==1){if(encrypt){cbcleft=left;cbcright=right;}else{left^=cbcleft2;right^=cbcright2;}}
 if(encrypt){tempresult+=String.fromCharCode((left>>>24),((left>>>16)&0xff),((left>>>8)&0xff),(left&0xff),(right>>>24),((right>>>16)&0xff),((right>>>8)&0xff),(right&0xff));}
 else{tempresult+=String.fromCharCode(((left>>>16)&0xffff), (left&0xffff),((right>>>16)&0xffff), (right&0xffff));}/*解密时输出双字节*/
 encrypt?chunk+=16:chunk+=8;
 if(chunk==512){result+=tempresult;tempresult="";chunk=0;}
 }//for every 8 characters,or 64 bits in the message

 //return the result as an array
 return result+tempresult;
}//end of des

//des_createKeys
//this takes as input a 64 bit key(even though only 56 bits are used)
//as an array of 2 integers,and returns 16 48 bit keys
function des_createKeys(key){
 //declaring this locally speeds things up a bit
 pc2bytes0=new Array(0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204);
 pc2bytes1=new Array(0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101);
 pc2bytes2=new Array(0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808);
 pc2bytes3=new Array(0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000);
 pc2bytes4=new Array(0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010);
 pc2bytes5=new Array(0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420);
 pc2bytes6=new Array(0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002);
 pc2bytes7=new Array(0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800);
 pc2bytes8=new Array(0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002);
 pc2bytes9=new Array(0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408);
 pc2bytes10=new Array(0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020);
 pc2bytes11=new Array(0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200);
 pc2bytes12=new Array(0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010);
 pc2bytes13=new Array(0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105);

 //how many iterations(1 for des,3 for triple des)
 var iterations=key.length>=24?3 :1;
 //stores the return keys
 var keys=new Array(32 * iterations);
 //now define the left shifts which need to be done
 var shifts=new Array(0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0);
 //other variables
 var lefttemp,righttemp,m=0,n=0,temp;

 for(var j=0;j<iterations;j++){//either 1 or 3 iterations
 left=(key.charCodeAt(m++)<<24)|(key.charCodeAt(m++)<<16)|(key.charCodeAt(m++)<<8)|key.charCodeAt(m++);
 right=(key.charCodeAt(m++)<<24)|(key.charCodeAt(m++)<<16)|(key.charCodeAt(m++)<<8)|key.charCodeAt(m++);

 temp=((left>>>4)^right)&0x0f0f0f0f;right^=temp;left^=(temp<<4);
 temp=((right>>>-16)^left)&0x0000ffff;left^=temp;right^=(temp<<-16);
 temp=((left>>>2)^right)&0x33333333;right^=temp;left^=(temp<<2);
 temp=((right>>>-16)^left)&0x0000ffff;left^=temp;right^=(temp<<-16);
 temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);
 temp=((right>>>8)^left)&0x00ff00ff;left^=temp;right^=(temp<<8);
 temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);

 //the right side needs to be shifted and to get the last four bits of the left side
 temp=(left<<8)|((right>>>20)&0x000000f0);
 //left needs to be put upside down
 left=(right<<24)|((right<<8)&0xff0000)|((right>>>8)&0xff00)|((right>>>24)&0xf0);
 right=temp;

 //now go through and perform these shifts on the left and right keys
 for(i=0;i<shifts.length;i++){
 //shift the keys either one or two bits to the left
 if(shifts[i]){left=(left<<2)|(left>>>26);right=(right<<2)|(right>>>26);}
 else{left=(left<<1)|(left>>>27);right=(right<<1)|(right>>>27);}
 left&=-0xf;right&=-0xf;

 //now apply PC-2,in such a way that E is easier when encrypting or decrypting
 //this conversion will look like PC-2 except only the last 6 bits of each byte are used
 //rather than 48 consecutive bits and the order of lines will be according to
 //how the S selection functions will be applied:S2,S4,S6,S8,S1,S3,S5,S7
 lefttemp=pc2bytes0[left>>>28]|pc2bytes1[(left>>>24)&0xf]
|pc2bytes2[(left>>>20)&0xf]|pc2bytes3[(left>>>16)&0xf]
|pc2bytes4[(left>>>12)&0xf]|pc2bytes5[(left>>>8)&0xf]
|pc2bytes6[(left>>>4)&0xf];
 righttemp=pc2bytes7[right>>>28]|pc2bytes8[(right>>>24)&0xf]
|pc2bytes9[(right>>>20)&0xf]|pc2bytes10[(right>>>16)&0xf]
|pc2bytes11[(right>>>12)&0xf]|pc2bytes12[(right>>>8)&0xf]
|pc2bytes13[(right>>>4)&0xf];
 temp=((righttemp>>>16)^lefttemp)&0x0000ffff;
 keys[n++]=lefttemp^temp;keys[n++]=righttemp^(temp<<16);
 }
 }//for each iterations
 //return the keys we"ve created
 return keys;
}//end of des_createKeys

////////////////////////////// 测试 //////////////////////////////

function stringToHex(s){
 var r="";
 var hexes=new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
 for(var i=0;i<(s.length);i++){r+=hexes[s.charCodeAt(i)>>4]+hexes[s.charCodeAt(i)&0xf];}
 return r;
}
function HexTostring(s){
 var r="";
 for(var i=0;i<s.length;i+=2){
 var sxx=parseInt(s.substring(i,i+2),16);
 r+=String.fromCharCode(sxx);}
 return r;
}

function needkey(){
if(document.encme.deskey.value==""){var tk=window.prompt("请设定密码!","");if(tk){document.encme.deskey.value=tk; return true;}else return false;}
else return true;
}

function encMe(){
if(!needkey())return;
var k=document.encme.deskey.value;
var s=document.encme.inps.value;
document.encme.outs.value=stringToHex(des(k,s,1,0));
}

function uncMe(){
if(!needkey())return;
var s=document.encme.outs.value;
document.encme.inps.value=des(document.encme.deskey.value,HexTostring(s),0,0);
}

//-->
</script>
<style type="text/css">
</style>
</head>
<body>
<table border=0>
<form name="encme">
<tr><td>原
<br />
文</td><td>
<textarea name="inps" cols="50" rows="10"></textarea>
</td></tr>
<tr><td>
密
<br />
码</td><td>
<input type="password" name="deskey" maxlength="100" size="16" value="">
<input type="button" onclick="javascript:encMe()" value="&#8595;加密&#8595;">
<input type="button" onclick="javascript:uncMe()" value="&#8593;解密&#8593;">
</td></tr>
<tr><td>
密
<br />
文</td><td>
<textarea name="outs" cols="50" rows="10"></textarea>
</td></tr>
</form>
</table>
</body>
</html>

 

 

0
0
分享到:
评论

相关推荐

    DES加密 java与.net可以相互加密解密两种方法

    在Java和.NET平台上,我们可以使用内置的加密库实现DES加密和解密。本文将详细介绍如何在Java和.NET中实现互操作的DES加密解密,并提供两种不同的方法。 **方法一:使用标准库** 1. **Java实现**: - 引用`javax....

    ASP.NET实现 DES加密解密类

    【ASP.NET实现DES加密解密类】的知识点详解 在ASP.NET中,有时我们需要对敏感数据进行加密处理,以确保其在传输或存储时的安全性。DES(Data Encryption Standard)是一种常用的对称加密算法,适合对小量数据进行...

    两套java与.net交互数据加密解密代码

    在这个场景下,"两套java与.net交互数据加密解密代码" 提供了一种安全的数据传输解决方案,确保了信息在不同系统间传递时的安全性。这里主要涉及了两个关键的技术:DES3(Triple DES)加密算法以及CBC(Cipher Block...

    Asp.net 2.0 DES加密解密

    提供Asp.net 2.0文件和字符串的DES算法加密解密. 在项目的Bin文件夹里引用这个文件就可以了,或者直接考到Bin目录里。。专门给初学者用的呵呵。。

    .NET DES加解密的公共类程序

    .NET DES加解密的公共类程序,这可是我自己写的哦,超级好用的东东

    DES加密解密算法(vb.net)

    vb.net实现des加密解密算法,winform程序,基于.net framework 2.0开发

    des加密解密JAVA与.NET互通实例

    下面将详细解释DES加密解密的基本原理、Java实现以及如何在Java与.NET之间进行互通。 1. DES加密算法概述: DES是一种块密码,使用56位的密钥对64位的数据块进行加密。它通过一系列的置换和混淆操作(称为轮函数)...

    java和javascript之间的DES加密解密

    4. **Java与JavaScript之间的互操作**: 由于Java和JavaScript环境的不同,直接使用代码可能无法直接互换。为了使两者之间能够兼容,我们需要确保使用相同的密钥和填充方式。同时,由于JavaScript可能无法处理字节...

    java与delphi 通用 DES加解密(源代码)

    本文将深入探讨Java和Delphi中实现DES加解密的源代码,以及如何实现这两个不同平台之间的互操作性。 DES算法是在1970年代由IBM开发,后来被美国国家标准局(NIST)采纳为标准。它基于64位的数据块进行操作,使用56...

    java和C#通用des3加解密

    标题中的“java和C#通用des3加解密”指的是在Java和C#这两种不同的编程语言中实现DES(Data Encryption Standard)的加强版——3DES(Triple DES)加解密算法。3DES是一种广泛使用的对称加密算法,通过三次应用DES的...

    JAVA实现DES加解密系统

    **JAVA实现DES加解密系统** DES(Data Encryption Standard)是一种经典的对称加密算法,它在信息安全领域有着广泛的应用。本文将深入探讨如何使用Java语言实现DES加解密系统,以及涉及的相关技术点。 首先,DES...

    Java调用dll实现des加密解密

    总的来说,通过Java调用DLL实现DES加密解密涉及了Java和C/C++的混合编程,需要理解JNI的工作原理,以及如何在C/C++中与DLL交互。这个过程对于那些希望在Java中利用已有的本地库功能的开发者来说是至关重要的。

    des加密解密java实现版本

    在Java中实现DES加密和解密是一项常见的任务,通常涉及到`javax.crypto`包中的类和接口。本篇文章将详细阐述DES加密解密的Java实现过程。 首先,我们需要了解DES算法的基本原理。DES是一种块密码,它将明文数据分成...

    DES加密解密java工具类

    DES加密解密java工具类 public static final String KEY_ALGORITHM = "DES"; public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5PADDING"; validKeySpecException, NoSuchPaddingException, ...

    java的DES加密解密

    java的DES加密解密:用于密码在数据库的加密,解码类

    C语言实现DES加密解密算法

    DES加密解密算法的C语言实现 DES(Data Encryption Standard)是一种对称密钥 BLOCK 加密算法,使用 56 ...DES 加密解密算法的 C 语言实现需要理解字节与二进制转换、密钥处理、加密和解密操作以及基本操作等知识点。

    3DES加密解密工具

    标题中的“3DES加密解密工具”指的是一个用于执行三重数据加密标准(3DES,Triple DES)的软件工具,这种工具通常用于保护敏感数据的安全,确保信息在传输过程中的机密性。3DES是DES(Data Encryption Standard,...

    Asp.Net/C# DES加密解密

    本资源提供DES加密解密的方法,大家可以复制该方法运用到各种项目中,简单便捷,快速使用。我这里用的是控制台给大家演示的,大家可以上传到API项目或者MVC等项目中,只需要把自己所需要的参数给提供出来就可

    Java中3DES加密解密示例(封装byte数组16进制互转)

    Java 中的 3DES 加密解密示例(封装 byte 数组和 16 进制字符串互转) 在 Java 中,3DES 加密是一种常用的加密算法,它可以将明文数据转换为密文数据,以保护数据的安全性。在本示例中,我们将展示如何使用 3DES ...

    3des加密解密base64编码解码完全与ASP.NET,JAVA,兼容的js

    本资源提供的是一套完整的JavaScript实现,能够与ASP.NET和JAVA平台的3DES加密解密及Base64编码解码功能保持兼容。 首先,3DES的工作原理是在DES的基础上增加了一次加密过程,即使用同一个密钥进行三次加密,这大大...

Global site tag (gtag.js) - Google Analytics