首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用公钥加密技术加密字符串

如何使用公钥加密技术加密字符串
EN

Stack Overflow用户
提问于 2014-06-17 15:08:33
回答 3查看 2.3K关注 0票数 2

我正在尝试实现我自己的RSA加密引擎。给定这些RSA算法值:

代码语言:javascript
复制
p = 61. // A prime number.
q = 53. // Also a prime number.
n = 3233. // p * q.
totient = 3120. // (p - 1) * (q - 1)
e = 991. // Co-prime to the totient (co-prime to 3120).
d = 1231. // d * e = 1219921, which is equal to the relation where 1 + k * totient = 1219921 when k = 391.

我试图编写一个方法来加密字符串中的每个字节,并返回一个加密的字符串:

代码语言:javascript
复制
public string Encrypt(string m, Encoding encoding)
{
    byte[] bytes = encoding.GetBytes(m);
    for (int i = 0; i < bytes.Length; i++)
    {
        bytes[i] = (byte)BigInteger.ModPow(bytes[i], e, n);
    }
    string encryptedString = encoding.GetString(bytes);
    Console.WriteLine("Encrypted {0} as {1}.", m, encryptedString);
    return encryptedString;
}

这里最明显的问题是,BigInteger.ModPow(bytes[i], e, n)可能太大,无法容纳一个字节空间;它可能导致8位以上的值。如何在仍能将加密的字节串解密回常规字符串的情况下解决此问题?

Update:即使从byte[]加密到byte[],您也会遇到这样的情况:使用RSA算法加密该字节超出了字节的大小限制:

代码语言:javascript
复制
public byte[] Encrypt(string m, Encoding encoding)
{
    byte[] bytes = encoding.GetBytes(m);
    for (int i = 0; i < bytes.Length; i++)
    {
        bytes[i] = (byte)BigInteger.ModPow(bytes[i], e, n);
    }
    return bytes;
}

Update:我的问题是加密会导致比初始输入字符串更多的字节:

代码语言:javascript
复制
public byte[] Encrypt(string m, Encoding encoding)
{
    byte[] bytes = encoding.GetBytes(m);
    byte[] returnBytes = new byte[0];
    for (int i = 0; i < bytes.Length; i++)
    {
        byte[] result = BigInteger.ModPow(bytes[i], (BigInteger)e, n).ToByteArray();
        int preSize = returnBytes.Length;
        Array.Resize(ref returnBytes, returnBytes.Length + result.Length);
        result.CopyTo(returnBytes, preSize);
    }
    return returnBytes;
}

public string Decrypt(byte[] c, Encoding encoding)
{
    byte[] returnBytes = new byte[0];
    for (int i = 0; i < c.Length; i++)
    {
        byte[] result = BigInteger.ModPow(c[i], d, n).ToByteArray();
        int preSize = returnBytes.Length;
        Array.Resize(ref returnBytes, returnBytes.Length + result.Length);
        result.CopyTo(returnBytes, preSize);
    }
    string decryptedString = encoding.GetString(returnBytes);
    return decryptedString;
}

如果您像这样运行这个代码:

代码语言:javascript
复制
byte[] encryptedBytes = engine.Encrypt("Hello, world.", Encoding.UTF8);
Console.WriteLine(engine.Decrypt(encryptedBytes, Encoding.UTF8));

产出如下:

代码语言:javascript
复制
?♥D
?♥→☻►♦→☻►♦oD♦8? ?♠oj?♠→☻►♦;♂?♠♂♠?♠

显然,输出不是原始字符串,因为我不能一次解密每个字节,因为有时两个或更多字节的密码-文本表示一个整数的值,我需要将其解密回原始string...so的一个字节,我想知道处理这个问题的标准机制是什么。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-17 19:21:26

注意:我更新了这个答案。请向下滚动到更新,了解如何实际实现它,因为第一种方法并不是执行RSA加密的正确方式。

我认为这样做是一种方法(但可能不符合标准),而且请注意,这并不符合标准:

代码语言:javascript
复制
public byte[] Encrypt(string m, Encoding encoding)
{
    byte[] bytes = encoding.GetBytes(m);
    byte[] returnBytes = new byte[0];
    for (int i = 0; i < bytes.Length; i++)
    {
        byte[] result = BigInteger.ModPow(bytes[i], (BigInteger)e, n).ToByteArray();
        int preSize = returnBytes.Length;
        Array.Resize(ref returnBytes, returnBytes.Length + result.Length + 1);
        (new byte[] { (byte)(result.Length) }).CopyTo(returnBytes, preSize);
        result.CopyTo(returnBytes, preSize + 1);
    }
    return returnBytes;
}

public string Decrypt(byte[] c, Encoding encoding)
{
    byte[] returnBytes = new byte[0];
    for (int i = 0; i < c.Length; i++)
    {
        int dataLength = (int)c[i];
        byte[] result = new byte[dataLength];
        for (int j = 0; j < dataLength; j++)
        {
            i++;
            result[j] = c[i];
        }
        BigInteger integer = new BigInteger(result);
        byte[] integerResult = BigInteger.ModPow(integer, d, n).ToByteArray();
        int preSize = returnBytes.Length;
        Array.Resize(ref returnBytes, returnBytes.Length + integerResult.Length);
        integerResult.CopyTo(returnBytes, preSize);
    }
    string decryptedString = encoding.GetString(returnBytes);
    return decryptedString;
}

这有可能成为跨平台的,因为您可以选择使用不同的数据类型来表示e或n,并将其传递给这样的C#后端服务。下面是一个测试:

