当前位置:首页 » 比特币问答 » java用btc的公钥来加密

java用btc的公钥来加密

发布时间: 2021-10-02 07:17:33

㈠ JAVA公钥加密,私钥解密,该怎么解决

RSA算法,选取两个互质数
如p:6和q:5(最大公约数为1)
求出乘积n=30,欧拉函数值((p - 1) * (q - 1)) eul =20
选出一个和eul互质且小于eul大于1的数,如 e = 19
通过扩展欧几里得算法求逆元 此处求出一个逆元 d = 39
逆元就是满足公式 (e*d) % eul = 1的值(该公式可能有多个解,求出一个就行)
(n,e)组成公钥,(n,d)组成私钥
假定明文是一个数字m
计算 m的e次方模n 得到的余数就是密文 em
计算 em的d次方模n 得到的余数就是明文 m
因此可以使用公钥加密byte数组,使用私钥解密还原byte数组
byte数组组成了字符串、文件等
最后注意,要加密的明文二进制位数不能超过密钥的二进制位数

㈡ c#怎么调用java生成的RSA 公钥进行加密

.NET无法调用JAVA产生的RSA公钥,必须将RSA算法在.NET里面重写才行,在.NET里面RSA的公钥长度是128位的,但是你给出的JAVA公钥却是159位长度,非常的不标准,公钥长度不满足128的肯定无法给.NET使用。

这里最多帮做个对应解析,数据是肯定无法用的:
将java的RSA公钥最后四个字母AQAB分割开,用.NET的xml格式表示就是
<RSAKeyValue><Molus>
/qp
jLFfDCu3qytxf+/IoCYE

+Hf4hsEDUKV2kkhRJsnwwID</Molus><Exponent>AQAB</Exponent>
</RSAKeyValue>
这里的数据都是用的BASE64编码,你用BASE64解码后可以得到byte[],就可以看到密钥长度了,实际密钥要转换为BigInteger后才能参与RSA核心运算

㈢ JAVA公钥加密,私钥解密,该怎么解决

{
publicstaticfinalStringKEY_ALGORITHM="RSA";
_ALGORITHM="MD5withRSA";
_KEY="RSAPublicKey";
_KEY="RSAPrivateKey";
/**
*用私钥对信息生成数字签名
*
*@paramdata
*加密数据
*@paramprivateKey
*私钥
*
*@return
*@throwsException
*/
publicstaticStringsign(byte[]data,StringprivateKey)throwsException{
//解密由base64编码的私钥
byte[]keyBytes=decryptBASE64(privateKey);
//构造PKCS8EncodedKeySpec对象
=newPKCS8EncodedKeySpec(keyBytes);
//KEY_ALGORITHM指定的加密算法
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
//取私钥匙对象
PrivateKeypriKey=keyFactory.generatePrivate(pkcs8KeySpec);
//用私钥对信息生成数字签名
Signaturesignature=Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(priKey);
signature.update(data);
returnencryptBASE64(signature.sign());
}
/**
*校验数字签名
*
*@paramdata
*加密数据
*@parampublicKey
*公钥
*@paramsign
*数字签名
*
*@return校验成功返回true失败返回false
*@throwsException
*
*/
publicstaticbooleanverify(byte[]data,StringpublicKey,Stringsign)
throwsException{
//解密由base64编码的公钥
byte[]keyBytes=decryptBASE64(publicKey);
//构造X509EncodedKeySpec对象
X509EncodedKeySpeckeySpec=newX509EncodedKeySpec(keyBytes);
//KEY_ALGORITHM指定的加密算法
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
//取公钥匙对象
PublicKeypubKey=keyFactory.generatePublic(keySpec);
Signaturesignature=Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(pubKey);
signature.update(data);
//验证签名是否正常
returnsignature.verify(decryptBASE64(sign));
}
/**
*解密<br>
*用私钥解密
*
*@paramdata
*@paramkey
*@return
*@throwsException
*/
publicstaticbyte[]decryptByPrivateKey(byte[]data,Stringkey)
throwsException{
//对密钥解密
byte[]keyBytes=decryptBASE64(key);
//取得私钥
=newPKCS8EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
KeyprivateKey=keyFactory.generatePrivate(pkcs8KeySpec);
//对数据解密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE,privateKey);
returncipher.doFinal(data);
}
/**
*解密<br>
*用公钥解密
*
*@paramdata
*@paramkey
*@return
*@throwsException
*/
publicstaticbyte[]decryptByPublicKey(byte[]data,Stringkey)
throwsException{
//对密钥解密
byte[]keyBytes=decryptBASE64(key);
//取得公钥
X509EncodedKeySpecx509KeySpec=newX509EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
KeypublicKey=keyFactory.generatePublic(x509KeySpec);
//对数据解密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE,publicKey);
returncipher.doFinal(data);
}
/**
*加密<br>
*用公钥加密
*
*@paramdata
*@paramkey
*@return
*@throwsException
*/
publicstaticbyte[]encryptByPublicKey(byte[]data,Stringkey)
throwsException{
//对公钥解密
byte[]keyBytes=decryptBASE64(key);
//取得公钥
X509EncodedKeySpecx509KeySpec=newX509EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
KeypublicKey=keyFactory.generatePublic(x509KeySpec);
//对数据加密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
returncipher.doFinal(data);
}
/**
*加密<br>
*用私钥加密
*
*@paramdata
*@paramkey
*@return
*@throwsException
*/
publicstaticbyte[]encryptByPrivateKey(byte[]data,Stringkey)
throwsException{
//对密钥解密
byte[]keyBytes=decryptBASE64(key);
//取得私钥
=newPKCS8EncodedKeySpec(keyBytes);
KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
KeyprivateKey=keyFactory.generatePrivate(pkcs8KeySpec);
//对数据加密
Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE,privateKey);
returncipher.doFinal(data);
}
/**
*取得私钥
*
*@paramkeyMap
*@return
*@throwsException
*/
(Map<String,Object>keyMap)
throwsException{
Keykey=(Key)keyMap.get(PRIVATE_KEY);
returnencryptBASE64(key.getEncoded());
}
/**
*取得公钥
*
*@paramkeyMap
*@return
*@throwsException
*/
(Map<String,Object>keyMap)
throwsException{
Keykey=(Key)keyMap.get(PUBLIC_KEY);
returnencryptBASE64(key.getEncoded());
}
/**
*初始化密钥
*
*@return
*@throwsException
*/
publicstaticMap<String,Object>initKey()throwsException{
KeyPairGeneratorkeyPairGen=KeyPairGenerator
.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPairkeyPair=keyPairGen.generateKeyPair();
//公钥
RSAPublicKeypublicKey=(RSAPublicKey)keyPair.getPublic();
//私钥
RSAPrivateKeyprivateKey=(RSAPrivateKey)keyPair.getPrivate();
Map<String,Object>keyMap=newHashMap<String,Object>(2);
keyMap.put(PUBLIC_KEY,publicKey);
keyMap.put(PRIVATE_KEY,privateKey);
returnkeyMap;
}
}

