java公钥解密私钥加密
㈠ 怎么在ios进行rsa公钥加密,java做rsa私钥解密
1、用公钥加密,用私钥解密。
2、给别人发信息,就从服务器上拉下来别人的公钥,加密后发给他。
3、对方拿到信息后用自己的私钥解密。
4、这样,公钥加密后除了私钥持有人,别正察人都看不到信息。
5、若是用私钥加密,那么公钥都能解密,还有何安全性可言?
6、私钥加密的场合旅清尘只有一个,那就是数字签名,用来表明这拆禅个信息来源于你。
㈡ java中RSA用私钥加密公钥解密问题
公钥和私钥可以互换的用,用公钥加密私钥解密,用私钥加密公钥解密都ok,方法一样
㈢ JAVA公钥加密,私钥解密,该怎么解决
一个比较简单的实现:一个三个类KeyGenerater生成公钥私钥对,Signaturer类使用私钥签名,SignProvider用公钥验证。公钥和私钥使用Base64加密Base64这个类也在博客里面
public class KeyGenerater {
private byte[] priKey;
private byte[] pubKey;
public void generater() {
try {
Java.security.KeyPairGenerator keygen = java.security.KeyPairGenerator
.getInstance("RSA");
SecureRandom secrand = new SecureRandom();
secrand.setSeed("syj".getBytes()); // 初始化随机产生器
keygen.initialize(1024, secrand);
KeyPair keys = keygen.genKeyPair();
PublicKey pubkey = keys.getPublic();
PrivateKey prikey = keys.getPrivate();
pubKey = Base64.encodeToByte(pubkey.getEncoded());
priKey = Base64.encodeToByte(prikey.getEncoded());
System.out.println("pubKey = " + new String(pubKey));
System.out.println("priKey = " + new String(priKey));
} catch (java.lang.Exception e) {
System.out.println("生成密钥对失败");
e.printStackTrace();
}
}
public byte[] getPriKey() {
return priKey;
}
public byte[] getPubKey() {
return pubKey;
}
}
public class Signaturer {
/**
*
* Description:数字签名
*
* @param priKeyText
* @param plainText
* @return
* @author 孙钰佳
* @since:2007-12-27 上午10:51:48
*/
public static byte[] sign(byte[] priKeyText, String plainText) {
try {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64
.decode(priKeyText));
KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey prikey = keyf.generatePrivate(priPKCS8);
// 用私钥对信息生成数字签名
java.security.Signature signet = java.security.Signature
.getInstance("MD5withRSA");
signet.initSign(prikey);
signet.update(plainText.getBytes());
byte[] signed = Base64.encodeToByte(signet.sign());
return signed;
} catch (java.lang.Exception e) {
System.out.println("签名失败");
e.printStackTrace();
}
return null;
}
}
public class SignProvider {
private SignProvider() {
}
/**
*
* Description:校验数字签名,此方法不会抛出任务异常,成功返回true,失败返回false,要求全部参数不能为空
*
* @param pubKeyText
* 公钥,base64编码
* @param plainText
* 明文
* @param signTest
* 数字签名的密文,base64编码
* @return 校验成功返回true 失败返回false
* @author 孙钰佳
* @since:2007-12-27 上午09:33:55
*/
public static boolean verify(byte[] pubKeyText, String plainText,
byte[] signText) {
try {
// 解密由base64编码的公钥,并构造X509EncodedKeySpec对象
java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
Base64.decode(pubKeyText));
// RSA对称加密算法
java.security.KeyFactory keyFactory = java.security.KeyFactory
.getInstance("RSA");
// 取公钥匙对象
java.security.PublicKey pubKey = keyFactory
.generatePublic(bobPubKeySpec);
// 解密由base64编码的数字签名
byte[] signed = Base64.decode(signText);
java.security.Signature signatureChecker = java.security.Signature
.getInstance("MD5withRSA");
signatureChecker.initVerify(pubKey);
signatureChecker.update(plainText.getBytes());
// 验证签名是否正常
if (signatureChecker.verify(signed))
return true;
else
return false;
} catch (Throwable e) {
System.out.println("校验签名失败");
e.printStackTrace();
return false;
}
}
}
望采纳,谢谢。
㈣ 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私钥加密
java rsa私钥加密是什么?让我们一起来了解一下吧!
java rsa私钥加密是一种加密算法。私钥加密算法是用私钥来进行加密与解密信息。私钥加密也被称作对称加密,原因是加密与解密使用的秘钥是同一个。
RSA加密需要注意的事项如下:
1. 首先产生公钥与私钥
2. 设计加密与解密的算法
3. 私钥加密的数据信息只能由公钥可以解密
4. 公钥加密的数据信息只能由私钥可以解密
实战演练,具体步骤如下: public class RsaCryptTools { private static final String CHARSET = "utf-8"; private static final Base64.Decoder decoder64 = Base64.getDecoder(); private static final Base64.Encoder encoder64 = Base64.getEncoder(); /** * 生成公私钥 * @param keySize * @return * @throws NoSuchAlgorithmException */ public static SecretKey generateSecretKey(int keySize) throws NoSuchAlgorithmException { //生成密钥对 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keySize, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); PrivateKey privateKey = pair.getPrivate(); PublicKey publicKey = pair.getPublic(); //这里可以将密钥对保存到本地 return new SecretKey(encoder64.encodeToString(publicKey.getEncoded()), encoder64.encodeToString(privateKey.getEncoded())); } /** * 私钥加密 * @param data * @param privateInfoStr * @return * @throws IOException * @throws InvalidCipherTextException */ public static String encryptData(String data, String privateInfoStr) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey(privateInfoStr)); return encoder64.encodeToString(cipher.doFinal(data.getBytes(CHARSET))); } /** * 公钥解密 * @param data * @param publicInfoStr * @return */ public static String decryptData(String data, String publicInfoStr) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { byte[] encryptDataBytes=decoder64.decode(data.getBytes(CHARSET)); //解密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, getPublicKey(publicInfoStr)); return new String(cipher.doFinal(encryptDataBytes), CHARSET); } private static PublicKey getPublicKey(String base64PublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes())); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(keySpec); } private static PrivateKey getPrivateKey(String base64PrivateKey) throws NoSuchAlgorithmException, InvalidKeySpecException { PrivateKey privateKey = null; PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes())); KeyFactory keyFactory = null; keyFactory = KeyFactory.getInstance("RSA"); privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /** * 密钥实体 * @author hank * @since 2020/2/28 0028 下午 16:27 */ public static class SecretKey { /** * 公钥 */ private String publicKey; /** * 私钥 */ private String privateKey; public SecretKey(String publicKey, String privateKey) { this.publicKey = publicKey; this.privateKey = privateKey; } public String getPublicKey() { return publicKey; } public void setPublicKey(String publicKey) { this.publicKey = publicKey; } public String getPrivateKey() { return privateKey; } public void setPrivateKey(String privateKey) { this.privateKey = privateKey; } @Override public String toString() { return "SecretKey{" + "publicKey='" + publicKey + '\'' + ", privateKey='" + privateKey + '\'' + '}'; } } private static void writeToFile(String path, byte[] key) throws IOException { File f = new File(path); f.getParentFile().mkdirs(); try(FileOutputStream fos = new FileOutputStream(f)) { fos.write(key); fos.flush(); } } public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException { SecretKey secretKey = generateSecretKey(2048); System.out.println(secretKey); String enStr = encryptData("你好测试测试", secretKey.getPrivateKey()); System.out.println(enStr); String deStr = decryptData(enStr, secretKey.getPublicKey()); System.out.println(deStr); enStr = encryptData("你好测试测试hello", secretKey.getPrivateKey()); System.out.println(enStr); deStr = decryptData(enStr, secretKey.getPublicKey()); System.out.println(deStr); } }
㈥ JAVA公钥加密,私钥解密,该怎么解决
1、默认 Java 中仅支持 128 位密钥,当使用 256 位密钥的时候,会报告密钥长度错误 Invalid AES key length 你需要下载一个支持更长密钥的包。这个包叫做 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6 看一下你的 JRE 环境,将 JRE 环境中 lib\lib\security 中的同名包替换掉。
2、Base64 问题 // 编码 String asB64 = new Base64().encodeToString("some string".getBytes("utf-8")); System.out.println(asB64); // 输出为: c29tZSBzdHJpbmc= 解码 // 解码 byte[] asBytes = new Base64().getDecoder().decode("c29tZSBzdHJpbmc="); System.out.println(new String(asBytes, "utf-8")); // 输出为: some string ...
㈦ 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个参数就可以。
但是我要提醒楼主,你要公钥解密,公钥是公开的,相当于任何人都查到公钥可以解密。
你是想做签名是吧。