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();
            }

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

     

  • 相关阅读:
    Script:List NLS Parameters and Timezone
    Script:List Buffer Cache Details
    Know about RAC Clusterware Process OPROCD
    RAC Deadlock For Example
    Know more about redo log buffer and latches
    如何设计分区索引
    SCN may jump in a distributed transaction with dblink
    Script to Collect Log File Sync Diagnostic Information (lfsdiag.sql)
    Oracle学习笔记:10046 SQL tracle event
    Oracle学习笔记:创建physical standby
  • 原文地址:https://www.cnblogs.com/macheal/p/3066086.html
Copyright © 2011-2022 走看看