java JS DES互相加密解密 通用!!!

加完后 可以到 http://tool.chacuo.net/cryptdes  在线DES加密 解密 进行测试

 

如果 出现 异常 javax.crypto.IllegalBlockSizeException  

那就看我博客园的 另外一篇  解决这个异常

 帮客户整了1天 试了N个JS 的方法 终于找我弄出来了!

 

java部分:

package com.util;

import java.security.Key;

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

        // 算法名称/加密模式/填充方式   
        public static final String CIPHER_ALGORITHM_ECB = "DES/ECB/ZeroBytePadding";  

    //主要看填充方式 PKCS5Padding  PKCS7Padding  还有其他的填充方式没用过

     //    public static final String CIPHER_ALGORITHM_CBC = "DES/CBC/ZerosPadding";   
           
        public static void main(String[] args) throws Exception {   
            /* 
             * 使用 ECB mode 
             * 密钥生成器 生成密钥 
             * ECB mode cannot use IV 
             */   
            byte[] key = "DAF3100DCD39CAEB5DD35E9651712A86".getBytes();    
            byte[] encrypt = encrypt("1212121211221",key);
            String encodeBase64String = Base64.encodeBase64String(encrypt);
            System.out.println(encodeBase64String);
            System.out.println(new String(decrypt(encrypt, key)));                   
         
        }   
           
        static byte[] getIV() {   
            String iv = "asdfivh7"; //IV length: must be 8 bytes long   
            return iv.getBytes();   
        }   
       
        /** 
         * 生成密钥 
         *  
         * @return 
         * @throws Exception 
         */   
        private static byte[] generateKey() throws Exception {   
            KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);   
            keyGenerator.init(56); //des 必须是56, 此初始方法不必须调用   
            SecretKey secretKey = keyGenerator.generateKey();   
            return secretKey.getEncoded();   
        }   
       
        /** 
         * 还原密钥 
         *  
         * @param key 
         * @return 
         * @throws Exception  
         */   
        private static Key toKey(byte[] key) throws Exception {   
            DESKeySpec des = new DESKeySpec(key);   
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);   
            SecretKey secretKey = keyFactory.generateSecret(des);   
            return secretKey;   
        }   
           
        /** 
         * 加密  
         * @param data 原文 
         * @param key 
         * @return 密文 
         * @throws Exception 
         */   
        public static byte[] encrypt(byte[] data, byte[] key) throws Exception {   
            Key k = toKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);   
            cipher.init(Cipher.ENCRYPT_MODE, k);   
            byte[] doFinal = cipher.doFinal(data);
            return cipher.doFinal(data);   
        }   
        /** 
         * 解密 
         * @param data 密文 
         * @param key 
         * @return 明文、原文 
         * @throws Exception 
         */   
        public static byte[] decrypt(byte[] data, byte[] key) throws Exception {   
            Key k = toKey(key);   
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);   
            cipher.init(Cipher.DECRYPT_MODE, k);   
            return cipher.doFinal(data);   
        }   
    }   

 

 Html部分

<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<title>JS设置DES加密处理</title>
		<script type="text/javascript" src="C:\Users\Administrator\Desktop\js\jquery.min.js"></script>
		<script src="C:\Users\Administrator\Desktop\js\rollups\tripledes.js"></script>
		<script src="C:\Users\Administrator\Desktop\js\components\mode-ecb.js"></script>
		<script>
			 //DES 解密 加密
			function encryptByDES(message, key) {
					var keyHex = CryptoJS.enc.Utf8.parse(key);
					var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
						mode: CryptoJS.mode.ECB,
						padding: CryptoJS.pad.Zero
					});
					return encrypted.toString();
				}
				//DES 解密
			function decryptByDES(ciphertext, key) {
				var keyHex = CryptoJS.enc.Utf8.parse(key);
				// direct decrypt ciphertext
				var decrypted = CryptoJS.DES.decrypt({
					ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
				}, keyHex, {
					mode: CryptoJS.mode.ECB,
				//这一步 是来填写 加密时候填充方式	padding: CryptoJS.pad.Pkcs7
				});
				return decrypted.toString(CryptoJS.enc.Utf8);
			}
		</script>
		<script>
			 //加密
			function encryptStr() {
					var strKey = $.trim($('#key').val());
					var strMsg = $.trim($('#text1').val());
					$('#text2').val(encryptByDES(strMsg, strKey));
				}
				//解密
			function decryptStr() {
				var strKey = $.trim($('#key').val());
				var ciphertext = $.trim($('#text2').val());
				$('#text3').val(decryptByDES(ciphertext, strKey));
			}
		</script>
	</head>
	<body>
		<h1>JS设置DES加密处理</h1>
		<label>key</label>
		<input type="text" value='12345678' id="key" />
		<div>
			<textarea id="text1" placeholder="请输入需要加密的字符">abcde12345这个中文!@#!@$#%$#^%(":''")[]=_-</textarea>
			<input type="button" value="加密" onclick="encryptStr();" />
			<textarea id="text2"></textarea>
			<input type="button" value="解密" onclick="decryptStr();" />
			<textarea id="text3"></textarea>
		</div>
	</body>
</html>

 http://files.cnblogs.com/files/liuJava/DESjs%E5%8A%A0%E5%AF%86%E5%92%8CJava%E4%BA%92%E9%80%9A.zip 所有的包 和 html例子

posted @ 2016-01-25 10:50 Lars 阅读(...) 评论(...) 编辑 收藏