发布网友 发布时间:2022-04-06 00:42
共2个回答
懂视网 时间:2022-04-06 05:03
php实现date转string的方法:可以利用SimpleDateFormat.format把date转成String,如【str3 = format1.format(date1);】。
Date与String格式的转换
(推荐教程:php视频教程)
SimpleDateFormat.format把date转成String。
SimpleDateFormat.parse把String转成date。
代码测试:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date1 = null; Date date2 = null; String str1 = "2009-02-14 12:00:00"; String str2 = "2009年02月14日 12时00分00秒"; // String转Date:String 必须严格按照定义的格式 try { date1 = format1.parse(str1); date2 = format2.parse(str2); } catch (ParseException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("date1= "+date1); System.out.println("date2= "+date2); //Date转StringString str3 = null; String str4 = null; str3 = format1.format(date1); str4 = format2.format(date2); System.out.println("str1= "+str3); System.out.println("str2= "+str4);
热心网友 时间:2022-04-06 02:11
IOS:引入ios自带库 #include
先以DES加密算法为例讲解,DES的加密和解密都同用一个Key,下面两个加解密函数如下:
//加密
-(NSString *) encryptUseDES:(NSString *)clearText key:(NSString *)key
{
//一般对加密的字符串采用UTF-8编码 NSData存储的就是二进制数据
NSData *data = [clearText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//确定加密过后的字符串在内存中存放的大小,根据文档,对于块密码方式(这个库还包括流密码方式)
//加密过后的字符串大小总是小于或等于加密之前数据的大小加上对应加密算法的块大小
//但看到一些大牛还这样一下 & ~(kCCBlockSizeDES - 1) 目前不知道为嘛
size_t bufferSize = ([data length] + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
//void *buffer = malloc(bufferSize);//可以手动创建buffer,但之后要记得free掉
unsigned char buffer[bufferSize]; //定义输出加密串所占内存空间
memset(buffer, 0, sizeof(char)); //采用ios中宏定义好的方法分配空间,可免去手动free
size_t numBytesEncrypted = 0; //输出加密串的字节数
//加密数据,采用库中的CCCrypt方法,这个方法会按次序执行CCCrytorCreate(),
// CCCryptorUpdate(), CCCryptorFinal(), and CCCryptorRelease() 如果开发者自己create这个对象,
//那么后面就必须执行final、release之类的函数,CCCrypt方法一次性解决
// Byte iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
//Byte iv[] = {1,2,3,4,5,6,7,8}; 加密所需的随机字符
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, //加密方式,kCCEncrypt加密 kCCDecrypt解密
kCCAlgorithmDES, //采用的加密算法,内置包含AES、DES、
//3DES、其他还有四个,不知道是什么
//后续讨论
//加密额外参数,注意此处各个平台之间指定的时候要记得一样
kCCOptionPKCS7Padding | kCCOptionECBMode,
[key UTF8String], //加密密匙 UTF8的字符串
kCCKeySizeDES, //密匙长度字节 各算法有对应的长度宏
nil, //随机字符,可指定也可不指定,各平台之间不绝对
[data bytes], //待加密串的字节长度
[data length], //待加密串的长度
buffer, //输出已加密串的内存地址
bufferSize, //已加密串的大小
&numBytesEncrypted);
NSString* plainText = nil;
if (cryptStatus == kCCSuccess) {
NSData *dataTemp = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
plainText = [GTMBase stringByEncodingData:dataTemp];
}else{
NSLog(@"DES加密失败");
}
return plainText;
}
//解密
-(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key {
// 利用 GTMBase 解码 Base 字串
NSData* cipherData = [GTMBase decodeString:cipherText];
size_t bufferSize = ([cipherData length] + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
//unsigned char buffer[1024];
unsigned char buffer[bufferSize];
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;
// IV 偏移量不需使用
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
[key UTF8String],
kCCKeySizeDES,
nil,
[cipherData bytes],
[cipherData length],
buffer,
bufferSize,//1024,
&numBytesDecrypted);
NSString* plainText = nil;
if (cryptStatus == kCCSuccess) {
NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
plainText = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
}
return plainText;
}
java和php平台的代码实现:
Java代码 收藏代码
import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASEDecoder;
import sun.misc.BASEEncoder;
public class DES {
private byte[] desKey;
public DES(String desKey) {
this.desKey = desKey.getBytes();
}
public byte[] desEncrypt(byte[] plainText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
byte data[] = plainText;
byte encryptedData[] = cipher.doFinal(data);
return encryptedData;
}
public byte[] desDecrypt(byte[] encryptText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
byte encryptedData[] = encryptText;
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
}
public String encrypt(String input) throws Exception {
return baseEncode(desEncrypt(input.getBytes()));
}
public String decrypt(String input) throws Exception {
byte[] result = baseDecode(input);
return new String(desDecrypt(result));
}
public static String baseEncode(byte[] s) {
if (s == null)
return null;
BASEEncoder b = new sun.misc.BASEEncoder();
return b.encode(s);
}
public static byte[] baseDecode(String s) throws IOException {
if (s == null)
return null;
BASEDecoder decoder = new BASEDecoder();
byte[] b = decoder.decodeBuffer(s);
return b;
}
public static void main(String[] args) throws Exception {
String key = "abcdefgh";
String input = "a";
DES crypt = new DES(key);
System.out.println("Encode:" + crypt.encrypt(input));
System.out.println("Decode:" + crypt.decrypt(crypt.encrypt(input)));
}
}
php 方法一
Php代码 收藏代码
<?php
class DES1 {
var $key;
function DES1($key) {
$this->key = $key;
}
function encrypt($input) {
$size = mcrypt_get_block_size('des', 'ecb');
$input = $this->pkcs5_pad($input, $size);
$key = $this->key;
$td = mcrypt_mole_open('des', '', 'ecb', '');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
$data = base_encode($data);
return $data;
}
function decrypt($encrypted) {
$encrypted = base_decode($encrypted);
$key =$this->key;
$td = mcrypt_mole_open('des','','ecb','');
//使用MCRYPT_DES算法,cbc模式
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
@mcrypt_generic_init($td, $key, $iv);
//初始处理
$decrypted = mdecrypt_generic($td, $encrypted);
//解密
mcrypt_generic_deinit($td);
//结束
mcrypt_mole_close($td);
$y=$this->pkcs5_unpad($decrypted);
return $y;
}
function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text))
return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
return false;
return substr($text, 0, -1 * $pad);
}
}
$key = "abcdefgh";
$input = "a";
$crypt = new DES1($key);
echo "Encode:".$crypt->encrypt($input)."<br/>";
echo "Decode:".$crypt->decrypt($crypt->encr