好的,我有一个小程序,我用C#winforms构建,基本上使用安全密码学,我试图弄清楚我在代码中做错了什么。它会加密文本文件。但是,当我将加密文件粘贴到文本框中并点击Decrypt时,它不会解密文件,而是再次对其进行加密。
我想知道我是否对代码做错了。
public class MainActivity extends AppCompatActivity {
private CameraCaptureSession mSession;
private CaptureRequest.Builder mBuilder;
private CameraDevice mCameraDevice;
private CameraManager mCameraManager;
Boolean isOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (null == savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(R.id.container, Camera2BasicFragment.newInstance())
.commit();
}
final Button flashOn = (Button) findViewById(R.id.flah_off);
flashOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isOn) {
flashOn.setBackgroundResource(R.drawable.flash_on);
turnOnFlashLight();
}else{
flashOn.setBackgroundResource(R.drawable.flah_off);
turnOffFlashLight();
}
isOn = !isOn;
}
});
}
public void turnOnFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOffFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
这是上面和下面的类是Form1:
namespace Encrypted
{
class Encryptor
{
public static string IV = "1a1a1a1a1a1a1a1a";
public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a";
public static string Encrypt (string decrypted)
{
byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return Convert.ToBase64String(enc);
}
public static string Decrypted(string encrypted)
{
byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(encrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return ASCIIEncoding.ASCII.GetString(enc);
}
}
}
在Decrypt按钮中,我是否需要使用StreamReader或StreamWriter。一旦加密文件在data.ls中,我将其复制并粘贴到文本框中进行解密。但是不解密,它只重新加密文件。有什么我做错了吗?
答案 0 :(得分:2)
这是因为您正在使用Decrypted()
来创建Encryptor
来加密输入文件。它应该创建Decryptor
来执行解密操作。这个单词改变将颠倒这个过程。 AesCryptoServiceProvider
类的CreateDecryptor Method (Byte[], Byte[])将使用指定的密钥和初始化向量(IV)创建对称的AES解密器对象。
即。更改Decrypted()
方法中的以下行
ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);
答案 1 :(得分:0)
这不仅是不使用CreateDecryptor的错误。您还必须解码base64中的密文字符串,这是您的加密返回的内容。
你这样做:
取字符串,从ASCII解码为明文字节
取明文字节,加密到密文字节
取密文字节,转换为base64字符串
然后你这样做:
代替:
取字符串,从 base64 解码为密文字节
获取密文字节,解密为明文字节
取明文字节,编码为ASCII。
string - > bytes - >加密字节 - > base64 - >加密字节 - > bytes - >串
答案 2 :(得分:0)
我搜索了它并制作了一个与我合作的程序:) 所以......
这是班级
class Encryptor
{
public static string IV = "1a1a1a1a1a1a1a1a";
public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a13";
public static string Encrypt(string decrypted)
{
byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return Convert.ToBase64String(enc);
}
public static string Decrypted(string encrypted)
{
byte[] textbytes = Convert.FromBase64String(encrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return System.Text.ASCIIEncoding.ASCII.GetString(enc);
}
}
...................................
这是表格`
private void button1_Click(object sender, EventArgs e)
{
string dir = textBox1.Text;
Directory.CreateDirectory("data\\" + dir);
var sw = new StreamWriter("data\\" + dir + "data.ls");
string enctxt = Encryptor.Encrypt(textBox1.Text);
sw.WriteLine(enctxt);
sw.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string dir = textBox2.Text;
StreamReader sr = new StreamReader(Application.StartupPath +"\\data\\"+ dir + "data.ls");
string line = sr.ReadLine();
textBox1.Text = Encryptor.Decrypted(Convert.ToString(line));
}
........................
祝你好运!