私钥加密 公钥解密
私钥加密 公钥解密
我方公钥
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCYJiltAnJWUDHXH+qMQdjYe+Rzgl0z53f54s92vh6QMfOtugAYtefG7AadCyCcSqETefqWxMQBYbK7tGZpCx6Mf/1j5RYFz2cYyUg9jwCJnVFyEqDwBRj9hmsLC+qkHOphvdgPa9xC3GIYPVajZJkxnS3xSoWenh4yxCdCIedjgQIDAQAB
我方私钥
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJgmKW0CclZQMdcf6oxB2Nh75HOCXTPnd/niz3a+HpAx8626ABi158bsBp0LIJxKoRN5+pbExAFhsru0ZmkLHox//WPlFgXPZxjJSD2PAImdUXISoPAFGP2GawsL6qQc6mG92A9r3ELcYhg9VqNkmTGdLfFKhZ6eHjLEJ0Ih52OBAgMBAAECgYA1W1S1xODiAheocKnpbAIm0TPpijMzS55YhMDqzHudZ9cj+pGkGM7ihM1A77EM/mMEmIdXrkNqlJ+AkiBEAEFCyOB3gvBH4UGi/ca4x2leI71v5YGmu+8GvSloK0hwtqstxrGQFfVdONJJWf6zq+EsBXsffVNRjQKtr4moVLjqTQJBAPQ0Nr7Nfp8JFesyYwyV1YkveYWXnhyTX5a88N9tkQdR9au9PJ1TvBhnZS3SutuzKWSpZBPsINo2ZyC446/GpMsCQQCff5y+S4bCeoBLoynkq1t1TM2Z0UdUyASnqr7t5MOypt0ySrBTqFvVxfHeUK9xTwEHcG99hYg7YFG5KKoj1NtjAkBCrELKI0bP2uYuGcqrM/liBCE0lSmo+Z+egyVsiviOvBTL99WlyqfPLeOU+DTmKOtDPXBCdDSf/zx21aPsaNqzAkAV7OPuINHAkDhsrVxH6+phoJIuz97/X5OMPOOMLkNcnqWPkUbV6THenBxYlDzZ1aNtk14wIhgNGXuCeX2R+WPDAkEAwInrzqVumqjaOimgEdctnTRiEtnLOyKOGiWQ4AsrB8jRUSEQ2pDHiydPjXVAriy2Xfy20w8GB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PuqfB9omGAeRNtewN5tZZL
我方持有对方的公钥和我方的私钥
对方持有我方公钥和自己的私钥
- 生成公钥私钥对
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(“RSA”);// 用于生成公钥和私钥对
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");// 产生随机数
byte[] seed = random.generateSeed(20);
random = new SecureRandom(seed);
keyGenerator.initialize(1024, random);
KeyPair keyPair = keyGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>();
keyMap.put(“RSAPublicKey”, publicKey);
keyMap.put(“RSAPrivateKey”, privateKey);
私钥进行数字签名
try {
byte[] encodedKey = Base64.decodeBase64(privateKey.getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
PrivateKey key = keyFactory.generatePrivate(keySpec);
Signature signature = Signature.getInstance(“MD5withRSA”);
signature.initSign(key);
signature.update(data);
return new String(Base64.encodeBase64(Base64.encodeBase64(signature.sign())));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("根据私钥对数据要进行签名异常");
}
公钥进行验证
byte[] encodedKey = Base64.decodeBase64(publicKey.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
PublicKey key = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(“MD5withRSA”);
signature.initVerify(key);// 初始化验证
signature.update(data);
return signature.verify(Base64.decodeBase64(Base64.decodeBase64(sign.getBytes())));
上一篇: python 实现凯撒密码加解密