加密和解密字符串 [英] Encrypt And Decrypt String

查看:70
本文介绍了加密和解密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
正在尝试加密和解密字符串值.
字符串已成功加密,但解密时却给我带来了加密异常要解密的数据长度无效"
你能帮我吗?
这是我后面的代码.

Hello guys..
am trying to encrypt and decrypt a string value.
String is getting encrypted successfully, but while decrypting it gives me cryptographic exception " Length of the data to decrypt is invalid"
Can you please help me with this??
Here''s my code behind.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace EncryptDecrypt
{
    public partial class Form1 : Form
    {
        private byte[] key = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
        private byte[] iv = { 65,110,68,26,69,178,200,219};
        byte[] inputinbytes;
        UTF8Encoding obj;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
           Byte[] abc= Encrypt(textBox1.Text);
           FileStream fs = new FileStream(@"C:\temp\abc.txt", FileMode.Create, FileAccess.Write);
           fs.Write(abc, 0, abc.Length);
           fs.Close();
        }


        public Byte[] Encrypt(String plaintext)
        {
            byte[] result=new byte[1024];
            try
            {
                obj = new UTF8Encoding();
                inputinbytes = obj.GetBytes(textBox1.Text);
                TripleDESCryptoServiceProvider objtriple = new TripleDESCryptoServiceProvider();
                ICryptoTransform cryptotransform = objtriple.CreateEncryptor(this.key, this.iv);
                MemoryStream encryptedstream = new MemoryStream();
                CryptoStream cryptstream = new CryptoStream(encryptedstream, cryptotransform, CryptoStreamMode.Write);
                cryptstream.Write(inputinbytes, 0, inputinbytes.Length);
                cryptstream.FlushFinalBlock();
                encryptedstream.Position = 0;
                result = new byte[encryptedstream.Length-1];
                encryptedstream.Read(result, 0, (int)encryptedstream.Length-1);
                cryptstream.Close();
                return result;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
                return result;
            }
            
        }


        public string Decrypt(byte[] input)
        {
            
            try
            {
                UTF8Encoding utf8encoder = new UTF8Encoding();
                TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
                ICryptoTransform cryptotransform = tdesProvider.CreateDecryptor(this.key, this.iv);
                MemoryStream decryptedstream = new MemoryStream();
                CryptoStream cryptstream = new CryptoStream(decryptedstream, cryptotransform, CryptoStreamMode.Write);
                cryptstream.Write(input, 0, input.Length);
                cryptstream.FlushFinalBlock();
                //cryptstream.Position = 0;
                byte[] result = new byte[decryptedstream.Length-1];
                decryptedstream.Read(result, 0,(int) decryptedstream.Length-1);
                cryptstream.Close();
                UTF8Encoding ob = new UTF8Encoding();
                return ob.GetString(result);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
                return "false";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"C:\temp\abc.txt", FileMode.Open, FileAccess.Read);
            byte[] abc=new byte[fs.Length];
            fs.Read(abc, 0, (int)fs.Length);
            string name= Decrypt(abc);
            MessageBox.Show("" + name);
        }
    }
}

推荐答案

与您正在使用的减号有什么关系吗?
Could it have anything to do with the minus one you are using?
result = new byte[encryptedstream.Length-1];
encryptedstream.Read(result, 0, (int)encryptedstream.Length-1);

加密流中的所有字节是否都可能相关?

Isn''t it possible that ALL bytes in an encrypted stream are relevant?


这篇关于加密和解密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