zoukankan      html  css  js  c++  java
  • 软件注册码随笔

    用到的是:BouncyCastle.Crypto.dll类库

    官网:http://www.bouncycastle.org/csharp/

    生成密钥对

     RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator();
                //密钥构造器
                RsaKeyGenerationParameters p = new RsaKeyGenerationParameters(BigInteger.ValueOf(3), new SecureRandom(), 1024, 25);
                //初始化密钥构造器
                keyGenerator.Init(p);
                //密钥对
                AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair();
                //公钥和私钥
                AsymmetricKeyParameter publicKey = keyPair.Public;
                AsymmetricKeyParameter privateKey = keyPair.Private;

    保存密钥对到文件(我这里用*.key形式保存的,当然也可以用其它的)

    1  private static void SavePublicKey(AsymmetricKeyParameter publicKey)
    2         {
    3             SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
    4             Asn1Object ao = publicKeyInfo.ToAsn1Object();
    5             byte[] publicKeyByte = ao.GetEncoded();
    6             FileStream fs = new FileStream(@"D:\public.key", FileMode.Create, FileAccess.Write);
    7             fs.Write(publicKeyByte, 0, publicKeyByte.Length);
    8             fs.Close();
    9         }
    1  private static void SavePrivateKey(AsymmetricKeyParameter privateKey)
    2         {
    3             PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
    4             Asn1Object ao = privateKeyInfo.ToAsn1Object();
    5             byte[] privateKeyByte = ao.GetEncoded();
    6             FileStream fs = new FileStream(@"D:\private.key", FileMode.Create, FileAccess.Write);
    7             fs.Write(privateKeyByte, 0, privateKeyByte.Length);
    8             fs.Close();
    9         }

    软件注册基本结构:

    自己拥有私密:privatekey

    客户拥有公密:publicKey

    1:客户端生成序列号

    2:客户把序列号告诉私密拥有者

    3:私密拥有者用私钥加密信息(时间,版本,或者什么的)

    4:加密后的信息给客户,客户在软件操作,验证

    1,2步骤不用说了,很简单

    直接3,4步骤

    私密加密

    1 IAsymmetricBlockCipher enginePrivate = new RsaEngine();
    2             Asn1Object aobj = Asn1Object.FromStream(new FileStream(@"D:\private.key", FileMode.Open, FileAccess.Read));
    3             PrivateKeyInfo priKey = PrivateKeyInfo.GetInstance(aobj);
    4             AsymmetricKeyParameter privateKey = PrivateKeyFactory.CreateKey(priKey);
    5             enginePrivate.Init(true, privateKey);
    6             byte[] enData = enginePrivate.ProcessBlock(data1, 0, data1.Length);
    7             string tempEnString = ConvertBytesToHexString(enData);

    公密解密

    1  byte[] data = ConvertHexStringToBytes(tempEnString);
    2             IAsymmetricBlockCipher engine = new RsaEngine();
    3             Asn1Object aobject = Asn1Object.FromStream(new FileStream(@"D:\public.key", FileMode.Open, FileAccess.Read));  //a.puk??
    4             SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfo.GetInstance(aobject);
    5             AsymmetricKeyParameter publicKey = (RsaKeyParameters)PublicKeyFactory.CreateKey(pubInfo);
    6             engine.Init(false, publicKey);
    7             engine.GetInputBlockSize();
    8             byte[] deData = engine.ProcessBlock(data, 0, data.Length);
    9             string tempString = byteToHexStr(deData);

    帮助类

     private string byteToHexStr(byte[] bytes)
            {
                string returnStr = "";
    
                if (bytes != null)
                {
    
                    for (int i = 0; i < bytes.Length; i++)
                    {
    
                        returnStr += bytes[i].ToString("X2");
    
                    }
                }
                return returnStr;
            }
            public byte[] ConvertHexStringToBytes(string hexString)
            {
                int len = hexString.Length / 2;
                byte[] bytes = new byte[len];
                for (int i = 0; i < len; i++)
                {
                    bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                }
                return bytes;
            }
            public string ConvertBytesToHexString(byte[] bytes)
            {
                StringBuilder sb = new StringBuilder();
                foreach (byte b in bytes)
                {
                    sb.Append(string.Format("{0:X2}", b).PadLeft(2, '0'));
                }
                return sb.ToString();
            }

    直接看代码,有些东西是自己要研究的,

     

  • 相关阅读:
    Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题
    java JDK JRE 1.6,1.7,1.8各个版本版本下载链接
    window 10 企业版激活
    IntelliJ IDEA 缺少 javax 包 支持
    IntelliJ Idea 2017 免费激活方法
    url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介【转】
    【转载】 CUDA_DEVICE_ORDER 环境变量说明 ( ---------- tensorflow环境下的应用 )
    【转载】 TensorFlow tf.app&tf.app.flags用法介绍
    中国知网(cnki)上caj格式转pdf的方法 ----------------- 转载
    同时购入两台同款thinkpad笔记本电脑,分别使用同一账户激活office失败--------------解决方法(账户下有多个Office激活信息,重装后提示“许可证不正确或者最大激活次数”)
  • 原文地址:https://www.cnblogs.com/macheal/p/3066086.html
Copyright © 2011-2022 走看看