Java中RSA的方式如何实现非对称加密的示例

作者&投稿:闵昌 (若有异议请与网页底部的电邮联系)
RSA PKCS#1在java中怎么实现?~

楼主看看下面的代码是不是你所需要的,这是我原来用的时候收集的
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.io.*;
import java.math.BigInteger;

/**
* RSA 工具类。提供加密,解密,生成密钥对等方法。
* 需要到http://www.bouncycastle.org下载bcprov-jdk14-123.jar。
* RSA加密原理概述
* RSA的安全性依赖于大数的分解,公钥和私钥都是两个大素数(大于100的十进制位)的函数。
* 据猜测,从一个密钥和密文推断出明文的难度等同于分解两个大素数的积
* ===================================================================
* (该算法的安全性未得到理论的证明)
* ===================================================================
* 密钥的产生:
* 1.选择两个大素数 p,q ,计算 n=p*q;
* 2.随机选择加密密钥 e ,要求 e 和 (p-1)*(q-1)互质
* 3.利用 Euclid 算法计算解密密钥 d , 使其满足 e*d = 1(mod(p-1)*(q-1)) (其中 n,d 也要互质)
* 4:至此得出公钥为 (n,e) 私钥为 (n,d)
* ===================================================================
* 加解密方法:
* 1.首先将要加密的信息 m(二进制表示) 分成等长的数据块 m1,m2,...,mi 块长 s(尽可能大) ,其中 2^s<n
* 2:对应的密文是: ci = mi^e(mod n)
* 3:解密时作如下计算: mi = ci^d(mod n)
* ===================================================================
* RSA速度
* 由于进行的都是大数计算,使得RSA最快的情况也比DES慢上100倍,无论是软件还是硬件实现。
* 速度一直是RSA的缺陷。一般来说只用于少量数据加密。
* 文件名:RSAUtil.java
* @author 赵峰
* 版本:1.0.1
* 描述:本算法摘自网络,是对RSA算法的实现
* 创建时间:2009-7-10 下午09:58:16
* 文件描述:首先生成两个大素数,然后根据Euclid算法生成解密密钥
*/
public class RSAUtil {

//密钥对
private KeyPair keyPair = null;

/**
* 初始化密钥对
*/
public RSAUtil(){
try {
this.keyPair = this.generateKeyPair();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 生成密钥对
* @return KeyPair
* @throws Exception
*/
private KeyPair generateKeyPair() throws Exception {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
//这个值关系到块加密的大小,可以更改,但是不要太大,否则效率会低
final int KEY_SIZE = 1024;
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGen.genKeyPair();
return keyPair;
} catch (Exception e) {
throw new Exception(e.getMessage());
}

}

/**
* 生成公钥
* @param modulus
* @param publicExponent
* @return RSAPublicKey
* @throws Exception
*/
private RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) throws Exception {

KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
try {
return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}

}

/**
* 生成私钥
* @param modulus
* @param privateExponent
* @return RSAPrivateKey
* @throws Exception
*/
private RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));
try {
return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}
}

/**
* 加密
* @param key 加密的密钥
* @param data 待加密的明文数据
* @return 加密后的数据
* @throws Exception
*/
public byte[] encrypt(Key key, byte[] data) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, key);
// 获得加密块大小,如:加密前数据为128个byte,而key_size=1024 加密块大小为127 byte,加密后为128个byte;
// 因此共有2个加密块,第一个127 byte第二个为1个byte
int blockSize = cipher.getBlockSize();
// System.out.println("blockSize:"+blockSize);
int outputSize = cipher.getOutputSize(data.length);// 获得加密块加密后块大小
// System.out.println("加密块大小:"+outputSize);
int leavedSize = data.length % blockSize;
// System.out.println("leavedSize:"+leavedSize);
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1 : data.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (data.length - i * blockSize > 0) {
if (data.length - i * blockSize > blockSize)
cipher.doFinal(data, i * blockSize, blockSize, raw, i * outputSize);
else
cipher.doFinal(data, i * blockSize, data.length - i * blockSize, raw, i * outputSize);
// 这里面doUpdate方法不可用,查看源代码后发现每次doUpdate后并没有什么实际动作除了把byte[]放到ByteArrayOutputStream中
// 而最后doFinal的时候才将所有的byte[]进行加密,可是到了此时加密块大小很可能已经超出了OutputSize所以只好用dofinal方法。
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}