㈣ Java 第三方公钥 RSA加密求助

下面是RSA加密代码。

/**
* RSA算法,实现数据的加密解密。
* @author ShaoJiang
*
*/
public class RSAUtil {

private static Cipher cipher;

static{
try {
cipher = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}

/**
* 生成密钥对
* @param filePath 生成密钥的路径
* @return
*/
public static Map<String,String> generateKeyPair(String filePath){
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 密钥位数
keyPairGen.initialize(1024);
// 密钥对
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公钥
PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
//得到公钥字符串
String publicKeyString = getKeyString(publicKey);
//得到私钥字符串
String privateKeyString = getKeyString(privateKey);

FileWriter pubfw = new FileWriter(filePath+"/publicKey.keystore");
FileWriter prifw = new FileWriter(filePath+"/privateKey.keystore");
BufferedWriter pubbw = new BufferedWriter(pubfw);
BufferedWriter pribw = new BufferedWriter(prifw);
pubbw.write(publicKeyString);
pribw.write(privateKeyString);
pubbw.flush();
pubbw.close();
pubfw.close();
pribw.flush();
pribw.close();
prifw.close();
//将生成的密钥对返回
Map<String,String> map = new HashMap<String,String>();
map.put("publicKey",publicKeyString);
map.put("privateKey",privateKeyString);
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 得到公钥
*
* @param key
* 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}

/**
* 得到私钥
*
* @param key
* 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}

/**
* 得到密钥字符串(经过base64编码)
*
* @return
*/
public static String getKeyString(Key key) throws Exception {
byte[] keyBytes = key.getEncoded();
String s = (new BASE64Encoder()).encode(keyBytes);
return s;
}

/**
* 使用公钥对明文进行加密,返回BASE64编码的字符串
* @param publicKey
* @param plainText
* @return
*/
public static String encrypt(PublicKey publicKey,String plainText){
try {
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] enBytes = cipher.doFinal(plainText.getBytes());
return (new BASE64Encoder()).encode(enBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}

/**
* 使用keystore对明文进行加密
* @param publicKeystore 公钥文件路径
* @param plainText 明文
* @return
*/
public static String encrypt(String publicKeystore,String plainText){
try {
FileReader fr = new FileReader(publicKeystore);
BufferedReader br = new BufferedReader(fr);
String publicKeyString="";
String str;
while((str=br.readLine())!=null){
publicKeyString+=str;
}
br.close();
fr.close();
cipher.init(Cipher.ENCRYPT_MODE,getPublicKey(publicKeyString));
byte[] enBytes = cipher.doFinal(plainText.getBytes());
return (new BASE64Encoder()).encode(enBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 使用私钥对明文密文进行解密
* @param privateKey
* @param enStr
* @return
*/
public static String decrypt(PrivateKey privateKey,String enStr){
try {
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));
return new String(deBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* 使用keystore对密文进行解密
* @param privateKeystore 私钥路径
* @param enStr 密文
* @return
*/
public static String decrypt(String privateKeystore,String enStr){
try {
FileReader fr = new FileReader(privateKeystore);
BufferedReader br = new BufferedReader(fr);
String privateKeyString="";
String str;
while((str=br.readLine())!=null){
privateKeyString+=str;
}
br.close();
fr.close();
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyString));
byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));
return new String(deBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

㈤ JAVA 公钥与私钥的问题

这个公钥和私钥如果是非对称加密的算法,那么用公钥加密的话,就需要用私钥才能解密了。如果是对称加密的话,就用加密的公钥就可以解密了。

SSL加密通信的过程一般都是首先双方确认大家支持的加密方式,然后采用一种大家都支持的,然后的话,就先用非对称加密将公钥加密然后发给client,然后client解密获得公钥。之后的话,大家就可以用公钥进行对称加密了。

㈥ JAVA写RSA加密,公钥私钥都是一样的,为什么每次加密的结果不一样

JAVA写RSA加密,私钥都是一样的,公钥每次加密的结果不一样跟对数据的padding(填充)有关。

㈦ C#用JavaRSA公钥加密得到的密文再用Java去解可以吗

RSA 加密的话,一共有几个参数,你需要看看你的填充(padding)是不是配置成一样的,另外就是你的块长度是不是两边一样了。一般来说解密不对都是因为这些配置没有对。

㈧ JAVA公钥加密,私钥解密,该怎么解决

今天有空研究了下大家都在喊的AES加密!还以为是什么深奥的东西呢!终于了解了,心中释然了!跟大家一起分享下吧!DES其实就是:数据加密标准 英文的缩写!就是个加密的标注而已,AES就是 高级加密标准 英文的缩写咯,大家都叫缩写叫惯了,搞得我们这些没接触的人一头雾水!心里还真憋屈的慌呢!

这是在搜集资料的时候拿了个例子练手,不过有个问题就是,把这代码放到文本里用CMD运行的时候出现了乱码情况!所幸的是注释,不影响效果!但是,程序要真遇到这样的情况的话,就得转码了,因为文本的编码是GBK的,而我所要粘贴的代码的编码是UTF-8

[html] view plain
import java.util.*;
import java.io.*;

public class Test
{
private String encodeResult;//编码后字串
private String decodeResult;//解码后字串

public Test()
{

}
//编码设置
public void setEncodeResult(String encodeResult)
{
char[] src = encodeResult.toCharArray();//将待编码字串拆分成字符数组
StringBuilder sb = new StringBuilder();//保存编码后字符
//将待编码字串拆分成字符数组
for(int i = 0; i< src.length; i++)
{
if(Character.isDigit(src[i]))
{
if(i != src.length-1)
{//满足条件3
char[] temp = new char[Character.getNumericValue(src[i])+1];
Arrays.fill(temp,src[i+1]);
sb.append(temp);
sb.append("_");
}
else
{//满足条件2
sb.append(src[i]);
}
}
else if(src[i] == '_')//满足条件5
{
sb.append("\\UL");
sb.append("_");
}
else if(i == src.length-1)//满足条件1,且到了字串结尾
{
sb.append(src[i]);
}
else//满足条件1,且未到字串结尾
{
sb.append(src[i]);
sb.append("_");
}
}
this.encodeResult = new String(sb);//创建返回编码后字串
}
//获得编码后结果
public String getEncodeResult()
{
return encodeResult;
}
//解码设置
public void setDecodeResult(String encodeResult)
{
String[] temp = encodeResult.split("_");
StringBuilder sb = new StringBuilder();
for(int i = 0; i< temp.length; i++)
{
if(temp[i].equals("\\UL"))
sb.append("_");
else if(temp[i].length()>1)
sb.append(temp[i].length()-1);
else
sb.append(temp[i]);
}
this.decodeResult = new String(sb);
}
//获得解码后结果
public String getDecodeResult()
{
return decodeResult;
}
public static void main(String[] args)
{
System.out.println("请输入待编码字符串(以回车键结束):"); //此处存在一个乱码问题,在文本文档中的编码是GBK而它的编码是UTF-8,cmd不识别!
String source = "";
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
source = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
Test e = new Test();
e.setEncodeResult(source);
System.out.println("编码后结果:"+e.getEncodeResult());
e.setDecodeResult(e.getEncodeResult());
System.out.println("解码后结果:"+e.getDecodeResult());
}
}
[html] view plain
<pre name="code" class="html">请输入待编码字符串(以回车键结束):
abcdc123
编码后结果:a_b_c_d_c_22_333_3
解码后结果:abcdc123
【最简单的加密】
1.简单的概念

明文:加密前的信息

密文:机密后的信息

算法:加密或解密的算法

密钥:算法使用的钥匙

例子:
将123456每位数字都加 1 后得到234567,

其中123456就是明文,

234567就是密文,

加密密钥就是1,

加密算法是每位加

[html] view plain
<span style="font-size:18px;">import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 编码工具类
* 1.将byte[]转为各种进制的字符串
* 2.base 64 encode
* 3.base 64 decode
* 4.获取byte[]的md5值
* 5.获取字符串md5值
* 6.结合base64实现md5加密
* 7.AES加密
* 8.AES加密为base 64 code
* 9.AES解密
* 10.将base 64 code AES解密
* @author uikoo9
* @version 0.0.7.20140601
*/
public class Test {
public static void main(String[] args) throws Exception {
String content = "我爱你,祖国";
System.out.println("加密前:" + content);

String key = "123456";
System.out.println("加密密钥和解密密钥:" + key);

String encrypt = aesEncrypt(content, key);
System.out.println("加密后:" + encrypt);

String decrypt = aesDecrypt(encrypt, key);
System.out.println("解密后:" + decrypt);
}
/**
* AES加密为base 64 code
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的base 64 code
* @throws Exception
*/
public static String aesEncrypt(String content, String encryptKey) throws Exception {
return base64Encode(aesEncryptToBytes(content, encryptKey));
}
/**
* AES加密
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的byte[]
* @throws Exception
*/
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(encryptKey.getBytes()));

Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

return cipher.doFinal(content.getBytes("utf-8"));
}
/**
* base 64 encode
* @param bytes 待编码的byte[]
* @return 编码后的base 64 code
*/
public static String base64Encode(byte[] bytes){
return new BASE64Encoder().encode(bytes);
}
/**
* 将base 64 code AES解密
* @param encryptStr 待解密的base 64 code
* @param decryptKey 解密密钥
* @return 解密后的string
* @throws Exception
*/
public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
return aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
}
/**
* AES解密
* @param encryptBytes 待解密的byte[]
* @param decryptKey 解密密钥
* @return 解密后的String
* @throws Exception
*/
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(decryptKey.getBytes()));

Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);

return new String(decryptBytes);
}
/**
* base 64 decode
* @param base64Code 待解码的base 64 code
* @return 解码后的byte[]
* @throws Exception
*/
public static byte[] base64Decode(String base64Code) throws Exception{
return new BASE64Decoder().decodeBuffer(base64Code);
}
}</span><span style="font-size:14px;">
</span>

