首先是第一件事.前段时间我在Android中需要一个简单的AES加密来加密密码并将其作为密码被解密的.net Web服务的参数发送.
以下是我的Android加密:
private static String Encrypt(String text, String key)
throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes= new byte[16];
byte[] b= key.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);
byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
String result = Base64.encodeBytes(results);
return result;
}
Run Code Online (Sandbox Code Playgroud)
然后我用C#解密它:
public static string Decrypt(string textToDecrypt, string key)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
RijndaelManaged rijndaelCipher = new …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我应该能够在蓝牙打印机 Zebra iMZ320 上进行打印,但我在使用 UTF-8 特定字符(\xc3\x86、\xc3\x98 或 \xc3\x85)时遇到一些问题。
\n\n我按如下方式连接到设备:
\n\n BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddr);\n Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { Integer.TYPE });\n bSocket = (BluetoothSocket)m.invoke(device, new Object[] { Integer.valueOf(1) });\n bSocket.connect();\n outStream = bSocket.getOutputStream();\n inStream = bSocket.getInputStream();\nRun Code Online (Sandbox Code Playgroud)\n\n套接字打开后,我将在 CPCL 中发送数据:
\n\n String cpclData = "! U1 SETLP 5 2 24 \\r\\n"+text+"\\r\\n";\n outStream.write(cpclData.getBytes());\n outStream.flush();\nRun Code Online (Sandbox Code Playgroud)\n\n但是当我尝试打印上述字符时,它会写一些异常字符。
\n\n我联系了 Zebra,他们的一位工程师写道,我应该尝试以下操作:
\n\n! 0 200 200 80 1 \nIN-MILLIMETERS \nJOURNAL \nCENTER \nCOUNTRY NORWAY\nTEXT 4 0 0 8 COUNTRY IS NORWAY …Run Code Online (Sandbox Code Playgroud)