【发布时间】:2013-10-31 20:30:41
【问题描述】:
我有以下代码在 java 中为我加密/解密数据
我需要在 Windows phone(8) 设备上用 C# 加密/解密数据,并且相同的数据应该能够使用此 java 代码解密/加密。
在 Windows Phone 中这个 java 代码在 C# 中的等效代码是什么??
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值(我真的不知道它是什么???)
此外,该网站表示可以跳过一些步骤并使用预先创建的密钥和初始化向量。
在这件事上有什么帮助吗?
【问题讨论】:
-
@user2864740 有什么办法不使用“盐”???
-
@user2864740 我有java代码在服务器上运行,它不能被改变。我需要一段 C# (Windows Phone) 代码,它相当于这个 java 代码并产生完全相同的输出。有可能吗?
-
@user2864740 很抱歉,我对加密知之甚少。据我了解,java代码不需要盐值,而c#代码需要。我什至不知道我错在哪里。但感谢您的帮助。
-
哦,我看错了(Java 代码需要镜像 C#)。
-
@user2864740 你觉得我想要的可行吗?
标签: java windows-phone-7 encryption windows-phone-8 aes