zoukankan      html  css  js  c++  java
  • java RSA签名

            try{
                //1初始化秘钥
                KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
                keyPairGenerator.initialize(512);
                KeyPair keyPair = keyPairGenerator.generateKeyPair();
                RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
                RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
                
                //2执行签名
                PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
                Signature signature = Signature.getInstance("MD5withRSA");
                signature.initSign(privateKey);
                signature.update(src.getBytes());
                byte[] result = signature.sign();
                System.out.println("rsa sign:" + Hex.encodeHexString(result));
                
                
                //3.验证签名
                
                X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
                keyFactory = keyFactory.getInstance("RSA");
                PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
                signature = Signature.getInstance("MD5withRSA");
                signature.initVerify(publicKey);
                signature.update(src.getBytes());
                boolean bool = signature.verify(result);
                System.out.println("rsa verify " + bool);
            }
            
            catch(Exception ex){
                ex.printStackTrace();
            }
  • 相关阅读:
    Ubuntu 系统下载
    Shell 变量自增实现方法
    shell 单引号以及双引号
    A look at WeChat security
    利用HTTP Cache来优化网站
    require.js 简洁入门
    Nginx rewrite URL examples with and without redirect address
    页面制作部分之PS切图
    Joda-Time 简介
    [官方摘要]Setup And Configuration memcached with Tomcat
  • 原文地址:https://www.cnblogs.com/rubekid/p/5243864.html
Copyright © 2011-2022 走看看