代码语言:javascript
复制
string stringToEncrypt = "Mary had a little lamb.";
Console.WriteLine("Encrypting the string: {0}", stringToEncrypt);
byte[] encryptedBytes = engine.Encrypt(stringToEncrypt, Encoding.UTF8);
Console.WriteLine("Encrypted text: {0}", Encoding.UTF8.GetString(encryptedBytes));
Console.WriteLine("Decrypted text: {0}", engine.Decrypt(encryptedBytes, Encoding.UTF8));

输出:

代码语言:javascript
复制
Encrypting the string: Mary had a little lamb.
Encrypted text: ☻6☻1♦☻j☻☻&♀☻g♦☻t☻☻1♦☻?  ☻g♦☻1♦☻g♦☻?♥☻?☻☻7☺☻7☺☻?♥☻?♂☻g♦☻?♥☻1♦☻$☺☻
c       ☻?☻
Decrypted text: Mary had a little lamb.

更新:我之前说的一切都是完全错误的。错,错,错!这是进行RSA加密的正确方法:

  • 将字符串转换为BigInteger数据类型。
  • 确保您的整数小于为您的算法计算的n的值,否则您将无法对其进行解码。
  • 加密整数。RSA仅用于整数加密。这很清楚。
  • 将其从加密整数中解密。
  • 我不禁想知道,BigInteger类主要是为密码学创建的。

例如:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace BytePadder
{
    class Program
    {
        const int p = 61;
        const int q = 53;
        const int n = 3233;
        const int totient = 3120;
        const int e = 991;
        const int d = 1231;

        static void Main(string[] args)
        {
            // ---------------------- RSA Example I ----------------------
            // Shows how an integer gets encrypted and decrypted.
            BigInteger integer = 1000;
            BigInteger encryptedInteger = Encrypt(integer);
            Console.WriteLine("Encrypted Integer: {0}", encryptedInteger);
            BigInteger decryptedInteger = Decrypt(encryptedInteger);
            Console.WriteLine("Decrypted Integer: {0}", decryptedInteger);
            // --------------------- RSA Example II ----------------------
            // Shows how a string gets encrypted and decrypted.
            string unencryptedString = "A";
            BigInteger integer2 = new BigInteger(Encoding.UTF8.GetBytes(unencryptedString));
            Console.WriteLine("String as Integer: {0}", integer2);
            BigInteger encryptedInteger2 = Encrypt(integer2);
            Console.WriteLine("String as Encrypted Integer: {0}", encryptedInteger2);
            BigInteger decryptedInteger2 = Decrypt(encryptedInteger2);
            Console.WriteLine("String as Decrypted Integer: {0}", decryptedInteger2);
            string decryptedIntegerAsString = Encoding.UTF8.GetString(decryptedInteger2.ToByteArray());
            Console.WriteLine("Decrypted Integer as String: {0}", decryptedIntegerAsString);
            Console.ReadLine();
        }

        static BigInteger Encrypt(BigInteger integer)
        {
            if (integer < n)
            {
                return BigInteger.ModPow(integer, e, n);
            }
            throw new Exception("The integer must be less than the value of n in order to be decypherable!");
        }

        static BigInteger Decrypt(BigInteger integer)
        {
            return BigInteger.ModPow(integer, d, n);
        }
    }
}

示例输出:

代码语言:javascript
复制
Encrypted Integer: 1989
Decrypted Integer: 1000
String as Integer: 65
String as Encrypted Integer: 1834
String as Decrypted Integer: 65
Decrypted Integer as String: A
票数 1
EN

Stack Overflow用户

发布于 2014-06-17 16:52:27

您用于加密和解密每个字节的基本代码--对ModPow的调用--正在工作,但您正在进行“拆分消息并加密每个字节”的错误操作。

为了显示ModPow部分--即数学部分--很好,下面是基于您的代码,它将string加密到BigInteger[]并返回:

代码语言:javascript
复制
using System;
using System.Linq;
using System.Numerics;
using System.Text;

class Test
{
    const int p = 61;
    const int q = 53;
    const int n = 3233;
    const int totient = 3120;
    const int e = 991;
    const int d = 1231;

    static void Main()
    {
        var encrypted = Encrypt("Hello, world.", Encoding.UTF8);
        var decrypted = Decrypt(encrypted, Encoding.UTF8);
        Console.WriteLine(decrypted);
    }

    static BigInteger[] Encrypt(string text, Encoding encoding)
    {
        byte[] bytes = encoding.GetBytes(text);
        return bytes.Select(b => BigInteger.ModPow(b, (BigInteger)e, n))
                    .ToArray();
    }

    static string Decrypt(BigInteger[] encrypted, Encoding encoding)
    {
        byte[] bytes = encrypted.Select(bi => (byte) BigInteger.ModPow(bi, d, n))
                                .ToArray();
        return encoding.GetString(bytes);
    }
}

接下来,您需要了解更多关于byte[]是如何使用RSA加密到另一个byte[]的,包括所有不同的填充方案等等。其中不仅仅是对每个字节调用ModPow

但是要重申的是,您不应该这样做而最终得到一个生产RSA实现。你没有任何安全漏洞的机会是非常渺茫的。为了学术兴趣,更多地了解密码学的原理,但把真正的实现留给专家,这样做是没问题的。(我远不是这个领域的专家-我不可能开始实施自己的加密.)

票数 2
EN

Stack Overflow用户

发布于 2014-06-17 16:46:07

如果您希望在C#中使用RSA加密,那么您不应该试图构建自己的加密。首先,你选择的素数可能很小。P和q应该是大素数。

你应该看看其他一些问题/答案:

如何使用RSA加密C#中的文件(海量数据)

C#中大数据的RSA加密

及其他参考资料:http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.encrypt(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24267332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档