app登录,登录的密码要用DES加密,服务器是用erlang,客户端要同时支持多平台(Android、iOS)。首先,Java端的DES加密的实现方式,
少说废话了,直接上代码,如下:
public class DES {
private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; public static String encryptDES(String encryptString, String encryptKey)
throws Exception {
IvParameterSpec zeroIv = new IvParameterSpec(iv);
SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
return Base64.encode(encryptedData);
}
}
上述代码用到了一个Base64的编码类,其代码的实现方式如下:
public class Base64 {
private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
.toCharArray(); /**
* data[]进行编码
*
* @param data
* @return
*/
public static String encode(byte[] data) {
int start = 0;
int len = data.length;
StringBuffer buf = new StringBuffer(data.length * 3 / 2); int end = len - 3;
int i = start;
int n = 0; while (i <= end) {
int d = ((((int) data[i]) & 0x0ff) << 16)
| ((((int) data[i + 1]) & 0x0ff) << 8)
| (((int) data[i + 2]) & 0x0ff); buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append(legalChars[(d >> 6) & 63]);
buf.append(legalChars[d & 63]); i += 3; if (n++ >= 14) {
n = 0;
buf.append(" ");
}
} if (i == start + len - 2) {
int d = ((((int) data[i]) & 0x0ff) << 16)
| ((((int) data[i + 1]) & 255) << 8); buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append(legalChars[(d >> 6) & 63]);
buf.append("=");
} else if (i == start + len - 1) {
int d = (((int) data[i]) & 0x0ff) << 16; buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append("==");
} return buf.toString();
}
}
注意下:这里用的des加密,cbc模式,填充方式是:PKCS5Padding,还需注意下key和Ivev
然后,服务器由erlang解密,注意上述的加密模式,填充方式,以及key和Ivev的值需要保持一样。
代码如下:
-module(des). -define(DES_KEY, des_key).
-define(CHARSET, charset). -export([encrypto_des_cbc/1,decrypto_des_cbc/1,
encrypto_des_cbc/3,decrypto_des_cbc/3]). encrypto_des_cbc(PlainText) ->
Key = "xxxxxxxx",
Charset = utf8,
encrypto_des_cbc(PlainText, Key, Charset). decrypto_des_cbc(Ciphertext) ->
Key = "xxxxxxxx",
Charset = utf8,
decrypto_des_cbc(Ciphertext, Key, Charset). %% des 加密
encrypto_des_cbc(PlainText, Key, Charset) ->
Key2 = unicode:characters_to_list(Key, Charset),
Ivec = <<1,2,3,4,5,6,7,8>>, %% 按DES规则,补位
N = 8 - (byte_size(list_to_binary(PlainText)) rem 8),
PlainText2 = lists:append(PlainText, get_padding(N)),
%% 加密
Ciphertext = crypto:block_encrypt(des_cbc, Key2, Ivec, PlainText2),
{ok, Ciphertext}. %% des 解密
decrypto_des_cbc(Ciphertext, Key, Charset) -> Key2 = unicode:characters_to_list(Key, Charset),
Ivec = <<1,2,3,4,5,6,7,8>>,
case is_list(Ciphertext) of
true ->
CipherBin = list_to_binary(Ciphertext);
false ->
CipherBin = Ciphertext
end, PlainAndPadding = crypto:block_decrypt(des_cbc,Key2,Ivec,CipherBin),
<<PosLen/integer>> = binary_part(PlainAndPadding,{size(PlainAndPadding),-1}),
Len = byte_size(PlainAndPadding) - PosLen,
<<PlainText:Len/binary, _:PosLen/binary>> = PlainAndPadding,
{ok, PlainText}. get_padding(N) ->
case N of
0 ->
get_padding2(8,8,[]);
Num ->
get_padding2(Num,Num,[])
end. get_padding2(N, Val, PaddingList) when N > 0 ->
get_padding2(N-1, Val, [Val] ++ PaddingList);
get_padding2(N, _Val,PaddingList) when N == 0 ->
PaddingList.
这样,server端就可以解密了.
注意:java的des加密的最后有个base64的转码,server的解密,首先需要base64:decode,然后再是des的解密 decode.
这里可扩展AES加解密和对应模式等等.
这个的加解密的执行的shell截图没有上传,小伙伴可以试下~