DES 加密:ECB+PKCS5Padding

1 篇文章 0 订阅
1 篇文章 0 订阅

需求:采用 DES 对数据加密,DES 加密模式为ECB,数据补位的填充方式为PKCS5Padding

实现:代码如下(不用导入额外的jar包,jdk自带的即可,本案例使用jdk7)

import java.security.Key;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


import org.apache.commons.codec.binary.Base64;

public class testENC {
		/*** 
	     * 加密数据 
	     * @param encryptString  要加密的数据
	     * @param encryptKey  秘钥
	     * @return 
	     * @throws Exception 
	     */  
	    public static String encryptDES(String encryptString, String encryptKey) throws Exception {  
         
	        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");  
	        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(getKey(encryptKey), "DES"));  
	        byte[] encryptedData = cipher.doFinal(encryptString.getBytes("GBK"));  
	        return Base64.encodeBase64String(encryptedData);
	    } 
		/*** 
	     * 解密数据 
	     * @param decryptString  要解密的数据
	     * @param decryptKey  秘钥
	     * @return 
	     * @throws Exception 
	     */  
	  
	    public static String decryptDES(String decryptString, String decryptKey) throws Exception {  
	        
	        byte[] sourceBytes = Base64.decodeBase64(decryptString);    
	        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");   
	        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(getKey(decryptKey), "DES"));    
	        byte[] decoded = cipher.doFinal(sourceBytes);    
	        return new String(decoded, "GBK");  
	        
	    } 
	  
	    /** 
	     * key  不足8位补位
	     * @param string  
	     */  
	    public static byte[] getKey(String keyRule) {  
	        Key key = null;  
	        byte[] keyByte = keyRule.getBytes();  
	        // 创建一个空的八位数组,默认情况下为0  
	        byte[] byteTemp = new byte[8];  
	        // 将用户指定的规则转换成八位数组  
	        for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {  
	            byteTemp[i] = keyByte[i];  
	        }  
	        key = new SecretKeySpec(byteTemp, "DES");  
	        return key.getEncoded();  
	    }  
	      
	    
	    /** 
	     * 测试类
	     *   
	     */ 
	    public static void main(String[] args) throws Exception {  
	        String clearText = "|01|03|01|817777777||0|2017116371|"; 
	        String key = "12345678";//密钥
	        System.out.println("明文:"+clearText+"\n密钥:"+key);  
	        String encryptText = encryptDES(clearText, key);  
	        System.out.println("加密后:"+encryptText);  
	        String decryptText = decryptDES(encryptText, key);  
	        System.out.println("解密后:"+decryptText);  
	    }  

}

参考:java中DES加密(DES/ECB/pkcs5padding)的代码分享_ssylml的博客-CSDN博客_des/ecb/pkcs5padding

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Delphi 7中,可以使用TIdCoderMIME组件来实现DES加密,并且满足要求的DES加密模式ECB(Electronic Codebook)和PKCS5Padding(由于DES本身没有内置PKCS5Padding,我们需要手动实现)。最后,我们可以使用TIdEncoderMIME组件将加密后的结果进行Base64编码输出。 首先,我们需要在Delphi 7中创建一个新的控制台应用程序。然后,我们需要导入两个组件:TIdCoderMIME和TIdEncoderMIME。您可以在Delphi 7的工具选项卡中,点击"组件",选择"导入类型库",然后选择"Indy MIME Encoding"。 在导入后,您可以在代码中使用这两个组件。以下是一个示例程序: ```Delphi uses SysUtils, IdCoderMIME, IdGlobal; function PKCS5Padding(const Data: TBytes; const BlockSize: Integer): TBytes; var PaddingSize, I: Integer; begin PaddingSize := BlockSize - Length(Data) mod BlockSize; SetLength(Result, Length(Data) + PaddingSize); for I := 0 to Pred(PaddingSize) do Result[Length(Data) + I] := PaddingSize; Move(Data[0], Result[0], Length(Data)); end; function EncryptDES(const Input, Key: AnsiString): AnsiString; var Encoder: TIdEncoderMIME; Coder: TIdEncoder3to4; EncryptedData, EncodedData: TBytes; begin Encoder := TIdEncoderMIME.Create(nil); Coder := TIdEncoder3to4.Create(nil); try EncryptedData := PKCS5Padding(BytesOf(Input), 8); // 设置BlockSize为8,即DES加密的块大小 // 使用TIdCoderMIME组件进行DES加密 Coder.CodeBytes(EncryptedData, EncryptedData, Length(EncryptedData)); // 使用TIdEncoderMIME组件进行Base64编码 EncodedData := Encoder.EncodeBytes(EncryptedData); Result := StringOf(EncodedData); finally Encoder.Free; Coder.Free; end; end; var Input, Key, EncryptedData: AnsiString; begin try Input := 'Hello World'; // 要加密的字符串 Key := 'MyKey123'; // 加密密钥 EncryptedData := EncryptDES(Input, Key); Writeln('Encrypted Data: ' + EncryptedData); except on E: Exception do Writeln('Exception: ' + E.Message); end; Readln; end. ``` 请注意,这只是一个简单的示例程序,用于演示如何使用Delphi 7中的TIdCoderMIME和TIdEncoderMIME组件进行DES加密。在实际应用中,您可能需要添加更多的错误处理和输入验证来增强代码的健壮性和安全性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值