我的对称加密将数据添加到我要保存和加载加密的rsa密钥中

如何解决我的对称加密将数据添加到我要保存和加载加密的rsa密钥中

当前,我正在尝试在bouncycastle的帮助下为RSA密钥实现保存功能。如果我尝试将我的公钥或私钥加密后再加载,则会遇到问题。

作为一个小例子,这里是原始公共密钥:

305C300D06092A864886F70D0101010500034B00304802410096B4751049165D1E046063EA22E8FFA0F90AE1DD997A3876DA5F79C7DE97951F009AC9ACA3EB91114F8A32C04F48293B6665CD6DD5C406C81CD13270A2AB61130203010001

加载后得到的结果(它添加了4个零,键越大意味着添加的零越多):

305C300D06092A864886F70D0101010500034B00304802410096B4751049165D1E046063EA22E8FFA0F90AE1DD997A3876DA5F79C7DE97951F009AC9ACA3EB91114F8A32C04F48293B6665CD6DD5C406C81CD13270A2AB611302030100010000

我发现它与我的对称加密的实现和那里使用的填充有关。普通文本无论多长时间都可以正常工作,而无需添加额外的数据。

这是我用于AES加密的代码:

加密

byte[] outputBytes = new byte[0];
AesEngine aesengine = new AesEngine();
CbcBlockCipher aesblockCipher = new CbcBlockCipher(aesengine);
PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(aesblockCipher);
KeyParameter aeskeyParameter = new KeyParameter(Hash.HashDataBlock(password,Hash.HashAlgorithm.SHA3).Bytes);
aescipher.Init(true,aeskeyParameter);
outputBytes = new byte[aescipher.GetOutputSize(inputBytes.Bytes.Length)];
int aeslength = aescipher.ProcessBytes(inputBytes.Bytes,outputBytes,0);
aescipher.DoFinal(outputBytes,aeslength);

解密

byte[] inputBytes = input.Bytes;
byte[] outputBytes = new byte[0];
AesEngine aesengine = new AesEngine();
CbcBlockCipher aesblockCipher = new CbcBlockCipher(aesengine);
PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(aesblockCipher);
KeyParameter aeskeyParameter = new KeyParameter(Hash.HashDataBlock(password,Hash.HashAlgorithm.SHA3).Bytes);
aescipher.Init(false,aeskeyParameter);
outputBytes = new byte[aescipher.GetOutputSize(inputBytes.Length)];
int aeslength = aescipher.ProcessBytes(inputBytes,aeslength);

“我的功能”以保存和加载按键。 DataBlock类仅将数据转换为所需的格式,例如UTF8,Base64或仅字节数组:

public static void SaveKeyEncrypted(DataBlock key,string path,DataBlock password)
{
  StreamWriter sw = new StreamWriter(path);
  DataBlock encrypted = SymmetricEncryption.Encrypt(key,password,SymmetricEncryption.SymmetricAlgorithms.AES);
  sw.Write(encrypted.Base64);
  sw.Close();
}

public static DataBlock ReadKeyEncrypted(string path,DataBlock password)
{
  StreamReader sr = new StreamReader(path);
  DataBlock readData = new DataBlock(sr.ReadLine(),DataBlock.DataType.Base64);
  sr.Close();
  return SymmetricEncryption.Decrypt(readData,SymmetricEncryption.SymmetricAlgorithms.AES);
}

为了复制我与此问题有关的其他代码:

