java端這邊 自己加密和解密都沒問題
delphi那邊不太懂,從網上找來的,自己加密解密也沒問題
但是java端私鑰加密過的結果給delphi端再還原公鑰解密就不行
package com.algorithm;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Hex;
/**
* RSA非對稱加密演算法安全編碼組件
* @author Administrator
*
*/
public abstract class RSACoder {
//非對稱加密密鑰演算法
public static final String KEY_ALGORITHM="RSA";
//數字簽名 簽名/驗證演算法
public static final String SIGNATURE_ALGORRITHM="SHA1withRSA";
//公鑰
private static final String PUBLIC_KEY="RSAPublicKey";
//私鑰
private static final String PRIVATE_KEY="RSAPrivateKey";
//RSA密鑰長度,默認為1024,密鑰長度必須是64的倍數,范圍在521~65526位之間
private static final int KEY_SIZE=512;
private static final int MAX_DECRYPT_BLOCK = 64;
public static byte[] getPublicKey(String modulus, String publicExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
BigInteger bigIntPrivateExponent = new BigInteger(publicExponent);
BigInteger bigIntModulus = new BigInteger(modulus);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus,bigIntPrivateExponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey.getEncoded();
}
/**
* 公鑰解密
* @param data 待解密資料
* @param key 公鑰
* @return byte[] 解密資料
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data,byte[] key) throws Exception
{
//取得公鑰
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
//生成公鑰
PublicKey publicKey=keyFactory.generatePublic(x509KeySpec);
//對資料解密
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
* 私鑰加密
* @param data 待加密資料
* @param key 私鑰
* @return byte[] 加密資料
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data,byte[] key) throws Exception
{
//取得私鑰
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
//生成私鑰
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
//對資料加密
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
/**
* 私鑰加密
* @param data 待加密資料
* @param key 私鑰
* @return byte[] 加密資料
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data,String key) throws Exception
{
return encryptByPrivateKey(data,getKey(key));
}
/**
* 取得私鑰
* @param keyMap 密鑰Map
* @return byte[] 私鑰
* @throws Exception
*/
public static byte[] getPrivateKey(Map<String,Object> keyMap) throws Exception
{
Key key=(Key)keyMap.get(PRIVATE_KEY);
return key.getEncoded();
}
/**
* 取得公鑰
* @param keyMap 密鑰Map
* @return byte[] 公鑰
* @throws Exception
*/
public static byte[] getPublicKey(Map<String,Object> keyMap) throws Exception
{
Key key=(Key)keyMap.get(PUBLIC_KEY);
return key.getEncoded();
}
/**
* 初始化密鑰
* @return 密鑰Map
* @throws Exception
*/
public static Map<String,Object> initKey() throws Exception
{
//實體化實鑰對生成器
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance(KEY_ALGORITHM);
//初始化密鑰對生成器
keyPairGen.initialize(KEY_SIZE);
//生成密鑰對
KeyPair keyPair=keyPairGen.generateKeyPair();
//公鑰
RSAPublicKey publicKey=(RSAPublicKey) keyPair.getPublic();
//私鑰
RSAPrivateKey privateKey=(RSAPrivateKey) keyPair.getPrivate();
//封裝密鑰
Map<String,Object> keyMap=new HashMap<String,Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
/**
* 取得私鑰十六進制表示形式
* @param keyMap 密鑰Map
* @return String 私鑰十六進制字串
* @throws Exception
*/
public static String getPrivateKeyString(Map<String,Object> keyMap) throws Exception
{
return Hex.encodeHexString(getPrivateKey(keyMap));
}
/**
* 取得公鑰十六進制表示形式
* @param keyMap 密鑰Map
* @return String 公鑰十六進制字串
* @throws Exception
*/
public static String getPublicKeyString(Map<String,Object> keyMap) throws Exception
{
return Hex.encodeHexString(getPublicKey(keyMap));
}
/**
* 獲取密鑰
* @param key 密鑰
* @return byte[] 密鑰
* @throws Exception
*/
public static byte[] getKey(String key) throws Exception
{
return Hex.decodeHex(key.toCharArray());
}
}
測驗
package com.alporithm;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
/**
* RSA演算法測驗用例
* @author Administrator
*
*/
public class RSACoderTest {
//公鑰
private static byte[] publicKey;
//私鑰
private static byte[] privateKey;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Map<String,Object> keyMap=RSACoder.initKey();
publicKey=RSACoder.getPublicKey(keyMap);
privateKey=RSACoder.getPrivateKey(keyMap);
String inputStr="1111";
byte[] data=https://bbs.csdn.net/topics/inputStr.getBytes();
//私鑰加密
byte[] enCodeData=https://bbs.csdn.net/topics/RSACoder.encryptByPrivateKey(data,privateKey);
System.out.println("加密字串:"+Base64.encodeBase64String(enCodeData));
//公鑰解密
byte[] deCodeData=https://bbs.csdn.net/topics/RSACoder.decryptByPublicKey(enCodeData, publicKey);
System.out.println(new String(deCodeData));
}
}
delphi這邊需要的居然是什么Exponent,Modulus
網上下的看不懂意思我當時思路是把java里公鑰的module和exponent找到丟給delphi那邊,然后都設512位的應該就能解。可是delphi那邊報錯
然后我又反過來試把delphi生成的module和exponent丟給java這邊,又用RSACoder。getPublicKey(String modulus, String publicExponent)里生成公鑰,用來解密。
可是結果發現重算出來的公鑰經常不是512bit,即使碰到一次生成回512bit也無法正常解密
求助一下不會傳附件
java用的jar包
鏈接: http://pan.baidu.com/s/1eSFnOVS 密碼: p4qu
網上delphi用例
鏈接: http://pan.baidu.com/s/1dFcasDV 密碼: tjhu
uj5u.com熱心網友回復:
頂
uj5u.com熱心網友回復:
樓主這個問題解決了嗎,我現在也碰到這個問題了,若解決了可否分享一下解決方法,謝謝uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/58218.html
標籤:語言基礎/算法/系統設計
上一篇:RzPageControl停靠視窗時出錯Error saving device context
下一篇:資料記錄 查找