㈨ java程序报错后 base64encode 使用公钥加密 加密值为一样的 怎么回事

在开发的时候遇到个现象。对方用PHPbase64_encode()对字符串进行加米。但我这边是用Java解马。导致出现问题。问题如下:[java]viewplainpackagecom.tudou.test;importjava.io.IOException;/***javabase64编码和解码的演示类*

㈩ java 中的Cipher类RSA方式能不能用私钥加密公钥解密,完整解释下

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//把第二个参数改为 key.getPrivate()
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
byte[] cipherText = cipher.doFinal("Message".getBytes("UTF8"));
System.out.println(new String(cipherText, "UTF8"));
//把第二个参数改为key.getPublic()
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println(new String(newPlainText, "UTF8"));

正常的用公钥加密私钥解密就是这个过程,如果按私钥加密公钥解密,只要按备注改2个参数就可以。

但是我要提醒楼主,你要公钥解密,公钥是公开的,相当于任何人都查到公钥可以解密。
你是想做签名是吧。

热点内容
收到假eth币 发布:2025-10-20 08:58:16 浏览:973
暗黑破坏神2eth打孔 发布:2025-10-20 08:42:58 浏览:105
BTC和CBT是一样的吗 发布:2025-10-20 08:42:57 浏览:233
华硕trx40Pro供电 发布:2025-10-20 08:33:26 浏览:432
晒人民币编号的朋友圈 发布:2025-10-20 08:25:32 浏览:687
doge格式 发布:2025-10-20 08:02:00 浏览:382
以太坊会爆发吗 发布:2025-10-20 08:01:59 浏览:772
一台比特币矿机的功率 发布:2025-10-20 07:39:24 浏览:925
trx辅助带 发布:2025-10-20 07:35:29 浏览:48
比特币哈希值有多少位 发布:2025-10-20 07:31:20 浏览:633