[英]AES encryption Java to c#
我有以下代碼可以在Java中為我加密/解密數據
我需要在Windows Phone(8)設備上使用C#加密/解密數據,並且相同的數據應該能夠使用此Java代碼解密/加密。
Windows Phone中C#中此Java代碼的等效代碼是什么?
public class AESencrp {
private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[] { 'S', 'D', 'P', 'i', 'b', 'm', 'B','H', 'A', 'R', 'T','I', 'P', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
System.out.println(".............Encryption start............");
Key key = generateKey();
System.out.println("Key : " + key);
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
//System.out.println("encVal in byte[] : " + encVal);
String encryptedValue = new BASE64Encoder().encode(encVal);
System.out.println("encryptedValue byusing base64 : " + encryptedValue);
System.out.println("..............Encryption End............");
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
//final byte[] keyValue1 = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't','S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
System.out.println("");
System.out.println("");
System.out.println(".............Decryption start............");
Key key = generateKey();
//Key key = new SecretKeySpec(keyValue1, ALGO);
//System.out.println("Key : " + key);
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
//System.out.println("decryptedValue byusing base64 : " + decordedValue);
byte[] decValue = c.doFinal(decordedValue);
// System.out.println("decValue in byte[] : " + decValue);
String decryptedValue = new String(decValue);
System.out.println("String representation of decrypted value: " + decryptedValue);
System.out.println(".............Decryption End............");
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
System.out.println("key is " + keyValue );
return key;
}
public static void main(String[] args) throws Exception {
String password = "encrypt_this";
String passwordEnc = AESencrp.encrypt(password);
String passwordDec = AESencrp.decrypt(passwordEnc);
System.out.println("");System.out.println("");
System.out.println("Plain Text : " + password);
System.out.println("Encrypted Text : " + passwordEnc);
System.out.println("Decrypted Text : " + passwordDec);
}
}
似乎在這段代碼中,我只需要一個字符串,即-
private static final byte[] keyValue = new byte[] { 'S', 'D', 'P', 'i', 'b', 'm', 'B','H', 'A', 'R', 'T','I', 'P', 'K', 'e', 'y' };
加密數據。
休息一切都是自動完成的。
我在此鏈接上看到了一些解決方案
http://robtiffany.com/dont-forget-to-encrypt-your-windows-phone-7-data/
但這需要SALT值(我真的不知道它是什么???)
該網站還說,可以跳過一些步驟,並使用預先創建的“密鑰和初始化向量”。
在這件事上有什么幫助嗎?
您可以使用本機庫以與Java代碼相同的方式來加密字符串。 您只需要將IV(Salt)設置為一個空數組,所需大小為16。
我創建了一個類來進行加密/解密和轉換(將byte[]
轉換為string
並反向轉換)。
希望您不要介意德國的評論。 只是沒有時間翻譯它們
static class AesClass
{
public static byte[] EncryptStringToBytes(string plainText, byte[] Key)
{
// Überprüfe, ob alle Parameter gefüllt sind
if ((String.IsNullOrEmpty(plainText) || (Key.Length <= 0 || Key == null)))
throw new Exception("Both values mustn't be null or empty");
byte[] encrypted;
// Erstelle ein Objekt der Klasse Aes und
// belege den Schlüssel, sowie den Salt (Initalisierungsvektor) mit den Parametern
using (AesManaged aesAlgo = new AesManaged())
{
aesAlgo.Key = Key;
aesAlgo.IV = new byte[16];
//Verschlüsseler zur Umwandlung erstellen
ICryptoTransform encryptor = aesAlgo.CreateEncryptor();
// Erstelle Streams, welche für die Verschlüsselung genutzt werden
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Schreibe die Daten in den Stream
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Gebe die verschlüsselten Bytes aus dem Stream zurück
return encrypted;
}
public static string DecryptStringFromBytes(byte[] cipherText, byte[] Key)
{
//Überprüfe, ob alle Parameter gefüllt sind
if (((cipherText.Length <= 0 || cipherText == null) || (Key.Length <= 0 || Key == null)))
throw new Exception("Both values mustn't be null or empty");
//Erstelle eine Variable, in welcher später der entschlüsselte Text gespeichert wird
string plaintext = null;
try
{
// Erstelle ein Objekt von AES mit begrenzter Gültigkeit und
// weiße dem Schlüssel (Key) und Salt (IV), die als Parameter übergebenen Werte zu
using (AesManaged aesAlgo = new AesManaged())
{
aesAlgo.Key = Key;
aesAlgo.IV = new byte[16];
// Erstelle einen Entschlüsseler, welcher den Schlüssel und Salt nutzt,
// um den ByteArray-Stream zu verändern (entschlüsseln)
ICryptoTransform decryptor = aesAlgo.CreateDecryptor(aesAlgo.Key, aesAlgo.IV);
// Erstelle Stream, welche zu Entschlüsselung genutzt werden
// Ein Stream ist eine Darstellung einer geordneten Abfolge von Bytes
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Lese die entschlüsselten Bytes aus dem entschlüsselten Stream
// und füge sie komplett in die Variable, welche den entschlüsselte Text speichert.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message + "\r\nMöglichweise passt der aktuelle Schlüssel oder Salt nicht mit jenem überein, welcher die Daten verschlüsselt hat");
return null;
}
return plaintext;
}
///
/// Wandle den String in ein ByteArray mit Base64
/// Base64 ist ein Verfahren zur Kodierung von Binärdaten
///
public static byte[] AESStringToByteArray(string cipher)
{
byte[] encByteArray = Convert.FromBase64String(cipher);
return encByteArray;
}
///
/// Wandelt das übergebene Bytearray in einen menschenlesbaren String.
/// Base64 ist ein Verfahren um Binärdaten zu kodieren
///
///Die Binärdaten
/// Den gewandelten String
public static string AESByteArrayToString(byte[] arr)
{
string base64 = Convert.ToBase64String(arr);
return base64;
}
}
您可以通過以下方式使用該類(我想您是在頁面上調用):
private static byte[] keyValue = new byte[] { (int)'S', (int)'D', (int)'P', (int)'i', (int)'b', (int)'m', (int)'B', (int)'H',
(int)'A', (int)'R', (int)'T', (int)'I', (int)'P', (int)'K', (int)'e', (int)'y' };
private void Button_Encrypt_Click(object sender, RoutedEventArgs e)
{
byte[] Data = AesClass.EncryptStringToBytes(TextBox_UnecryptedString.Text, keyValue);
TextBox_EncryptedString.Text = AesClass.AESByteArrayToString(Data);
}
private void Button_Decrypt_Click(object sender, RoutedEventArgs e)
{
byte[] cipherText = AesClass.AESStringToByteArray(TextBox_EncryptedString.Text);
TextBox_DecryptedString.Text = AesClass.DecryptStringFromBytes(cipherText, keyValue);
}
結果:
Java的:
C#/ WP7:
AES加密可以在C#和Java上完成,但是有一些區別。
可悲的是,現在我記得它相當差勁,但是本地wp7加密在密碼方面存在一些問題。 我在這里和這里都寫過。 您最好下載wp7的BouncyCastle.Crypto庫。
下載該庫后,請嘗試以下操作:
private char[] keyValue = new char[] { 'S', 'D', 'P', 'i', 'b', 'm', 'B', 'H', 'A', 'R', 'T', 'I', 'P', 'K', 'e', 'y' };
private char[] keyValue1 = new char[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
private byte[] key;
private byte[] key1;
public AESPage()
{
InitializeComponent();
}
private void buttonEncrypt_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
key = keyValue.Select(x => Convert.ToByte(x)).ToArray();
key1 = keyValue1.Select(s => Convert.ToByte(s)).ToArray();
String password = "encrypt_this";
String passwordEnc = encrypt(GetBytes(password));
String passwordDec = decrypt(GetBytes(passwordEnc));
}
private String encrypt(byte[] Data)
{
IBufferedCipher cipher = CipherUtilities.GetCipher("AES");
cipher.Init(true, new KeyParameter(key));
byte[] encVal = cipher.DoFinal(Data);
MemoryStream memoryStream = new MemoryStream();
//Encrypt Data
memoryStream.Write(encVal, 0, encVal.Length);
memoryStream.Flush();
//Return encrypted String
byte[] decryptBytes = memoryStream.ToArray();
return GetString(decryptBytes);
}
private String decrypt(byte[] encryptedData)
{
IBufferedCipher cipher = CipherUtilities.GetCipher("AES");
cipher.Init(false, new KeyParameter(key1));
byte[] decValue = cipher.DoFinal(encryptedData);
MemoryStream memoryStream = new MemoryStream();
//Decrypt Data
memoryStream.Write(decValue, 0, decValue.Length);
memoryStream.Flush();
//Return decrypted String
byte[] decryptBytes = memoryStream.ToArray();
return GetString(decryptBytes);
}
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
private String GetString(byte[] result)
{
return System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
}
我已經為一個具有單個按鈕的簡單Windows電話頁面編寫了此代碼,而沒有編寫其他類(例如您的AESencrp),但是我認為這個主意很明確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.