`
clasp
  • 浏览: 54066 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

RSA DES加密 在使用delphi密匙文件时会有错

阅读更多

项目中需要与delphi交换。为了增强安全性这两天学习了下几种算法今晚把这两天的心得及代码贴上来交流下:

我们的想法是这样的:

delphi生成密匙文件。java使用密匙文件加密或解密。delphi也使用同一个密匙文件加密或解密。我做了两种加密方式的实现rsa跟des。我做完后发现两个方式使用java生成的密匙文件文件加密解密都是没问题的,但使用delphi生成的密匙文件就不能加密或解密。下面我把我的代码贴上来请指点下。

一。加密接口

import java.io.IOException;

public interface CodeBean   {
	/**
	 * 编码
	 * @return
	 */
	public byte[] encode(byte []  cipherText)throws Exception;
	/**
	 * 解码
	 * @return
	 */
	public byte[] decode(byte [] cipherText)throws Exception;
}

 

二。Rsa加密实现

public class RsaEncoder implements InitializingBean, CodeBean {

	private final String KEY_ENCODE = "ENCODE";// 编码

	private final String KEY_DECODE = "DECODE";// 解码
	

	private String chartSet = "UTF-8";

	private String publickey;// ENCODE/DECODE

	private String privatekey;// ENCODE/DECODE

	private String publicKeyPath = null;

	private String privatekeypath = null;

	public void afterPropertiesSet() throws Exception {
		if (!StringUtils.hasText(this.getPrivatekey()) && !StringUtils.hasText(this.getPublickey()))
			throw new BeanInitializationException("请设置publickey或privatekey值");
		if (StringUtils.hasText(this.getPublickey()))
			Assert.notNull(publicKeyPath);
		if (StringUtils.hasText(this.getPrivatekey()))
			Assert.notNull(privatekeypath);
	}

	/**
	 * 解码
	 * 
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws NoSuchPaddingException
	 * @throws InvalidKeyException
	 * @throws InvalidKeySpecException
	 */
	public byte[] decode(byte [] cipherText) throws Exception {		
		Cipher cipher = null;

		if (KEY_DECODE.equals(this.getPublickey())) {
			
			PublicKey pubKey = (PublicKey)loadKey(this.getPublicKeyPath(),1);
			cipher = Cipher.getInstance("RSA",new BouncyCastleProvider()); 
			cipher.init(Cipher.DECRYPT_MODE, pubKey);
		} else {
			
			PrivateKey privKey = (PrivateKey)loadKey(this.getPrivatekeypath(),0);
			cipher = Cipher.getInstance("RSA",new BouncyCastleProvider()); 
			cipher.init(Cipher.ENCRYPT_MODE, privKey);
		}

		byte[] newPlainText = cipher.doFinal(cipherText);
		
		return newPlainText;
	}
	/**
	 * 加密
	 * @param cipherText
	 * @return
	 * @throws IOException
	 */
	public byte[] encode(byte []  cipherText)  throws Exception {
		Cipher cipher = null;
		KeyFactory keyFactory = KeyFactory.getInstance("RSA",new BouncyCastleProvider()); 
		if (KEY_ENCODE.equals(this.getPublickey())) {		
			
			PublicKey pubKey = (PublicKey)loadKey(this.getPublicKeyPath(),1);
			//cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			 cipher = Cipher.getInstance("RSA",new BouncyCastleProvider()); 
			cipher.init(Cipher.ENCRYPT_MODE, pubKey);
		} else {
			
			
			PrivateKey privKey = (PrivateKey)loadKey(this.getPrivatekeypath(),0);
			//cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			 cipher = Cipher.getInstance("RSA",new BouncyCastleProvider()); 
			cipher.init(Cipher.ENCRYPT_MODE, privKey);
		}
		byte[] newPlainText = cipher.doFinal(cipherText);

		return newPlainText;
	}
	
	/**
	 * @param filename
	 * @param type:
	 *            1-public 0-private
	 * @return
	 * @throws ConfigurationException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public Key loadKey(String filename, int type) throws Exception {
		System.out.println("filename:" + filename);
		PropertiesConfiguration config = new PropertiesConfiguration(filename);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA", new BouncyCastleProvider());		
		File file = new File(filename);
		if (filename == null || filename.equals("")) {
			throw new NullPointerException("无效的文件路径");
		}
		long len = file.length();
		
		byte[] bytes = new byte[(int) len];
		BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(file));
		int r = bufferedInputStream.read(bytes);
		if (r != len)
			throw new IOException("读取文件不正确");
		bufferedInputStream.close();

		if (type == 0) {
			// privateKey
		
			PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(bytes);
			PrivateKey privateKey = keyFactory.generatePrivate(priPKCS8);
			return privateKey;
		} else {
			// publicKey
		
			X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bytes);
			PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);
			return publicKey;
		}
	}
	public String getPrivatekeypath() {
		return privatekeypath;
	}

	public void setPrivatekeypath(String privatekeypath) {
		this.privatekeypath = privatekeypath;
	}

	public String getPublicKeyPath() {
		return publicKeyPath;
	}

	public void setPublicKeyPath(String publicKeyPath) {
		this.publicKeyPath = publicKeyPath;
	}

	public String getPrivatekey() {
		return privatekey;
	}

	public void setPrivatekey(String privatekey) {
		this.privatekey = privatekey.toUpperCase();
		
	}

	public String getPublickey() {
		return publickey;
	}

	public void setPublickey(String publickey) {
		this.publickey = publickey.toUpperCase();
	
	}


	public String getChartSet() {
		return chartSet;
	}

	public void setChartSet(String chartSet) {
		this.chartSet = chartSet;
	}

	public String getKEY_ENCODE() {
		return KEY_ENCODE;
	}

	public String getKEY_DECODE() {
		return KEY_DECODE;
	}


}

 Rsa加密解密的测试

	public static void main(String[]arg){

	
		byte[] byt = null;
		try {
			String encryptText = "ouxueying";
			System.out.println("第一种方式..........................");
			RsaEncoder rsaEncoder1 = new RsaEncoder();
			rsaEncoder1.setChartSet("utf-8");
			rsaEncoder1.setPrivatekey("ENCODE");//DECODE
			rsaEncoder1.setPrivatekeypath("F:\\PrivateKey.key");
			rsaEncoder1.setPublickey("DECODE");//ENCODE
			rsaEncoder1.setPublicKeyPath("F:\\PublicKey.key");			
			System.out.println("加密:..........." );
			byte [] byt1 = rsaEncoder1.encode(encryptText.getBytes());
			//System.out.println("加密结果:" +  toHexString(byt));
			System.out.println("解密:..........." );
			byte[] b1 = rsaEncoder1.decode(byt1);
			
			System.out.println("解密结果: " + new String(b1));			
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

三。des加密解密的实现

public class DesEncoder implements CodeBean {
	Cipher ecipher;

	Cipher dcipher;
	//密匙路径
	private String keyPath = null;

	public static void main(String[] arg) {

		String str = "123";
		try {
			DesEncoder des = new DesEncoder();
			des.setKeyPath("F:\\desKey.key");
			//SecretKey se = des.generateKey();
			//des.saveKey(des.getKeyPath(),se);
			//System.out.println("MD5 KEY完成");
			des.init(des.loadKey(des.getKeyPath()));
			byte[] utf8 = str.getBytes();			
			byte[] enc = des.encode(utf8);
			String s = Converts.bytesToHexString(enc);
			System.out.println("加密结果:" + s);
			//byte[] enc =   "02BB6C".getBytes();
			byte[] utf = des.decode(enc);
			System.out.println("结果:" + new String(utf,"UTF-8"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 初始化加密器
	 * @param key
	 */
	public void init(SecretKey key) {
		try {
			ecipher = Cipher.getInstance("DES");
			dcipher = Cipher.getInstance("DES");
			ecipher.init(Cipher.ENCRYPT_MODE, key);
			dcipher.init(Cipher.DECRYPT_MODE, key);
			
		} catch (javax.crypto.NoSuchPaddingException e) {
		} catch (java.security.NoSuchAlgorithmException e) {
		} catch (java.security.InvalidKeyException e) {
		}
	}
	/**
	 * 生成KEY
	 * @return
	 * @throws Exception
	 */
	public SecretKey generateKey() throws Exception {
		SecretKey key = KeyGenerator.getInstance("DES").generateKey();
		this.saveKey(this.getKeyPath(), key);
		return key;
	}
	/**
	 * 解码
	 */
	public byte[] decode(byte[] dec) throws Exception {
		byte[] utf8 = dcipher.doFinal(dec);
		return utf8;
	}
	/**
	 * 编码
	 */
	public byte[] encode(byte[] utf8) throws Exception {
		byte[] enc = ecipher.doFinal(utf8);
		return enc;
	}
	/**
	 * 从密匙文件生成KEY
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public SecretKey loadKey(String filename) throws Exception {
		System.out.println("filename:" + filename);
		//实例化文件对象
		File file = new File(filename);
		if (filename == null || filename.equals("")) {
			throw new NullPointerException("无效的文件路径");
		}
		long len = file.length();

		byte[] bytes = new byte[(int) len];
		BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(file));
		//读取密匙文件
		int r = bufferedInputStream.read(bytes);
		if (r != len)
			throw new IOException("读取文件不正确");
		bufferedInputStream.close();
		
		byte rawKeyData[] = bytes;
		
		// 从原始密匙数据创建一个DESKeySpec对象
		DESKeySpec dks = new DESKeySpec(rawKeyData);

		// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
		// 一个SecretKey对象
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(dks);

		return key;
	}
	/**
	 * 将密匙存到密匙文件中
	 * @param filename
	 * @param key
	 * @throws Exception
	 */
	public void saveKey(String filename, Key key) throws Exception {
		System.out.println("保存filename:" + filename);
		File file = new File(filename);
		BufferedOutputStream pubout = new BufferedOutputStream(new FileOutputStream(filename));
		byte[] keyt = key.getEncoded();
		
	    java.security.MessageDigest md = java.security.MessageDigest.getInstance( "MD5" );
	    md.update( keyt );
	    byte tmp[] = md.digest(); 
	   // System.out.println(Converts.);
		pubout.write(tmp);
		pubout.close();
	}

	 
	public String getKeyPath() {
		return keyPath;
	}

	public void setKeyPath(String keyPath) {
		this.keyPath = keyPath;
	}

}

 

四.在使用delphi密匙时rsa  des都会报错:

java.security.spec.InvalidKeySpecException: java.lang.IllegalArgumentException: unknown object in factory
	at org.bouncycastle.jce.provider.JDKKeyFactory$RSA.engineGeneratePublic(Unknown Source)
	at java.security.KeyFactory.generatePublic(KeyFactory.java:278)
	at com.hitech.cpp.components.encoding.RsaEncoder.loadKey(Unknown Source)
	at com.hitech.cpp.components.encoding.RsaEncoder.decode(Unknown Source)
	at com.hitech.cpp.components.encoding.RsaEncoderTest.main(Unknown Source)

 

分享到:
评论
4 楼 莫惟夕小清新 2017-09-15  
博主delphi的源码可以给一份吗。1073098039@qq.com,十分感谢
3 楼 clasp 2008-05-21  
我使用的:delphi是DLL传的是String
2 楼 ahuaxuan 2008-05-19  
这个问题可能是java生成得key何delphi需要得key得字节组织方式不同造成的

1 楼 duronshi 2008-05-19  
你delphi是什么程序?exe or dll???
调用delphi时传的是数组还是String ???

相关推荐

    RSA.rar_3DES DLL DELPHI_delphi rsa_rsa_rsa delphi

    总的来说,这个压缩包提供了Delphi环境下实现RSA和3DES加密的资源,对于学习和开发相关安全应用的Delphi程序员来说非常有价值。开发者可以通过研究这些代码,理解加密算法的工作原理,并在自己的项目中应用或改进...

    Delphi加密算法大全2DES_3DES_3DES3_AES_BASE64_BLOWFISH_CRC_DES_MD5_RSA_SHA

    这些加密算法在Delphi中都有相应的库和组件可供开发者使用。了解并熟练掌握这些算法,可以帮助开发人员构建安全的应用程序,保护用户的数据安全。同时,随着技术的发展,选择更为安全的加密算法和最佳实践变得越来越...

    DES加密算法的delphi源码

    在学习DES加密算法的Delphi源码时,你可以关注以下几个关键点: 1. **密钥扩展**:DES的56位密钥需要经过一系列的扩展和转换,形成16轮加密所需的48位子密钥。这个过程通常包括PC-1置换、左移操作和PC-2置换。 2. ...

    Delphi AES DES MD5 RSA BASE64等加密算法源码实例集.rar

    包括了Delphi AES DES MD5 RSA BASE64 2DES 3DES Blowfish CRC32-Static SHA等 常用的加密算法用法实例,比较不错的Delphi加密实例代码了,浅显易懂而且实用的常见加密算法实例,包括字符串和文件加密解密等。

    delphi实现文件加密解密

    5. **写入加密文件**:将加密后的数据保存到新的文件,通常会带有`.enc`等特殊后缀以表明其已加密。 四、Delphi实现文件解密 1. **加载密文**:打开加密文件,读取其内容。 2. **解密数据**:使用相同的加密库和...

    简单文件加密(Delphi解密代码)..rar

    更先进的加密算法有AES(高级加密标准)、DES(数据加密标准)和RSA(公钥加密技术)等。 2. **AES加密**:AES是目前最广泛使用的对称加密算法,适用于大量数据的快速加密。Delphi提供了Bouncy Castle库,其中包含...

    DELPHI RAS加密组件[支持delphi7-XE2版本]

    在使用RSA等公钥加密算法时,组件会处理公钥和私钥的生成、存储和交换。开发者可以通过设置组件的属性来管理这些密钥,确保只有授权的用户能够解密信息。 5. **安全传输**: DELPHI RAS加密组件不仅适用于本地...

    一个演示Delphi的数据加密技术:文本加密解密源代码..rar

    在Delphi中,我们可以利用各种加密算法来实现这一目标,如对称加密(如AES、DES、3DES等)和非对称加密(如RSA、ECC)。 AES(Advanced Encryption Standard)是一种广泛应用的对称加密算法,速度快,安全性高。在...

    Delphi流加密

    Delphi 提供了多种加密算法库,如 `CryptoLib4Pascal` 或 `DCPCrypt`,可以支持 AES、DES、RSA 等常见的加密算法。选择合适的加密算法,对字符串进行加密处理。 4. **加密文本写入文件**:将加密后的字符串写回到一...

    delphi编写的文件加密系统

    在这个"Delphi编写的文件加密系统"中,我们将探讨使用Delphi进行文件加密的相关知识点。 1. **加密算法选择**:在开发文件加密系统时,首先需要选择一个可靠的加密算法。常见的有AES(高级加密标准)、RSA(公钥...

    Delphi7加密算法大全

    Delphi7加密算法大全RSA DES MD5等

    delphi源码利用流制作EXE文件加密器

    在压缩包的"利用流制作EXE文件加密器"中,你可能会找到实现上述步骤的完整Delphi源代码,这将有助于你深入理解和实践EXE文件的加密技术。通过学习和研究这些源码,你能够提升你的加密编程技巧,并为你的项目提供更高...

    Delphi XE2+与Java互通3Des加解密方法

    使用的是Chilkat Delphi DLL,经过测试仅实现了3DES中ECB加密模式,填充方式为pkcs5pading的加解密,加密后的内容可用在线3DES加解密来验证,可得到与网页一致的加密结果。所以可以与Java,C#等其他语言实现互通加...

    RSA加解密算法 DELPHI原码

    在DELPHI编程环境中实现RSA加解密,需要对数学和加密原理有一定的理解。DELPHI是一种强大的面向对象的编程语言,它提供了丰富的库支持,可以方便地实现各种算法。描述中提到的"TXT后缀名请改成.PAS即可"意味着提供的...

    Delphi_d7加密算法大全.rar

    在Delphi_d7加密算法大全中,你将找到这些算法的Delphi实现,这对于学习和理解这些算法的工作原理、如何在Delphi程序中应用它们以及测试不同加密和解密场景非常有帮助。通过实践案例,开发者可以更好地掌握这些技术...

    delphi xe AES加密解密源码

    1. **密钥扩展**:AES允许128位、192位和256位的密钥,源码会有一个函数用于将输入的密钥扩展成足够多的轮密钥,以供加密和解密过程使用。 2. **字节替换**:使用预定义的S盒(Substitution Box)进行非线性变换,...

    delphi源码——.bat 文件批量加密解密器

    在Delphi程序中,这通常通过读取.bat文件的文本内容,然后使用某种加密算法(如AES、DES或RSA)对文本进行加密。加密后的数据会被保存到新的文件中,原.bat文件则被删除或替换。解密过程则是逆向操作,从加密后的...

    RAS公钥加密私钥解密的例子(加密文件)

    当一个信息发送者想要向接收者发送安全信息时,他会使用接收者的公钥来加密数据。因为公钥是公开的,任何人都可以看到这个加密的信息,但没有对应的私钥,任何人都无法解密。只有接收者拥有私钥,因此只有他能够解密...

    delphi 字符串加密 解密小程序

    在Delphi中,我们可以使用各种加密算法来实现这一目标,如AES(高级加密标准)、DES(数据加密标准)、RSA(公钥加密技术)等。 1. **AES加密**:AES是一种广泛应用的块加密算法,它使用相同的密钥对数据进行加密和...

    delphi 文本加密源码

    在Delphi项目中,你可以创建一个简单的文本加密应用程序,包含一个主界面,用户输入文本,选择加密算法,然后点击“加密”按钮将文本加密并保存到文件,同时提供解密功能。 7. **安全注意事项** - 密钥管理:密钥...

Global site tag (gtag.js) - Google Analytics