/**
* 解密
* @param key 解密的密钥
* @param raw 已经加密的数据
* @return 解密后的明文
* @throws Exception
*/
@SuppressWarnings("static-access")
public byte[] decrypt(Key key, byte[] raw) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(cipher.DECRYPT_MODE, key);
int blockSize = cipher.getBlockSize();
ByteArrayOutputStream bout = new ByteArrayOutputStream(64);
int j = 0;
while (raw.length - j * blockSize > 0) {
bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
j++;
}
return bout.toByteArray();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}

/**
* 返回公钥
* @return
* @throws Exception
*/
public RSAPublicKey getRSAPublicKey() throws Exception{
//获取公钥
RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
//获取公钥系数(字节数组形式)
byte[] pubModBytes = pubKey.getModulus().toByteArray();
//返回公钥公用指数(字节数组形式)
byte[] pubPubExpBytes = pubKey.getPublicExponent().toByteArray();
//生成公钥
RSAPublicKey recoveryPubKey = this.generateRSAPublicKey(pubModBytes,pubPubExpBytes);
return recoveryPubKey;
}

/**
* 获取私钥
* @return
* @throws Exception
*/
public RSAPrivateKey getRSAPrivateKey() throws Exception{
// 获取私钥
RSAPrivateKey priKey = (RSAPrivateKey) keyPair.getPrivate();
// 返回私钥系数(字节数组形式)
byte[] priModBytes = priKey.getModulus().toByteArray();
// 返回私钥专用指数(字节数组形式)
byte[] priPriExpBytes = priKey.getPrivateExponent().toByteArray();
// 生成私钥
RSAPrivateKey recoveryPriKey = this.generateRSAPrivateKey(priModBytes,priPriExpBytes);
return recoveryPriKey;
}


/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RSAUtil rsa = new RSAUtil();
String str = "天龙八部、神雕侠侣、射雕英雄传白马啸西风";
RSAPublicKey pubKey = rsa.getRSAPublicKey();
RSAPrivateKey priKey = rsa.getRSAPrivateKey();
// System.out.println("加密后==" + new String(rsa.encrypt(pubKey,str.getBytes())));
String mw = new String(rsa.encrypt(pubKey, str.getBytes()));
System.out.println("加密后:"+mw);
// System.out.println("解密后:");
System.out.println("解密后==" + new String(rsa.decrypt(priKey,rsa.encrypt(pubKey,str.getBytes()))));
}
}

1、初始化密钥 构建密钥对,生成公钥、私钥保存到keymap中
KeyPairGenerator --->KeyPair-->RSAPublicKey、RSAPrivateKey
2、甲方使用私钥加密, 加密后在用私钥对加密数据进行数据签名,然后发送给乙方
RSACoder.encryptByPrivateKey(data, privateKey);
RSACoder.sign(encodedData, privateKey);
3、乙方则通过公钥验证签名的加密数据,如果验证正确则在通过公钥对加密数据进行解密
RSACoder.verify(encodedData, publicKey, sign);
RSACoder.decryptByPublicKey(encodedData, publicKey);
4、乙方在通过公钥加密发送给甲方
RSACoder.encryptByPublicKey(decodedData, publicKey);
5、甲方通过私钥解密该数据
RSACoder.decryptPrivateKey(encodedData, privateKey);

