java des加密后的串比原字符串长
DESPlus.java
/
*** @author 李国庆
* @company leemenz (C) copyright
* @time Nov 1, :: AM
* @version 1.0.0.0
* @package com.des
*/
package com.des;
import java.security.*;
import javax.crypto.*;
public class DESPlus {
private static String strDefaultKey = "national";
private Cipher encryptCipher = null;
private Cipher decryptCipher = null;
/
*** 将byte数组转换为表示进制值的字符串, 如:byte[]{ 8,}转换为:, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的手工客 源码长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + ;
}
// 小于0F的数需要在前面补0
if (intTmp < ) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, ));
}
return sb.toString();
}
/
*** 将表示进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的印刷排版源码转换过程
*
* @param strIn
* 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:leo@.com">LiGuoQing</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, );
}
return arrOut;
}
/
*** 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public DESPlus() throws Exception {
this(strDefaultKey);
}
/
*** 指定密钥构造方法
*
* @param strKey
* 指定的密钥
* @throws Exception
*/
public DESPlus(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/
*** 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/
*** 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/
*** 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/
*** 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
/
*** 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
}
测试程序 Test.java
/
*** @author 李国庆
* @company leemenz (C) copyright
* @time Nov 1, :: AM
* @version 1.0.0.0
* @package com.des
*/
package com.des;
/
*** @author Administrator
*
*/
public class Test {
/
*** @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String test = "Hellow Word!";
//DESPlus des = new DESPlus();//默认密钥
DESPlus des = new DESPlus("leemenz");//自定义密钥
System.out.println("加密前的字符:"+test);
System.out.println("加密后的字符:"+des.encrypt(test));
System.out.println("解密后的字符:"+des.decrypt(des.encrypt(test)));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
看看 对你有帮助的 不一定加密长度一样的
JAVAå.NET使ç¨DES对称å å¯çåºå«
JAVAå.NETçç³»ç»ç±»åºéé½æå°è£ DES对称å å¯çå®ç°æ¹å¼ï¼ä½æ¯å¯¹å¤æ´é²çæ¥å£å´åä¸ç¸åï¼çè³ææ¶ä¼è®©èªå·±é¾ä»¥è§£å³å ¶ä¸çé®é¢ï¼æ¯å¦JAVAå å¯åçç»æå¨.NETä¸è§£å¯ä¸åºæ¥çï¼ç±äºæè¿é¡¹ç®æè·¨JAVAå.NETçå 解å¯ï¼ç»è¿æçåæè°è¯ï¼ç»äºè®©å®ä»¬å¯ä»¥äºç¸å å¯è§£å¯äºã
DESå å¯
DESæ¯ä¸ç§å¯¹ç§°å å¯(Data Encryption Standard)ç®æ³ï¼ä»¥åæåè¿ä¸ç¯æç« ï¼.NETä¸å å¯è§£å¯ç¸å ³ç¥è¯ï¼æè¿ç®åæè¿°ã
DESç®æ³ä¸è¬æä¸¤ä¸ªå ³é®ç¹ï¼ç¬¬ä¸ä¸ªæ¯å å¯ç®æ³ï¼ç¬¬äºä¸ªæ¯æ°æ®è¡¥ä½ã
å å¯ç®æ³å¸¸è§çæECB模å¼åCBC模å¼ï¼
ECB模å¼ï¼çµåå¯æ¬æ¹å¼ï¼è¿æ¯JAVAå°è£ çDESç®æ³çé»è®¤æ¨¡å¼ï¼å°±æ¯å°æ°æ®æç §8个åèä¸æ®µè¿è¡DESå å¯æ解å¯å¾å°ä¸æ®µ8个åèçå¯ææè ææï¼æåä¸æ®µä¸è¶³8个åèï¼å补足8个åèï¼æ³¨æï¼è¿éå°±æ¶åå°æ°æ®è¡¥ä½äºï¼è¿è¡è®¡ç®ï¼ä¹åæç §é¡ºåºå°è®¡ç®æå¾çæ°æ®è¿å¨ä¸èµ·å³å¯ï¼å段æ°æ®ä¹é´äºä¸å½±åã
CBC模å¼ï¼å¯æåç»é¾æ¥æ¹å¼ï¼è¿æ¯.NETå°è£ çDESç®æ³çé»è®¤æ¨¡å¼ï¼å®æ¯è¾éº»ç¦ï¼å å¯æ¥éª¤å¦ä¸ï¼
1ãé¦å å°æ°æ®æç §8个åèä¸ç»è¿è¡åç»å¾å°D1D2......Dnï¼è¥æ°æ®ä¸æ¯8çæ´æ°åï¼å°±æ¶åå°æ°æ®è¡¥ä½äºï¼
2ã第ä¸ç»æ°æ®D1ä¸åéIå¼æåçç»æè¿è¡DESå å¯å¾å°ç¬¬ä¸ç»å¯æC1ï¼æ³¨æï¼è¿éæåéIç说æ³ï¼ECB模å¼ä¸æ²¡æ使ç¨åéIï¼
3ã第äºç»æ°æ®D2ä¸ç¬¬ä¸ç»çå å¯ç»æC1å¼æ以åçç»æè¿è¡DESå å¯ï¼å¾å°ç¬¬äºç»å¯æC2
4ãä¹åçæ°æ®ä»¥æ¤ç±»æ¨ï¼å¾å°Cn
5ãæ顺åºè¿ä¸ºC1C2C3......Cnå³ä¸ºå å¯ç»æã
æ°æ®è¡¥ä½ä¸è¬æNoPaddingåPKCS7Padding(JAVAä¸æ¯PKCS5Padding)å¡«å æ¹å¼ï¼PKCS7PaddingåPKCS5Paddingå®é åªæ¯åè®®ä¸ä¸æ ·ï¼æ ¹æ®ç¸å ³èµæ说æï¼PKCS5Paddingæç¡®å®ä¹äºå å¯åæ¯8åèï¼PKCS7Paddingå å¯å¿«å¯ä»¥æ¯1-ä¹é´ãä½æ¯å°è£ çDESç®æ³é»è®¤é½æ¯8åèï¼æ以å¯ä»¥è®¤ä¸ºä»ä»¬ä¸æ ·ãæ°æ®è¡¥ä½å®é æ¯å¨æ°æ®ä¸æ»¡8åèçåæ°ï¼æè¡¥å å°8åèçåæ°çå¡«å è¿ç¨ã
NoPaddingå¡«å æ¹å¼ï¼ç®æ³æ¬èº«ä¸å¡«å ï¼æ¯å¦.NETçpaddingæä¾äºæNoneï¼Zerosæ¹å¼ï¼åå«ä¸ºä¸å¡«å åå¡«å 0çæ¹å¼ã
PKCS7Paddingï¼PKCS5Paddingï¼å¡«å æ¹å¼ï¼ä¸º.NETåJAVAçé»è®¤å¡«å æ¹å¼ï¼å¯¹å å¯æ°æ®åèé¿åº¦å¯¹8åä½ä¸ºrï¼å¦r大äº0ï¼åè¡¥8-r个åèï¼åè为8-rçå¼ï¼å¦ærçäº0ï¼åè¡¥8个åè8ãæ¯å¦ï¼
å å¯å符串为为AAAï¼åè¡¥ä½ä¸ºAAA;å å¯å符串为BBBBBBï¼åè¡¥ä½ä¸ºBBBBBBï¼å å¯å符串为CCCCCCCCï¼åè¡¥ä½ä¸ºCCCCCCCCã
.NETä¸çDESå å¯
对äº.NETï¼æ¡æ¶å¨System.Security.Cryptographyå½å空é´ä¸æä¾äºDESCryptoServiceProviderä½ä¸ºSystem.Security.Cryptography.DESå å¯è§£å¯çå è£ æ¥å£ï¼å®æä¾äºå¦ä¸ç4个æ¹æ³ï¼
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
public override void GenerateIV()
public override void GenerateKey()
ä».NETç±»åºå°è£ æ åµï¼å 解å¯éè¦ä¼ å ¥ä¸ä¸ªKeyåIVåéãèä¸Keyå¿ é¡»ä¸º8åèçæ°æ®ï¼å¦åä¼ç´æ¥æå¼å¸¸åºæ¥ï¼å½ä½¿ç¨ECB模å¼ä¸ï¼ä¸ç®¡ä¼ å ¥ä»ä¹IVåéï¼å å¯ç»æé½ä¸æ ·ã示ä¾ä»£ç å¦ä¸ï¼
public static string EncryptWithJava(string key, string str)
{
if (key.Length < 8 || string.IsNullOrEmpty(str))
{
throw new Exception("å å¯keyå°äº8æè å å¯å符串为空ï¼");
}
byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] bIV = IV;
byte[] bStr = Encoding.UTF8.GetBytes(str);
try
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
desc.Padding = PaddingMode.PKCS7;//è¡¥ä½
desc.Mode = CipherMode.ECB;//CipherMode.CBC
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, desc.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(bStr, 0, bStr.Length);
cStream.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
byte[] res = mStream.ToArray();
foreach (byte b in res)
{
ret.AppendFormat("{ 0:x2}", b);
}
return ret.ToString();
}
}
}
catch
{
return string.Empty;
}
}
ç±äºä¸ºECB模å¼ï¼å æ¤IVè¿é设置ä»ä¹å¼é½æ¯å¯ä»¥çï¼å½ä¸ºCBC模å¼ä¸ï¼åéè¦è®¾ç½®ä¸ºå ¶ä»å¼ï¼æ¯å¦ï¼public static byte[] IV = { 0x, 0x, 0x, 0x, 0x, 0x, 0x, 0x }ï¼æè½æ£å¸¸å å¯è§£å¯ã
JAVAä¸çDESå å¯
JAVAçjavax.crypto.Cipherå ä¸ï¼æä¾äºå å¯è§£å¯çåè½ï¼å®çéægetInstanceæ¹æ³ï¼å¯ä»¥è¿åä¸ä¸ªCipher对象ï¼ä¸è¬æpublic static final Cipher getInstance(String transformation)æ¹æ³ï¼transformation为ï¼algorithm/mode/paddingï¼åå«è¡¨ç¤ºç®æ³å称ï¼æ¯å¦DESï¼ä¹å¯ä»¥å¨åé¢å å«ç®æ³æ¨¡å¼åå¡«å æ¹å¼ï¼ä½ä¹å¯ä»¥åªæ¯ç®æ³å称ï¼å¦ä¸ºï¼"DES/CBC/PKCS5Padding"ï¼"DES"çãJAVAä¸é»è®¤çç®æ³ä¸ºECBï¼é»è®¤å¡«å æ¹å¼ä¸ºPKCS5PaddingãCipherçInitæ¹æ³ç¨æ¥åå§åå å¯å¯¹è±¡ï¼å¸¸è§çæï¼
public final void init(int opmode, Key key, AlgorithmParameterSpec params) ï¼
public final void init(int opmode,Key key, SecureRandom random)ï¼ç¨SecureRandomæ¶ï¼ä¸è¬ç¨äºä¸éè¦IVçç®æ³æ¨¡å¼ï¼ç¤ºä¾ä»£ç å¦ä¸ï¼
public static String encrypt2(String src) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec ks = new DESKeySpec(KEY.getBytes("UTF-8"));
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding");//Cipher.getInstance("DES");
IvParameterSpec iv2 = new IvParameterSpec(IV);
cip.init(Cipher.ENCRYPT_MODE, sk, iv2);//IVçæ¹å¼
//cip.init(Cipher.ENCRYPT_MODE, sk, sr);//没æä¼ éIV
String dest = byteToHex(cip.doFinal(src.getBytes("UTF-8")));
return dest;
}
å½é»è®¤ç¨DESï¼JAVAä¼ç¨ECB模å¼ï¼å æ¤è¿éIVåé没æä½ç¨ï¼è¿éï¼ä½å½ç¨CBC模å¼ä¸ï¼å¦æè¿æ¯ç¨SecureRandomï¼åæ¯æ¬¡å å¯çç»æé½ä¼ä¸ä¸æ ·ï¼å 为JAVAå é¨ä¼ç¨éæºçIVæ¥åå§åCipher对象ï¼å¦ç¤ºä¾ä»£ç ï¼ç±äºCipher.getInstance("DES/CBC/PKCS5Padding")使ç¨äºCBCï¼å æ¤æè¿éç¨çjavax.crypto.spec.IvParameterSpecå ä¸çIvParameterSpecæ¥åå§ååéIVï¼
Private final static byte[] IV = new byte[] { 0x, 0x, 0x, 0x, 0x, 0x, 0x, 0x};
æ»ç»
对äº.NETåJAVAå¨ä½¿ç¨DES对称å å¯æ¶ï¼éè¦å¤§å®¶æå®ä¸æ ·çç®æ³åå¡«å 模å¼ï¼å¹¶ä¸JAVAå¨åDESå 解å¯ç®æ³æ¶ï¼è¿éè¦æ ¹æ®å建Cipher对象çä¸åï¼æ£ç¡®ä½¿ç¨IVåéãå¨ä¸åç³»ç»éè¦äºç¸æ°æ®æ¶ï¼å¿ é¡»è¦æç¡®çæ¯å å¯ç®æ³ï¼Keyåç®æ³æ¨¡å¼ï¼åæ ¹æ®ä¸å模å¼æ¯å¦éè¦IVåéï¼æåæ¯å¡«å 模å¼ã
æ¬ææ¯ç»è¿èªå·±ç¿»é èµæååå¤è°è¯ä»£ç èåºæ¥çï¼å¦æé®é¢ï¼è¯·ææ£ã
ç¨javaå®ç°deså å¯å解å¯
ä¸ä¸ªç¨DESæ¥å å¯ã解å¯çç±»
/java/1/.html
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* åç¬¦ä¸²å·¥å ·éå
* @author Liudong
*/
public class StringUtils {
private static final String PASSWORD_CRYPT_KEY = "__jDlog_";
private final static String DES = "DES";
/**
* å å¯
* @param src æ°æ®æº
* @param key å¯é¥ï¼é¿åº¦å¿ é¡»æ¯8çåæ°
* @return è¿åå å¯åçæ°æ®
* @throws Exception
*/
public static byte[] encrypt(byte[] src, byte[] key)throws Exception {
//DESç®æ³è¦æ±æä¸ä¸ªå¯ä¿¡ä»»çéæºæ°æº
SecureRandom sr = new SecureRandom();
// ä»åå§å¯åæ°æ®å建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// å建ä¸ä¸ªå¯åå·¥åï¼ç¶åç¨å®æDESKeySpec转æ¢æ
// ä¸ä¸ªSecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象å®é å®æå å¯æä½
Cipher cipher = Cipher.getInstance(DES);
// ç¨å¯ååå§åCipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// ç°å¨ï¼è·åæ°æ®å¹¶å å¯
// æ£å¼æ§è¡å å¯æä½
return cipher.doFinal(src);
}
/**
* 解å¯
* @param src æ°æ®æº
* @param key å¯é¥ï¼é¿åº¦å¿ é¡»æ¯8çåæ°
* @return è¿å解å¯åçåå§æ°æ®
* @throws Exception
*/
public static byte[] decrypt(byte[] src, byte[] key)throws Exception {
// DESç®æ³è¦æ±æä¸ä¸ªå¯ä¿¡ä»»çéæºæ°æº
SecureRandom sr = new SecureRandom();
// ä»åå§å¯åæ°æ®å建ä¸ä¸ªDESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// å建ä¸ä¸ªå¯åå·¥åï¼ç¶åç¨å®æDESKeySpec对象转æ¢æ
// ä¸ä¸ªSecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象å®é å®æ解å¯æä½
Cipher cipher = Cipher.getInstance(DES);
// ç¨å¯ååå§åCipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
// ç°å¨ï¼è·åæ°æ®å¹¶è§£å¯
// æ£å¼æ§è¡è§£å¯æä½
return cipher.doFinal(src);
}
/**
* å¯ç 解å¯
* @param data
* @return
* @throws Exception
*/
public final static String decrypt(String data){
try {
return new String(decrypt(hex2byte(data.getBytes()),
PASSWORD_CRYPT_KEY.getBytes()));
}catch(Exception e) {
}
return null;
}
/**
* å¯ç å å¯
* @param password
* @return
* @throws Exception
*/
public final static String encrypt(String password){
try {
return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes())); }catch(Exception e) {
}
return null;
}
æ¯è¾é¿, 转äºä¸é¨å.
Java 接口数据加密和解密
提供一个基于注解实现接口加密解密工具源码,旨在方便在软件项目中对数据进行加密与解密。该工具支持多种加密方式,源码资本实力包括Base、DES、3DES、AES与RSA,bmob源码下载以及MD5加密。
使用方法:只需在需要加密解密的接口上添加相应的注解即可实现功能。
此加密解密组件仅适用于SpringBoot项目。
步骤如下:
1. 从gitee.com/zhao_jian_jun...拉取代码至本地。积分源码 模板
2. 使用meavn的install将项目打包为.jar文件。
3. 将加解密依赖引入至项目中。
4. 在配置文件中说明使用的加密方式的秘钥。RSA为非对称加密,需提供两个秘钥。变量名如下:
5. 对请求相应结果加密,使用@ZjjEncryptResponse注解并指定加密方式。
6. 前端接收到的为加密后的数据。
7. 对请求参数进行解密,使用@ZjjDecryptRequest注解。
2024-12-24 01:15
2024-12-24 01:10
2024-12-24 00:37
2024-12-24 00:27
2024-12-24 00:02