65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 RSA 算法进行图像加密 (C#)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.95/5 (28投票s)

2014 年 2 月 8 日

CPOL

5分钟阅读

viewsIcon

210175

downloadIcon

7760

使用 RSA 算法进行图像加密 (C#)

引言

本文的项目中使用了图像加密的概念。该项目是用 Visual Studio 2010 C#.NET 平台制作的。如今,多媒体技术中传输数据的隐私和安全问题备受关注,因此本项目旨在理解加密和解密是如何发生的?

在此项目中,我们使用密码学来隐藏图像。其中,使用了 RSA(Ron Rivest、Adi Shamir 和 Leonard Adleman)算法。

加密过程

解密过程

主要概念

在此项目中,将图像作为输入,并在属性部分显示图像属性,如文件名、图像分辨率、图像大小。加载图像后,HEX 函数提取图像的 HEX 代码,HEX 代码根据 RSA 设置转换为密文。相反,加载密文,然后应用 RSA 算法,然后解密文本,结果字符串转换为图像。

本项目有三个类文件

1. RSAalgorithm.cs

在此类文件中,提供了 RSA 算法相关的函数,如计算 phi、n、square 和 modulus。RSA 算法是本项目中用于加密和解密图像的步骤。该算法加密和解密图像,即每个帧都会被加密和解密。

2. library.cs

在此类文件中,提供了十六进制解码、素数检查、字节到图像转换、图像到字节转换,反之亦然。Library 用于基本转换、处理和解码十六进制值、字节数组到图像的转换,以及反之亦然,即图像到字节数组的转换。

加密和解密流程图

上面的流程图展示了本文的项目是如何工作的。该流程图以分步方式解释了使用 C# 进行加密和解密的处理过程。在此项目中,我们只考虑图像而不考虑音频。图像被加密,并创建 *.txt 文件,该文件也用于解密。

Library 用于基本转换、处理和解码十六进制值、字节数组到图像的转换,以及反之亦然,即图像到字节数组的转换。library.cs 文件包含以下四个函数:

  1. DecodeHex:此函数用于解码十六进制,意味着它接受十六进制字符串,并将其转换为字节数组以进行图像转换。
  2. IsPrime:此函数检查参数值是否为素数。此函数对于验证目的很有用。素数由用户输入。
  3. ConvertByteToImage:此函数将字节数组转换为图像。图像是位图图像。
  4. ConvertImageToByte:此函数将图像转换为字节数组。图像是 JPEG 格式的图像。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace ImageCrypto
{
    class library
    {
        public static byte[] DecodeHex(string hextext)
        {
            String[] arr = hextext.Split('-');
            byte[] array = new byte[arr.Length];
            for (int i = 0; i < arr.Length; i++)
                array[i] = Convert.ToByte(arr[i], 16);
            return array;
        }

        public static bool IsPrime(int number)
        {
            if (number < 2) return false;
            if (number % 2 == 0) return (number == 2);
            int root = (int)Math.Sqrt((double)number);
            for (int i = 3; i <= root; i += 2)
            {
                if (number % i == 0) 
                    return false;
            }
            return true;
        }

        public static Bitmap ConvertByteToImage(byte[] bytes)
        {
            return (new Bitmap(Image.FromStream(new MemoryStream(bytes))));
        }

        public static byte[] ConvertImageToByte(Image My_Image)
        {
            MemoryStream m1 = new MemoryStream();
            new Bitmap(My_Image).Save(m1, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] header = new byte[] { 255, 216 };
            header = m1.ToArray();
            return (header);
        }
    }
} 

RSA:(Rivest, Shamir, Adleman)

它是如何工作的?

RSA 是一个加密和身份验证系统,由 Ron Rivest、Adi Shamir 和 Leonard Adleman 于 1977 年开发的算法。RSA 是一个密码系统,也称为公钥密码系统。RSA 通常用于安全数据传输。

RSA 用户会生成两个大素数的乘积,以及一个辅助值,作为其公钥。素数因子被保密。公钥用于加密消息,私钥用于解密消息。

在下面的 RSA 算法中,清楚地展示了如何使用 RSA 加密和解密消息,并附有数值示例。但在本文提供的项目中,我们用图像帧的十六进制值而不是数值来加密。在代码部分,所有 RSA 算法相关的函数都已详细解释。

带示例的 RSA 算法

步骤 1:开始

步骤 2:选择两个素数

p = 3 且 q = 11

步骤 3:计算 'n' 的值

n = p * q = 3 * 11 = 33

步骤 4:计算 ? (n) 的值

? (n) = (p - 1) * (q -1) = 2 * 10 = 20

步骤 5:选择 e,使得 1 < e < ? (n),且 e 和 n 互质。令 e = 7

步骤 6:计算 d 的值,使得 (d * e) % ? (n) = 1。d = 3

公钥是 (e, n) => (7, 33)

私钥是 (d, n) => (3, 33)

步骤 7:停止。

令 M 为明文(消息),M = 2。

M 的加密是:C = Me % n。

密文是,C = 27 % 33。

C = 29。

C 的解密是:M = Cd % n。

明文(消息),M = 293 % 33。

M = 2

RSA 程序

在此类文件中,提供了 RSA 算法相关的函数,如计算 phi、n、square 和 modulus。RSA 算法是本项目中加密和解密视频图像或帧的第一层步骤。该算法逐帧加密和解密视频,即每个帧都会被加密和解密。

  • square:此函数计算平方值。
  • BigMod:这是一个递归函数。它调用自身。它用于加密和解密。
  • n_value:此函数计算 'n' 的值。
  • cal_phi:此函数计算 phi,即 ? (n) 的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;

namespace ImageCrypto
{
    class RSAalgorithm
    {
        public static long square(long a)
        {
            return (a * a);
        }

        public static long BigMod(int b, int p, int m) //b^p%m=?
        {
            if (p == 0)
                return 1;
            else if (p % 2 == 0)
                return square(BigMod(b, p / 2, m)) % m;
            else
                return ((b % m) * BigMod(b, p - 1, m)) % m;
        }

        public static int n_value(int prime1, int prime2) 
        {
            return( prime1 * prime2);
        }

        public static int cal_phi(int prime1, int prime2)
        { 
            return ( (prime1 - 1) * (prime2- 1) );
        }

        public static Int32 cal_privateKey(int phi, int e, int n )
        {             
            int d = 0 ;
            int RES = 0;

            for (d = 1; ; d++)
            {
                RES = (d * e) % phi;
                if (RES == 1) break;
            }
            return d;
        }
    }
} 

加密

以下函数执行实际的加密任务。此函数接受字符串参数。图像的十六进制代码作为参数传递给此函数。此字符串被转换为字符数组。每个字符都使用 RSA 设置进行加密,直到数组末尾。密文字符串保存为 *.txt 扩展名。

public string encrypt(string imageToEncrypt)
{
   string hex = imageToEncrypt;
   char[] ar = hex.ToCharArray();
   String c = "";
   progressBar1.Maximum = ar.Length;
   for (int i = 0; i < ar.Length; i++)
   {
      Application.DoEvents();
      progressBar1.Value = i;
      if (c == "")
         c = c + ImageCrypto.RSAalgorithm.BigMod(ar[i], RSA_E, n);
      else
         c = c + "-" + ImageCrypto.RSAalgorithm.BigMod(ar[i], RSA_E, n);
    }
    return c;
} 

解密

以下函数执行实际的解密任务。此函数接受字符串参数。图像的密文代码作为参数传递给此函数。密文字符串被转换为字符数组。每个字符都使用 RSA 设置进行解密。解密后的字符被转换为实际的字符值。每个字符都被连接起来,然后返回连接的字符串。

public string decrypt(String imageToDecrypt)
{
    char[] ar = imageToDecrypt.ToCharArray();
    int i = 0, j = 0;
    string c = "", dc = "";
    progressBar2.Maximum = ar.Length;
    try
    {
       for (; i < ar.Length; i++)
       {
          Application.DoEvents();
          c = "";
          progressBar2.Value = i;
          for (j = i; ar[j] != '-'; j++)
                 c = c + ar[j];
          i = j;
          int xx = Convert.ToInt16(c);
          dc = dc + ((char)ImageCrypto.RSAalgorithm.BigMod(xx, d, n)).ToString();
       }
    }
    catch (Exception ex) { }
    return dc;
} 

关注点

  • 学习如何加密和解密图像
  • 学习 RSA 算法
  • 学习如何从图像中提取十六进制代码

参考文献

[1] http://en.wikipedia.org/wiki/RSA_(cryptosystem)

抱歉

抱歉我的英语。如果您注意到错误或可以提出更正版本,请告知我。

历史

  • 2014 年 2 月 8 日 - 首次发布
© . All rights reserved.