代码如下,需要依赖一个jar包commons-codec-1.9.jar,用于Base64转换,请自行下载。

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

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSAUtils {

    // 加密方式
    public static final String ALGORITHM = "RSA";
    // 签名算法
    private static final String SIGNATURE_ALGORITHM = "SHA1WithRSA";
    // 创建密钥对初始长度
    private static final int KEY_SIZE = 512;
    // 字符编码格式
    private static final String CHARSET = "UTF-8";
    // RSA最大加密明文大小
    private static final int MAX_ENCRYPT_BLOCK = 117;
    // RSA最大解密密文大小
    private static final int MAX_DECRYPT_BLOCK = 128;

    private KeyFactory keyFactory;

    public RSAUtils() throws NoSuchAlgorithmException {
        keyFactory = KeyFactory.getInstance(ALGORITHM);
    }

    /**
     * 私钥加密
     *
     * @param content    待加密字符串
     * @param privateKey 私钥
     * @return 加密后字符串(BASE64编码)
     */
    public String encryptByPrivateKey(String content, String privateKey) throws Exception {
        String result;

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] keyBytes = new Base64().decode(privateKey);
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey pKey = keyFactory.generatePrivate(pkcs8KeySpec);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, pKey);

            byte[] data = content.getBytes(CHARSET);
            write2Stream(cipher, data, out);
            byte[] resultBytes = out.toByteArray();

            result = Base64.encodeBase64String(resultBytes);
        } catch (Exception e) {
            throw new Exception(e);
        }

        return result;
    }

    /**
     * 公钥解密
     *
     * @param content   已加密字符串(BASE64加密)
     * @param publicKey 公钥
     * @return
     */
    public String decryptByPublicKey(String content, String publicKey) throws Exception {
        String result = "";

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] keyBytes = new Base64().decode(publicKey);
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
            PublicKey pKey = keyFactory.generatePublic(x509KeySpec);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, pKey);

            byte[] data = Base64.decodeBase64(content);
            write2Stream(cipher, data, out);
            byte[] resultBytes = out.toByteArray();

            result = new String(resultBytes);
        } catch (Exception e) {
            throw new Exception(e);
        }

        return result;
    }

    /**
     * 公钥加密
     *
     * @param content   待加密字符串
     * @param publicKey 公钥
     * @return 加密后字符串(BASE64编码)
     */
    public String encryptByPublicKey(String content, String publicKey) throws Exception {
        String result = "";

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] keyBytes = new Base64().decode(publicKey);
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
            PublicKey pKey = keyFactory.generatePublic(x509KeySpec);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, pKey);

            byte[] data = content.getBytes(CHARSET);
            write2Stream(cipher,
                    data, out);
            byte[] resultBytes = out.toByteArray();
            result = Base64.encodeBase64String(resultBytes);
        } catch (Exception e) {
            throw new Exception(e);
        }

        return result;
    }

    /**
     * 私钥解密
     *
     * @param content    已加密字符串
     * @param privateKey 私钥
     * @return 解密后字符串
     */
    public String decryptByPrivateKey(String content, String privateKey) throws Exception {
        String result = "";

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] keyBytes = new Base64().decode(privateKey);
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey pKey = keyFactory.generatePrivate(pkcs8KeySpec);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, pKey);

            byte[] data = Base64.decodeBase64(content);
            write2Stream(cipher, data, out);
            byte[] resultBytes = out.toByteArray();
            result = new String(resultBytes);
        } catch (Exception e) {
            throw new Exception(e);
        }

        return result;
    }

    private static void write2Stream(Cipher cipher, byte[] data, ByteArrayOutputStream out) throws
            BadPaddingException, IllegalBlockSizeException {
        int dataLen = data.length;
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (dataLen - offSet > 0) {
            if (dataLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, dataLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
    }

    /**
     * 用私钥对信息生成数字签名
     *
     * @param data       已加密数据
     * @param privateKey 私钥(BASE64编码)
     * @return sign
     */
    public String sign(String data, String privateKey) throws Exception {
        String result = "";
        try {
            byte[] keyBytes = new Base64().decode(privateKey);
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
            signature.initSign(privateK);
            signature.update(parse2HexStr(data).getBytes(CHARSET));

            result = new Base64().encodeToString(signature.sign());
        } catch (Exception e) {
            throw new Exception(e);
        }
        return result;
    }

    /**
     * 校验数字签名
     *
     * @param data      已加密数据
     * @param publicKey 公钥(BASE64编码)
     * @param sign      数字签名
     * @return
     * @throws Exception
     */
    public boolean verify(String data, String publicKey, String sign) throws Exception {
        boolean result;

        try {
            byte[] keyBytes = new Base64().decode(publicKey);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            PublicKey publicK = keyFactory.generatePublic(keySpec);
            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
            signature.initVerify(publicK);
            signature.update(parse2HexStr(data).getBytes(CHARSET));
            result = signature.verify(new Base64().decode(sign));
        } catch (Exception e) {
            throw new Exception(e);
        }
        return result;
    }

    /**
     * 将二进制转换成16进制
     *
     * @param data
     * @return
     */
    public static String parse2HexStr(String data) throws Exception {
        String result = "";
        try {
            byte[] buf = data.getBytes(CHARSET);

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < buf.length; i++) {
                String hex = Integer.toHexString(buf[i] & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                sb.append(hex.toUpperCase());
            }
            result = sb.toString();
        } catch (UnsupportedEncodingException e) {
            throw new Exception(e);
        }
        return result;
    }

    /**
     * 生成公钥与私钥
     */
    public static void createKey() throws Exception {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
            keyPairGenerator.initialize(KEY_SIZE);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();

            String publicKey = Base64.encodeBase64String(rsaPublicKey.getEncoded());
            String privateKey = Base64.encodeBase64String(rsaPrivateKey.getEncoded());

            System.out.println("publicKey=" + publicKey + "
privateKey=" + privateKey);
        } catch (NoSuchAlgorithmException e) {
            throw new Exception(e);
        }
    }

    public static void main(String[] args) throws Exception {

        String PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKeYGXH6Vz+m+KuL11RDRaNdHB4YQeZgqpJGPRSwBelgvEoHu2/fNs1bmgfJhI8lhr/o/Hy8EFB/I/DDyLcCcU4bCLtxpki8edC+KJR2WvyYfnVmWEe/3q2jSVKRf81868q9Cd3MMfrQPMsY4x0TQ0GtOf/nMSMbAltV2W8J86IfAgMBAAECgYEApYu4lr2SMW3ddJZNvQ42W4g9nfyYG9igpJx8+VJmhIDpfLbmjzsOBwvUupx0NHH9CNQ7k3qxItJzzf+W5C+lesEOAqdO5nahRZsL8BIDoxTEn2j+1GXkzQw3vqPY50xqRnZsoP5TyNNsOM7KYaOoz4VFMdVIFwoT3OKM5z7mxgECQQD51r17WZDSa/kucaH7gCOePxJPS6Ust0eVd5tBOMpFzD/VtziogSIWyhGKkLH0SyTJEe91CCpdpxufLSZgIiN5AkEAq7ojtvU4twak1N1/1qX+t8f5wD8i/8GU702PeCwkGI5ymrARq+W2yCuvU1bouXBhjKHV4KhafKYixdHUMg00VwJAYVUjpLaUESY3gbyLWqvlNHVl8LaLtwwAO17JgXNaei7Ef8JNtHf6i95VTyJn8cCEqEDwhSuVNb8wp6azWKh0IQJBAJHrcT2d0bt0IcvfCynRk0eG3WnGPG8mhu9w8GAk4ecb47YdtmZio5YjyK8AQnCQVdOyEJL9eyY/5XxCeBSvs7ECQQCKQ2f5HLDkkHvc6rlaHCJmqNRCS+CxhoaiaSPYLAac7WXmb614ACLECc86C/nkefTq0SNpUDVbGxVpJi9/FOUf";
        String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnmBlx+lc/pviri9dUQ0WjXRweGEHmYKqSRj0UsAXpYLxKB7tv3zbNW5oHyYSPJYa/6Px8vBBQfyPww8i3AnFOGwi7caZIvHnQviiUdlr8mH51ZlhHv96to0lSkX/NfOvKvQndzDH60DzLGOMdE0NBrTn/5zEjGwJbVdlvCfOiHwIDAQAB";

        RSAUtils rsaUtil = new RSAUtils();
        String encryptByPublicKey = rsaUtil.encryptByPublicKey("你好!", PUBLIC_KEY);
        System.out.println(encryptByPublicKey);
        String decryptByPrivateKey = rsaUtil.decryptByPrivateKey(encryptByPublicKey, PRIVATE_KEY);
        System.out.println(decryptByPrivateKey);
        String encryptByPrivateKey = rsaUtil.encryptByPrivateKey("你好!", PRIVATE_KEY);
        System.out.println(encryptByPrivateKey);
        String decryptByPublicKey = rsaUtil.decryptByPublicKey(encryptByPrivateKey, PUBLIC_KEY);
        System.out.println(decryptByPublicKey);
        String sign = rsaUtil.sign("1234", PRIVATE_KEY);
        System.out.println("sign:" + sign);
        System.out.println(rsaUtil.verify("1234", PUBLIC_KEY, sign));
    }
}



“佛生两树”有什么典故
自从他觉得这种修行,并非正道,便独自离开苦行林中,接受牧女难陀波罗的乳糜供养,恢复到少壮的体力。憍陈如等五人见到这种情形,便认为他受不了苦行的考验,以致道心退堕,非常失望,就离他而去,到达波罗奈国(varanasl)的鹿野苑(Mrgadava)自修苦行去了。 释迦牟尼既已恢复体力,便自入尼连禅河淋浴,洗过去劳形苦志的...

星云大师《生死烦恼》
3.邪见(mithy-di):指否定四谛因果道理的见解。抱持邪见者,不惧恶,不行善,在五见中最为邪恶。 4.见取见(drsti-parmarsa):执著错误的见解以为真实。 5.戒禁取见(sila-vrata-parmarsa):执著不正确的戒律,以为可以达到解脱或升天的果报,这种错误的执著见解称为戒禁取见。 五见与六根本烦恼(贪、嗔、痴、慢...

斯特鲁维地质测量地点测量点
艾农泰基厄同上 - Avasaksa, 上托尔尼奥Tornio - Tornea, 托尔尼奥Korpiharjti - Puolakka, 科尔皮拉赫蒂Lappajarvi - Porlom II, 拉平耶尔德Pietarsaari - Svartvira, 皮赫泰Russi扩展资料斯特鲁维地质测量地点是从挪威到黑海的一组三角测量点,穿过10个国家,总距离为2820公里。

Y开头的女生英文名
y字母开头的女生英文名可以是YOLANDA和YVETTE。YOLANDA(西班牙语)"紫罗兰"。YOLANDA给人的印象是气质高雅、可静可动的女孩、看起来柔弱、却有令人不可亵渎的气质。YVETTE同YVONNE。人们形容YVETTE是美丽的金发法国女子-懂得人情世故,既聪明又友善,只是有点傲慢。英语姓名的一般结构为:名+中间名+姓。如Wil...

谁有《阿甘正传》的英文台词?
Prince Istvan Barossy Nagyavaros. ANN How do you do? (he kisses her hand) (The Master of Ceremonies announces the long German name and title of the next guest.) ANN (holding the woman's hand as she curtsies) Guten Abend. (She greets the man as he kisses her hand.) (The Master ...

子长县18841043632: java 非对称加密算法有哪些 -
诸柱舒安: 1、初始化密钥 构建密钥对,生成公钥、私钥保存到keymap中 KeyPairGenerator ---> KeyPair --> RSAPublicKey、RSAPrivateKey2、甲方使用私钥加密, 加密后在用私钥对加密数据进行数据签名,然后发送给乙方 RSACoder....

子长县18841043632: 怎么用java实现RSA加密 -
诸柱舒安: 流程分析:

子长县18841043632: java script 怎么做rsa签名 -
诸柱舒安: 由于项目要用到非对称加密解密签名校验什么的,于是参考《Java加密解密的艺术》写一个RSA进行加密解密签名及校验的Demo,代码很简单,特此分享!RSA加密解密类:package com.ihep;import java.io.BufferedReader;import java.io....

子长县18841043632: 如何实现用javascript实现rsa加解密 -
诸柱舒安: 具体实现思路如下:1.服务端生成公钥与私钥,保存.2.客户端在请求到登录页面后,随机生成一字符串.3.后此随机字符串作为密钥加密密码,再用从服务端获取到的公钥加密生成的随机字符串.4.将此两段密文传入服务端,服务端用私...

子长县18841043632: 编写程序实现RSA算法对数据进行加密和解密 -
诸柱舒安: RSA算法是抄非对称算法,要配合公钥和私钥机制实现加密和解密,如果常规大数据袭量的加密和解密,还是用对称加2113密算法效率高.5261 你的问题涉及内容很多,建议找《4102精通PKI网络安全认证技术与编程实现》来看,里面对具1653体的应用介绍的很清楚

子长县18841043632: 帮我解释一下RSA算法的原理 -
诸柱舒安: 首先, 找出三个数, p, q, r, 其中 p, q 是两个相异的质数, r 是与 (p-1)(q-1) 互质的数 p, q, r 这三个数便是 private key 接著, 找出 m, 使得 rm == 1 mod (p-1)(q-1 这个 m 一定存在, 因为 r 与 (p-1)(q-1) 互质, 用辗转相除法就可以得...

子长县18841043632: 用java怎么实现CA颁发的数字证书的认证功能 -
诸柱舒安: PKI 目前使用最多的非对称算法是 RSA.对于基于 RSA 的 PKI 而言,CA 应有一对 RSA 的公私钥对,私钥是 CA 的生命,严格保密,而公钥则发布给使用方.CA 签发一张证书的话,主要是使用 CA 的 RSA 私钥对证书进行签名,并将签名结果保存在证书当中.使用者通过 CA 发布的公钥来验证证书中的签名值,就可以确定该证书是否是由该 CA 签发的.自己要做的就是从证书中提取签名数据和用于签名的原始数据,再使用 CA 的公钥验证这个签名就可以了.

子长县18841043632: java中如何在静态方法中实现对非静态方法的调用,请问各位有没有具体的例子. -
诸柱舒安: public class A{ //类A中非静态方法 public void func(){ ...... } //类A中静态方法(主函数) public static void main(String[] args){ A a=new A();//需实例化A的对象后才可以调用A中非静态方法 a.func(); } 如上面这个简单的例子,静态方法对非静...

子长县18841043632: 关于java中rsa的问题 -
诸柱舒安: 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 ...

子长县18841043632: 怎样用Java实现RSA加密 -
诸柱舒安: 提供加密,解密,生成密钥对等方法.?梢愿?模??遣灰??螅?裨蛐?驶岬 keyPairGen.initialize(KEY_SIZE, new SecureRandom()); KeyPair keyPair = keyPairGen.genKeyPair(); return keyPair; } catch (Exception e) { throw new ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网