申请会员ID:caoxueqiang
1、申 请 I D :caoxueqiang2、个人邮箱:caoxueqiang@163.com
3、原创技术文章:Android、iOS、windows下通用的des加密解密方法
最近写了一个手机程序需要在Android、iOS、windows三个系统下生成相同的注册码
在网上找了很多代码但测试都没有通过,
最终经过研究对比修改后,于实现了三个平台通用方法。
三平台下截图
代码
java
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(new byte);
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);
}
public static byte[] encryptDESBytes(byte[] encryptBytes, String encryptKey)
throws Exception
{
// IvParameterSpec zeroIv = new IvParameterSpec(new byte);
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(encryptBytes);
return encryptedData;
}
public static String decryptDES(String decryptString, String decryptKey)
throws Exception
{
byte[] byteMi = Base64.decode(decryptString);
IvParameterSpec zeroIv = new IvParameterSpec(iv);
// IvParameterSpec zeroIv = new IvParameterSpec(new byte);
SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
byte decryptedData[] = cipher.doFinal(byteMi);
return new String(decryptedData, "UTF-8");
}
public static byte[] decryptDESBytes(byte[] decryptBytes, String decryptKey)
throws Exception
{
IvParameterSpec zeroIv = new IvParameterSpec(iv);
// IvParameterSpec zeroIv = new IvParameterSpec(new byte);
SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
byte[] decryptedData = cipher.doFinal(decryptBytes);
return decryptedData;
}
public static String parseByte2HexStr(byte buf[])
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++)
{
String hex = Integer.toHexString(buf & 0xFF);
if (hex.length() == 1)
{
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
}
代码
OC
@implementation DES
const Byte iv[] = {1,2,3,4,5,6,7,8};
const int bufferSize = 1024*1024*2; // 1024x10241048576
static unsigned char buffer;
+(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
{
NSString *ciphertext = nil;
NSData *textData = ;
NSUInteger dataLength = ;
//unsigned char buffer; //已经定义为静态变量放到上方,防止占用堆栈过大,程序闪退
memset(buffer, 0, sizeof(char));
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding,
, kCCKeySizeDES,
iv,
, dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
NSData *data = ;
ciphertext = ;
//ciphertext = ;
}
return ciphertext;
}
+(NSString *)decryptUseDES:(NSString *)cipherText key:(NSString *)key
{
NSString *plaintext = nil;
NSData *cipherdata = ;
//NSData *cipherdata = ;
//unsigned char buffer;//已经定义为静态变量放到上方,防止占用堆栈过大,程序闪退
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding,
, kCCKeySizeDES,
iv,
, ,
buffer, bufferSize,
&numBytesDecrypted);
if(cryptStatus == kCCSuccess) {
NSData *plaindata = ;
plaintext = [initWithData:plaindata encoding:NSUTF8StringEncoding];
NSLog(@"解密完成!");
}
return plaintext;
}
【开放注册公告】吾爱破解论坛2015年7月21日暑假开放注册公告
http://www.52pojie.cn/thread-381155-1-1.html
最近开放注册,自己开放注册的时候来注册吧。 前排支持。。。。。。。。 Hmily 发表于 2015-7-11 20:27
最近开放注册,自己开放注册的时候来注册吧。
H大大,这样好吗?人家都把技术文章贴出来了,还让人家在开放的时候注册吗 开放注册时间确实快到了,楼主稍微等等也行。 21号开放注册,到时候再来吧 顶一下我自己的帖子! 有没有简单一点的教程
页:
[1]