public class DataBlock
    {
        private byte[] _data;

        public DataBlock()
        {
            this._data = new byte[0];
        }

        public enum DataType
        {
            UTF8,UTF7,UTF32,ASCII,Unicode,Hex,Base64,Base32
        }

        public DataBlock(string data,DataType dataType) : this()
        {
            switch (dataType)
            {
                case DataType.UTF8:
                    this._data = Encoding.UTF8.GetBytes(data);
                    break;
                case DataType.UTF7:
                    this._data = Encoding.UTF7.GetBytes(data);  
                    break;
                case DataType.UTF32:
                    this._data = Encoding.UTF32.GetBytes(data);
                    break;
                case DataType.ASCII:
                    this._data = Encoding.ASCII.GetBytes(data);
                    break;
                case DataType.Unicode:
                    this._data = Encoding.Unicode.GetBytes(data);
                    break;
                case DataType.Hex:
                    this._data = new byte[data.Length / 2];
                    for (int i = 0; i < data.Length; i += 2)
                    {
                        this._data[i / 2] = Convert.ToByte(data.Substring(i,2),16);
                    }
                    break;
                case DataType.Base64:
                    this._data = Convert.FromBase64String(data);
                    break;
                case DataType.Base32:
                    this._data = this.FromBase32String(data);
                    break;
            }
        }

        public DataBlock(byte[] data)
        {
            this._data = data;
        }

        public string UTF8
        {
            get
            {
                return Encoding.UTF8.GetString(this._data);
            }
        }

        public string UTF7
        {
            get
            {
                return Encoding.UTF7.GetString(this._data);
            }
        }

        public string UTF32
        {
            get
            {
                return Encoding.UTF32.GetString(this._data);
            }
        }

        public string ASCII
        {
            get
            {
                return Encoding.ASCII.GetString(this._data);
            }
        }

        public string Unicode
        {
            get
            {
                return Encoding.Unicode.GetString(this._data);
            }
        }

        public string Hex
        {
            get
            {
                return BitConverter.ToString(this._data).Replace("-","");
            }
        }

        public string Base64
        {
            get
            {
                return Convert.ToBase64String(this._data);
            }
        }

        public string Base32
        {
            get
            {
                return this.ToBase32String(this._data);
            }
        }

        public byte[] Bytes
        {
            get
            {
                return this._data;
            }
        }

        private string ValidChars = "QAZ2WSX3" + "EDC4RFV5" + "TGB6YHN7" + "UJM8K9LP";

        private string ToBase32String(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            byte index;
            int hi = 5;
            int currentByte = 0;

            while (currentByte < bytes.Length)
            {
                if (hi > 8)
                {
                    index = (byte)(bytes[currentByte++] >> (hi - 5));
                    if (currentByte != bytes.Length)
                    {
                        index = (byte)(((byte)(bytes[currentByte] << (16 - hi)) >> 3) | index);
                    }

                    hi -= 3;
                }
                else if (hi == 8)
                {
                    index = (byte)(bytes[currentByte++] >> 3);
                    hi -= 3;
                }
                else
                {
                    index = (byte)((byte)(bytes[currentByte] << (8 - hi)) >> 3);
                    hi += 5;
                }

                sb.Append(ValidChars[index]);
            }

            return sb.ToString();
        }

        public byte[] FromBase32String(string str)
        {
            int numBytes = str.Length * 5 / 8;
            byte[] bytes = new Byte[numBytes];
            str = str.ToUpper();

            int bit_buffer;
            int currentCharIndex;
            int bits_in_buffer;

            if (str.Length < 3)
            {
                bytes[0] = (byte)(ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
                return bytes;
            }

            bit_buffer = (ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
            bits_in_buffer = 10;
            currentCharIndex = 2;
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)bit_buffer;
                bit_buffer >>= 8;
                bits_in_buffer -= 8;
                while (bits_in_buffer < 8 && currentCharIndex < str.Length)
                {
                    bit_buffer |= ValidChars.IndexOf(str[currentCharIndex++]) << bits_in_buffer;
                    bits_in_buffer += 5;
                }
            }

            return bytes;
        }
    }

生成密钥对的功能

public static DataBlock[] GenerateKeyPair(KeyPairSize keyPairSize)
        {
            RsaKeyPairGenerator keyPairGenerator = new RsaKeyPairGenerator();
            keyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(new CryptoApiRandomGenerator()),(int) keyPairSize));
            AsymmetricCipherKeyPair keyPair = keyPairGenerator.GenerateKeyPair();
            PrivateKeyInfo pkInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private);
            SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
            DataBlock[] keyPairData = new DataBlock[2];
            keyPairData[0] = new DataBlock(pkInfo.GetDerEncoded());
            keyPairData[1] = new DataBlock(info.GetDerEncoded());
            return keyPairData;
        }

重现该错误的代码:

DataBlock[] keyPair = AsymmetricEncryption.GenerateKeyPair(AsymmetricEncryption.KeyPairSize.Bits512);
DataBlock pass = new DataBlock("1234",DataBlock.DataType.UTF8);
DataBlock orig = new DataBlock("Hello World",DataBlock.DataType.UTF8);
DataBlock encrypted = AsymmetricEncryption.Encrypt(orig,keyPair[1]);
AsymmetricEncryption.SaveKeyEncrypted(keyPair[0],"D:\\privateenc",pass);
AsymmetricEncryption.SaveKeyEncrypted(keyPair[1],"D:\\publicenc",pass);
DataBlock privateKey = AsymmetricEncryption.ReadKeyEncrypted("D:\\privateenc",pass);
DataBlock publicKey = AsymmetricEncryption.ReadKeyEncrypted("D:\\publicenc",pass);
DataBlock decrypted = AsymmetricEncryption.Decrypt(encrypted,privateKey);
Console.WriteLine(decrypted.UTF8);

不需要加密/解密方法,因为读取硬盘驱动器上的加密密钥后,该错误已经发生。

为什么/在哪里添加了额外的数据,我该如何解决?

解决方法

我能够通过将密钥的初始字节数组长度添加到加密文本中来修复它,并在以后读取它。在读取功能中,我将所有内容都剪裁成原始大小。

主要问题仍然存在,这只是一种解决方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-