精华内容
下载资源
问答
  • RSA加密解密

    2018-09-20 11:38:30
    RSA加密解密算法,: 加密:RSAUtil.encryptByPublic("明文字符串","公钥"); 解密:RSAUtil.decryptByPrivate("加密字符串", "密钥");
  • rsa加密解密

    2018-06-06 13:57:41
    RSA加密解密,html+css+js编写,如果有不懂的话可以私我。很基础
  • RSA 加密解密

    千次阅读 2019-12-05 15:48:15
    RSA 加密解密实现代码如下: package com.cictec.network.bus.barcodecheck; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import javax.crypto.Cipher; ...

    RSA 加密解密实现代码如下:

    package com.barcodecheck;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    
    import javax.crypto.Cipher;
    import java.security.*;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.util.Arrays;
    import java.util.Base64;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author :zoboy
     * @Description:
     * @ Date: Created in 2019-12-05 09:28
     */
    public class RSADemo {
        static String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM/bj9+o9pQobuHKRCIuFm2fky8cAvmE1aiAsG/fGLm0zqVGt7M9DAHEVcLd3WVPPVSFLGvp9ysRm565vGrU4HkCAwEAAQ==";
        static String privateKey = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAz9uP36j2lChu4cpEIi4WbZ+TLxwC+YTVqICwb98YubTOpUa3sz0MAcRVwt3dZU89VIUsa+n3KxGbnrm8atTgeQIDAQABAkEAm62pw21sSSlDaw8wGp2EJNTYyvbi73ljARJpk1B311XPlsrfJJzDEGpHmqoenXnKTtrFK+OOLGWEEwPsJWY+gQIhAO6WrONtzxmc9CF3TgpD8c05XpKdQKHlvHNXcnCzn5bRAiEA3wbFJiQFFOXzeelDOVw+xwdKO4o4eyPx67AX4ElKaSkCIBdw+GWT+WAT2qybEzDRAiXeuBsBlkMR1lUix1ypWUmxAiEAonSSAxhVw0VFN1Zcq1mwONXskrY6Miiave2FVtDMLRECIDWX4aXKHKDpu7HwUYwsa7o/bbG7POMsCoeMkKhUxAN9";
    
        public static void main(String[] args) {
            initKey();
            Map map = new HashMap();
            map.put("requestId", "1231");
            map.put("companyCode", "10");
            map.put("busNum", "陕A07157D");
            map.put("svrReqTime", "1575422395");
            map.put("classesMD5Code", "");
            map.put("lineNum", "123569");
            String result11 = encrypt(map);
            decrypt(result11);
        }
        public static void rsaPrivateKeyInfo(RSAPrivateKey rsaPrivateKey){
            System.out.println("Private Key : " + privateKey);
            System.out.println("Private Key Mod: " + rsaPrivateKey.getModulus());
            System.out.println("Private Key Mod length: " + rsaPrivateKey.getModulus().bitLength());
            System.out.println("Private Key Exp: " + rsaPrivateKey.getPrivateExponent());
            System.out.println("Private format:" + rsaPrivateKey.getFormat());
            System.out.println("Private Key Algorithm: " +  rsaPrivateKey.getAlgorithm());
        }
        public static void rsaPublicKeyInfo(RSAPublicKey rsaPublicKey){
            System.out.println("Public Key : " + publicKey);
            System.out.println("Public Key Mod: " + rsaPublicKey.getModulus());
            System.out.println("Public Key Mod length: " + rsaPublicKey.getModulus().bitLength());
            System.out.println("Public Key Exp: " + rsaPublicKey.getPublicExponent());
            System.out.println("Public Key Algorithm: " +  rsaPublicKey.getAlgorithm());
            System.out.println("Public format:" + rsaPublicKey.getFormat());
        }
    
        public static void initKey() {
            // 1、初始化密钥
            KeyPairGenerator keyPairGenerator;
            try {
                keyPairGenerator = KeyPairGenerator.getInstance("RSA");
                keyPairGenerator.initialize(512);// 64的整倍数
                KeyPair keyPair = keyPairGenerator.generateKeyPair();
                RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
                RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
                publicKey = Base64.getEncoder().encodeToString(rsaPublicKey.getEncoded());
                privateKey = Base64.getEncoder().encodeToString(rsaPrivateKey.getEncoded());
                rsaPublicKeyInfo(rsaPublicKey);
                rsaPrivateKeyInfo(rsaPrivateKey);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
    
        public static String encrypt(Object obj) {
    
            //2 私钥加密,公钥解密   --加密
            try {
                String encryptStr = JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
                byte[] result1 = encryptStr.getBytes("utf-8");
                byte[] buffer = Base64.getDecoder().decode(privateKey);
                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
                KeyFactory instance = KeyFactory.getInstance("RSA");
                RSAPrivateKey key = (RSAPrivateKey) instance.generatePrivate(keySpec);
                rsaPrivateKeyInfo(key);
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                int inputLength = result1.length;
                System.out.println("加密字节数:" + inputLength);
    
                int MAX_ENCRYPT_BLOCK = key.getModulus().bitLength()/8-11;
                // 标识
                int offSet = 0;
                byte[] resultBytes = {};
                byte[] cache = {};
                while (inputLength - offSet > 0) {
                    if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
                        cache = cipher.doFinal(result1, offSet, MAX_ENCRYPT_BLOCK);
                        offSet += MAX_ENCRYPT_BLOCK;
                    } else {
                        cache = cipher.doFinal(result1, offSet, inputLength - offSet);
                        offSet = inputLength;
                    }
                    resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
                    System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
                }
    
                String result11 = Base64.getEncoder().encodeToString(resultBytes);
                System.out.println("私钥加密,公钥解密   --加密: " + result11);
                return result11;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static void decrypt(String ss) {
            //3 私钥加密,公钥解密   --解密
            try {
                byte[] buffer = Base64.getDecoder().decode(publicKey);
                byte[] result1 = Base64.getDecoder().decode(ss);
                X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(buffer);
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
                rsaPublicKeyInfo(publicKey);
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE, publicKey);
                int inputLength = result1.length;
                int MAX_ENCRYPT_BLOCK =  publicKey.getModulus().bitLength()/8;
                // 标识
                int offSet = 0;
                byte[] resultBytes = {};
                byte[] cache = {};
                while (inputLength - offSet > 0) {
                    if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
                        cache = cipher.doFinal(result1, offSet, MAX_ENCRYPT_BLOCK);
                        offSet += MAX_ENCRYPT_BLOCK;
                    } else {
                        cache = cipher.doFinal(result1, offSet, inputLength - offSet);
                        offSet = inputLength;
                    }
                    resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
                    System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
                }
    
                System.out.println("私钥加密,公钥解密   --解密: " + javax.xml.bind.DatatypeConverter.printHexBinary(resultBytes));
                System.out.println("私钥加密,公钥解密   --解密: " + new String(resultBytes, "utf-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    

     

    展开全文
  • php rsa加密解密实例,rsa加密解密实例1、加密解密的第一步是生成公钥、私钥对,私钥加密的内容能通过公钥解密(反过来亦可以)下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,...

    php rsa加密解密实例,rsa加密解密实例

    1、加密解密的第一步是生成公钥、私钥对,私钥加密的内容能通过公钥解密(反过来亦可以)

    下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin目录,执行以下命令:

    openssl genrsa -out rsa_private_key.pem 1024

    openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pem

    openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

    第一条命令生成原始 RSA私钥文件 rsa_private_key.pem,第二条命令将原始 RSA私钥转换为 pkcs8格式,第三条生成RSA公钥 rsa_public_key.pem

    从上面看出通过私钥能生成对应的公钥,因此我们将私钥private_key.pem用在服务器端,公钥发放给android跟ios等前端

    2、php中用生成的公钥、私钥进行加密解密,直接上代码

    $fp=fopen("rsa/rsa_private_key.pem","r"); //你的私钥文件路径

    $private_key=fread($fp,8192);

    fclose($fp);

    $fp1=fopen("rsa/rsa_public_key.pem","r"); //你的公钥文件路径

    $public_key=fread($fp1,8192);

    fclose($fp1);

    //echo $private_key;

    $pi_key=openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id

    $pu_key=openssl_pkey_get_public($public_key );//这个函数可用来判断公钥是否是可用的

    print_r($pi_key);echo "\n"; echo "

    ";

    print_r($pu_key);echo "\n"; echo "

    ";

    echo "";

    $data='php ras加密算法';

    $encrypted = "";

    $decrypted = "";

    echo "加密的源数据:".$data."\n"; echo "

    ";

    echo "private key encrypt:\n"; echo "

    ";

    openssl_private_encrypt($data,$encrypted,$pi_key);//私钥加密

    $encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的

    echo '私钥加密后:'.$encrypted."\n"; echo "

    ";echo "

    ";

    echo "public key decrypt:\n"; echo "

    ";

    openssl_public_decrypt(base64_decode($encrypted),$decrypted,$pu_key);//私钥加密的内容通过公钥可用解密出来

    echo '公钥解密后:'.$decrypted."\n"; echo "

    ";

    echo "";

    echo "public key encrypt:\n"; echo "

    ";

    openssl_public_encrypt($data,$encrypted,$pu_key);//公钥加密

    $encrypted = base64_encode($encrypted);

    echo $encrypted,"\n"; echo "

    ";

    echo "private key decrypt:\n"; echo "

    ";

    openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key);//私钥解密

    echo $decrypted,"\n"; echo "

    ";

    PHP的RSA配置常见问题:

    ●PHP开发语言的代码示例中openssl文件夹中的3个DLL文件用法

    1、如果你的系统是windows系统,且system32文件目录下没有libeay32.dll、ssleay32.dll这两个文件

    那么需要拷贝这两个文件到system32文件目录。

    2、如果您的php安装目录下(php\ext)中没有php_openssl.dll

    那么请把php_openssl.dll放在这个文件夹中

    http://www.bkjia.com/PHPjc/942279.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/942279.htmlTechArticlephp rsa加密解密实例,rsa加密解密实例 1、加密解密的第一步是生成公钥、私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 下载开...

    本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉 本文系统来源:php中文网

    展开全文
  • python操作RSA加密解密#!/usr/bin/env python3# -*- coding: UTF-8 -*-# Author:LBLfrom Cryptodome.PublicKey import RSAfrom Cryptodome.Cipher import PKCS1_OAEPdef generate_key():key = RSA.generate(1024)...

    python操作RSA加密解密

    #!/usr/bin/env python3

    # -*- coding: UTF-8 -*-

    # Author:LBL

    from Cryptodome.PublicKey import RSA

    from Cryptodome.Cipher import PKCS1_OAEP

    def generate_key():

    key = RSA.generate(1024)

    private_key = key.export_key()

    print(private_key)

    with open('private.pem', 'wb') as f:

    f.write(private_key)

    public_key = key.publickey().export_key()

    print(public_key)

    with open('public_key.pem', 'wb') as f:

    f.write(public_key)

    # 实例化生成密钥对文件

    # generate_key()

    # 加密

    def encrypt(data):

    # 导入公钥

    public_key = RSA.import_key(open('public_key.pem').read())

    # 加密对象

    cipher = PKCS1_OAEP.new(public_key)

    # 加密

    msg = cipher.encrypt(data)

    return msg

    # 解密

    def decrypt(data):

    # 导入私钥

    private_key = RSA.import_key(open('private.pem').read())

    cipher = PKCS1_OAEP.new(private_key)

    res = cipher.decrypt(data)

    return res

    if __name__ == '__main__':

    # 加密

    data = '我是六六'.encode()

    msg = encrypt(data)

    print(msg)

    # 解密

    res = decrypt(msg)

    print(res)

    print(res.decode('utf-8'))

    展开全文
  • /usr/bin/env python#encoding=utf-8'''测试rsa加密解密'''from M2Crypto import RSAmsg = 'aaaa-aaaa'rsa_pub = RSA.load_pub_key('rsa_pub.pem')rsa_pri = RSA.load_key('rsa_pri.pem')print '*******************...

    代码:

    #!/usr/bin/env python

    #encoding=utf-8

    '''

    测试rsa加密解密

    '''

    from M2Crypto import RSA

    msg = 'aaaa-aaaa'

    rsa_pub = RSA.load_pub_key('rsa_pub.pem')

    rsa_pri = RSA.load_key('rsa_pri.pem')

    print '*************************************************************'

    print '公钥加密,私钥解密'

    ctxt = rsa_pub.public_encrypt(msg, RSA.pkcs1_padding)

    ctxt64 = ctxt.encode('base64')

    print ('密文:%s'% ctxt64)

    rsa_pri = RSA.load_key('rsa_pri.pem')

    txt = rsa_pri.private_decrypt(ctxt, RSA.pkcs1_padding)

    print('明文:%s'% txt)

    print '*************************************************************'

    print '私钥加密,公钥解密'

    ctxt_pri = rsa_pri.private_encrypt(msg, RSA.pkcs1_padding)

    ctxt64_pri = ctxt.encode('base64')

    print ('密文:%s'% ctxt64_pri)

    txt_pri = rsa_pub.public_decrypt(ctxt_pri, RSA.pkcs1_padding)

    print('明文:%s'% txt_pri)

    **********************************************************************************************

    库的安装说明

    M2Crypto库的下载地址:

    https://github.com/martinpaljak/M2Crypto

    依赖的库:openssh-devel gcc swig (这3个库在centos上可以直接使用yum安装)

    展开全文
  • java RSA加密解密

    2017-04-10 11:50:30
    java RSA加密解密
  • RSA加密解密算法.ppt

    2020-09-02 08:02:48
    RSA 加密解密算法 1.RSA 算法的基本概念 2.RSA 加密算法实现 3.RSA 解密算法实现 4. 遇到的问题 5. 总结 RSA 加密解密算法基本概念 RSA 公钥加密算法是 1977 年由 Ron Rivest Adi Shamirh 和 LenA dleman 开发的 RSA...
  • RSA加密解密demo.zip

    2021-04-16 11:00:53
    PHP RSA加密解密Demo
  • C# RSA加密解密

    2019-04-15 09:59:15
    C#做的RSA加密解密, 带winform界面, 测试通过, 代码完整。
  • 要用nodejs开发接口,实现远程调用,如果裸奔太危险了,就在网上找了一下nodejs的加密,感觉node-rsa挺不错的,下面来总结一下简单的rsa加密解密用法,需要的朋友可以参考下
  • iOS RSA 加密解密

    2016-08-24 18:11:54
    iOS RSA 加密解密

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 10,628
精华内容 4,251
关键字:

rsa加密解密