導航:首頁 > 編程語言 > java對稱字元串

java對稱字元串

發布時間:2023-01-13 18:47:40

『壹』 java相關 請看這個程序,如果輸入一個對稱字元串,會輸出好幾行「對稱字元串」。 求只輸出一行的解

你在循環體裡面肯定會列印很多次啊,你定義一個String變數,如果是對稱的String str=「是對稱的」;
如果不是String str="不是對稱的";
然後在循環外面列印str

『貳』 判斷一個字元串是否是對稱字元串,例如"abc"不是對稱字元串,"aba"、"abba"、"aaa"、"mnanm"是對稱字元串

packagecom.itheima;
/**
*4、判斷一個字元串是否是對稱字元串,例如"abc"不是對稱字元串
*,"aba"、"abba"、"aaa"、"mnanm"是對稱字元串
*
*/
publicclassTest4{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
StringarrString[]={"abc","aba","aa","mnanm","a"};
intlength=arrString.length;
for(inti=0;i<length;i++){
intk=arrString[i].length();
booleanstau=true;
//將字元串轉化為字元數組
char[]as=arrString[i].toCharArray();
for(intj=0;j<=k/2;j++){
if(as[j]!=as[k-j-1]){
System.out.println("字元串""+arrString[i]+""不是對稱字元串");
stau=false;
break;
}
}
if(stau){
System.out.println("字元串""+arrString[i]+""是對稱字元串");
}
}
}
}

『叄』 如何用JAVA實現字元串簡單加密解密

java加密字元串可以使用des加密演算法,實例如下:
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* 加密解密
*
* @author shy.qiu
* @since http://blog.csdn.net/qiushyfm
*/
public class CryptTest {
/**
* 進行MD5加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToMD5(String info) {
byte[] digesta = null;
try {
// 得到一個md5的消息摘要
MessageDigest alga = MessageDigest.getInstance("MD5");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
/**
* 進行SHA加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToSHA(String info) {
byte[] digesta = null;
try {
// 得到一個SHA-1的消息摘要
MessageDigest alga = MessageDigest.getInstance("SHA-1");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
// //////////////////////////////////////////////////////////////////////////
/**
* 創建密匙
*
* @param algorithm
* 加密演算法,可用 DES,DESede,Blowfish
* @return SecretKey 秘密(對稱)密鑰
*/
public SecretKey createSecretKey(String algorithm) {
// 聲明KeyGenerator對象
KeyGenerator keygen;
// 聲明 密鑰對象
SecretKey deskey = null;
try {
// 返回生成指定演算法的秘密密鑰的 KeyGenerator 對象
keygen = KeyGenerator.getInstance(algorithm);
// 生成一個密鑰
deskey = keygen.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 返回密匙
return deskey;
}
/**
* 根據密匙進行DES加密
*
* @param key
* 密匙
* @param info
* 要加密的信息
* @return String 加密後的信息
*/
public String encryptToDES(SecretKey key, String info) {
// 定義 加密演算法,可用 DES,DESede,Blowfish
String Algorithm = "DES";
// 加密隨機數生成器 (RNG),(可以不寫)
SecureRandom sr = new SecureRandom();
// 定義要生成的密文
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
// 參數:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
c1.init(Cipher.ENCRYPT_MODE, key, sr);
// 對要加密的內容進行編碼處理,
cipherByte = c1.doFinal(info.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
// 返回密文的十六進制形式
return byte2hex(cipherByte);
}
/**
* 根據密匙進行DES解密
*
* @param key
* 密匙
* @param sInfo
* 要解密的密文
* @return String 返回解密後信息
*/
public String decryptByDES(SecretKey key, String sInfo) {
// 定義 加密演算法,
String Algorithm = "DES";
// 加密隨機數生成器 (RNG)
SecureRandom sr = new SecureRandom();
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
c1.init(Cipher.DECRYPT_MODE, key, sr);
// 對要解密的內容進行編碼處理
cipherByte = c1.doFinal(hex2byte(sInfo));
} catch (Exception e) {
e.printStackTrace();
}
// return byte2hex(cipherByte);
return new String(cipherByte);
}
// /////////////////////////////////////////////////////////////////////////////
/**
* 創建密匙組,並將公匙,私匙放入到指定文件中
*
* 默認放入mykeys.bat文件中
*/
public void createPairKey() {
try {
// 根據特定的演算法一個密鑰對生成器
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
// 加密隨機數生成器 (RNG)
SecureRandom random = new SecureRandom();
// 重新設置此隨機對象的種子
random.setSeed(1000);
// 使用給定的隨機源(和默認的參數集合)初始化確定密鑰大小的密鑰對生成器
keygen.initialize(512, random);// keygen.initialize(512);
// 生成密鑰組
KeyPair keys = keygen.generateKeyPair();
// 得到公匙
PublicKey pubkey = keys.getPublic();
// 得到私匙
PrivateKey prikey = keys.getPrivate();
// 將公匙私匙寫入到文件當中
doObjToFile("mykeys.bat", new Object[] { prikey, pubkey });
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* 利用私匙對信息進行簽名 把簽名後的信息放入到指定的文件中
*
* @param info
* 要簽名的信息
* @param signfile
* 存入的文件
*/
public void signToInfo(String info, String signfile) {
// 從文件當中讀取私匙
PrivateKey myprikey = (PrivateKey) getObjFromFile("mykeys.bat", 1);
// 從文件中讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile("mykeys.bat", 2);
try {
// Signature 對象可用來生成和驗證數字簽名
Signature signet = Signature.getInstance("DSA");
// 初始化簽署簽名的私鑰
signet.initSign(myprikey);
// 更新要由位元組簽名或驗證的數據
signet.update(info.getBytes());
// 簽署或驗證所有更新位元組的簽名,返回簽名
byte[] signed = signet.sign();
// 將數字簽名,公匙,信息放入文件中
doObjToFile(signfile, new Object[] { signed, mypubkey, info });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 讀取數字簽名文件 根據公匙,簽名,信息驗證信息的合法性
*
* @return true 驗證成功 false 驗證失敗
*/
public boolean validateSign(String signfile) {
// 讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile(signfile, 2);
// 讀取簽名
byte[] signed = (byte[]) getObjFromFile(signfile, 1);
// 讀取信息
String info = (String) getObjFromFile(signfile, 3);
try {
// 初始一個Signature對象,並用公鑰和簽名進行驗證
Signature signetcheck = Signature.getInstance("DSA");
// 初始化驗證簽名的公鑰
signetcheck.initVerify(mypubkey);
// 使用指定的 byte 數組更新要簽名或驗證的數據
signetcheck.update(info.getBytes());
System.out.println(info);
// 驗證傳入的簽名
return signetcheck.verify(signed);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將二進制轉化為16進制字元串
*
* @param b
* 二進制位元組數組
* @return String
*/
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
/**
* 十六進制字元串轉化為2進制
*
* @param hex
* @return
*/
public byte[] hex2byte(String hex) {
byte[] ret = new byte[8];
byte[] tmp = hex.getBytes();
for (int i = 0; i < 8; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
/**
* 將兩個ASCII字元合成一個位元組; 如:"EF"--> 0xEF
*
* @param src0
* byte
* @param src1
* byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
.byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
.byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
/**
* 將指定的對象寫入指定的文件
*
* @param file
* 指定寫入的文件
* @param objs
* 要寫入的對象
*/
public void doObjToFile(String file, Object[] objs) {
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
for (int i = 0; i < objs.length; i++) {
oos.writeObject(objs[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 返回在文件中指定位置的對象
*
* @param file
* 指定的文件
* @param i
* 從1開始
* @return
*/
public Object getObjFromFile(String file, int i) {
ObjectInputStream ois = null;
Object obj = null;
try {
FileInputStream fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
for (int j = 0; j < i; j++) {
obj = ois.readObject();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
/**
* 測試
*
* @param args
*/
public static void main(String[] args) {
CryptTest jiami = new CryptTest();
// 執行MD5加密"Hello world!"
System.out.println("Hello經過MD5:" + jiami.encryptToMD5("Hello"));
// 生成一個DES演算法的密匙
SecretKey key = jiami.createSecretKey("DES");
// 用密匙加密信息"Hello world!"
String str1 = jiami.encryptToDES(key, "Hello");
System.out.println("使用des加密信息Hello為:" + str1);
// 使用這個密匙解密
String str2 = jiami.decryptByDES(key, str1);
System.out.println("解密後為:" + str2);
// 創建公匙和私匙
jiami.createPairKey();
// 對Hello world!使用私匙進行簽名
jiami.signToInfo("Hello", "mysign.bat");
// 利用公匙對簽名進行驗證。
if (jiami.validateSign("mysign.bat")) {
System.out.println("Success!");
} else {
System.out.println("Fail!");
}
}
}

『肆』 java中如何實現對文件和字元串加密. 解密

DES 密鑰生成,加解密方法,,你可以看一下

//DES 密鑰生成工具
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class GenKey {

private static final String DES = "DES";
public static final String SKEY_NAME = "key.des";

public static void genKey1(String path) {

// 密鑰
SecretKey skey = null;
// 密鑰隨機數生成
SecureRandom sr = new SecureRandom();
//生成密鑰文件
File file = genFile(path);

try {
// 獲取密鑰生成實例
KeyGenerator gen = KeyGenerator.getInstance(DES);
// 初始化密鑰生成器
gen.init(sr);
// 生成密鑰
skey = gen.generateKey();
// System.out.println(skey);

ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* @param file : 生成密鑰的路徑
* SecretKeyFactory 方式生成des密鑰
* */
public static void genKey2(String path) {
// 密鑰隨機數生成
SecureRandom sr = new SecureRandom();
// byte[] bytes = {11,12,44,99,76,45,1,8};
byte[] bytes = sr.generateSeed(20);
// 密鑰
SecretKey skey = null;
//生成密鑰文件路徑
File file = genFile(path);

try {
//創建deskeyspec對象
DESKeySpec desKeySpec = new DESKeySpec(bytes,9);
//實例化des密鑰工廠
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//生成密鑰對象
skey = keyFactory.generateSecret(desKeySpec);
//寫出密鑰對象
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

private static File genFile(String path) {
String temp = null;
File newFile = null;
if (path.endsWith("/") || path.endsWith("\\")) {
temp = path;
} else {
temp = path + "/";
}

File pathFile = new File(temp);
if (!pathFile.exists())
pathFile.mkdirs();

newFile = new File(temp+SKEY_NAME);

return newFile;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
genKey2("E:/a/aa/");
}

}

//DES加解密方法

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
*制卡文件加/解密 加密方式DES
*/
public class SecUtil {

public static final Log log = LogFactory.getLog(SecUtil.class);

/**
* 解密
*
* @param keyPath
* 密鑰路徑
* @param source
* 解密前文件
* @param dest
* 解密後文件
*/
public static void decrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try {
ObjectInputStream keyFile = new ObjectInputStream(
// 讀取加密密鑰
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
} catch (FileNotFoundException ey1) {
log.info("Error when read keyFile");
throw new RuntimeException(ey1);
} catch (Exception ey2) {
log.info("error when read the keyFile");
throw new RuntimeException(ey2);
}
// 用key產生Cipher
Cipher cipher = null;
try {
// 設置演算法,應該與加密時的設置一樣
cipher = Cipher.getInstance("DES");
// 設置解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception ey3) {
log.info("Error when create the cipher");
throw new RuntimeException(ey3);
}
// 取得要解密的文件並解密
File file = new File(source);
String filename = file.getName();
try {
// 輸出流,請注意文件名稱的獲取
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(dest));
// 輸入流
CipherInputStream in = new CipherInputStream(
new BufferedInputStream(new FileInputStream(file)), cipher);
int thebyte = 0;
while ((thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
} catch (Exception ey5) {
log.info("Error when encrypt the file");
throw new RuntimeException(ey5);
}
}

/**
* 加密
* @param keyPath 密鑰路徑
* @param source 加密前文件
* @param dest 加密後文件
*/
public static void encrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try {
ObjectInputStream keyFile = new ObjectInputStream(
// 讀取加密密鑰
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
} catch (FileNotFoundException ey1) {
log.info("Error when read keyFile");
throw new RuntimeException(ey1);
} catch (Exception ey2) {
log.info("error when read the keyFile");
throw new RuntimeException(ey2);
}
// 用key產生Cipher
Cipher cipher = null;
try {
// 設置演算法,應該與加密時的設置一樣
cipher = Cipher.getInstance("DES");
// 設置解密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (Exception ey3) {
log.info("Error when create the cipher");
throw new RuntimeException(ey3);
}
// 取得要解密的文件並解密
File file = new File(source);
String filename = file.getName();
try {
// 輸出流,請注意文件名稱的獲取
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(dest));
// 輸入流
CipherInputStream in = new CipherInputStream(
new BufferedInputStream(new FileInputStream(file)), cipher);
int thebyte = 0;
while ((thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
} catch (Exception ey5) {
log.info("Error when encrypt the file");
throw new RuntimeException(ey5);
}
}

}

『伍』 java中字元、字元串的常用方法

char的所有方法:
static int charCount(int codePoint)
確定表示指定字元(Unicode 代碼點)所需的 char 值的數量。
char charValue()
返回此 Character 對象的值。
static int codePointAt(char[] a, int index)
返回 char 數組的給定索引上的代碼點。
static int codePointAt(char[] a, int index, int limit)
返回 char 數組的給定索引上的代碼點,該數組中只有那些具有小於 limit 的 index 值的數組元素可以使用。
static int codePointAt(CharSequence seq, int index)
返回 CharSequence 的給定索引上的代碼點。
static int codePointBefore(char[] a, int index)
返回 char 數組的給定索引前面的代碼點。
static int codePointBefore(char[] a, int index, int start)
返回 char 數組的給定索引前面的代碼點,該數組中只有那些具有大於等於 start 的 index 值的數組元素可以使用。
static int codePointBefore(CharSequence seq, int index)
返回 CharSequence 的給定索引前面的代碼點。
static int codePointCount(char[] a, int offset, int count)
返回 char 數組參數的子數組中 Unicode 代碼點的數量。
static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
返回指定字元序列的文本范圍內的 Unicode 代碼點數量。
int compareTo(Character anotherCharacter)
根據數字比較兩個 Character 對象。
static int digit(char ch, int radix)
返回使用指定基數的字元 ch 的數值。
static int digit(int codePoint, int radix)
返回使用指定基數的指定字元(Unicode 代碼點)的數值。
boolean equals(Object obj)
將此對象與指定對象比較。
static char forDigit(int digit, int radix)
確定使用指定基數的特定數字的字元表示形式。
static byte getDirectionality(char ch)
返回給定字元的 Unicode 方向屬性。
static byte getDirectionality(int codePoint)
返回給定字元(Unicode 代碼點)的 Unicode 方向屬性。
static int getNumericValue(char ch)
返回指定的 Unicode 字元表示的 int 值。
static int getNumericValue(int codePoint)
返回指定字元(Unicode 代碼點)表示的 int 值。
static int getType(char ch)
返回一個指示字元的常規類別的值。
static int getType(int codePoint)
返回一個指示字元的常規類別的值。
int hashCode()
返回此 Character 的哈希碼。
static boolean isDefined(char ch)
確定字元是否被定義為 Unicode 中的字元。
static boolean isDefined(int codePoint)
確定字元(Unicode 代碼點)是否被定義為 Unicode 中的字元。
static boolean isDigit(char ch)
確定指定字元是否為數字。
static boolean isDigit(int codePoint)
確定指定字元(Unicode 代碼點)是否為數字。
static boolean isHighSurrogate(char ch)
確定給出的 char 值是否為一個高代理項代碼單元(也稱為前導代理項代碼單元)。
static boolean isIdentifierIgnorable(char ch)
確定是否應該認為指定字元是 Java 標識符或 Unicode 標識符中可忽略的一個字元。
static boolean isIdentifierIgnorable(int codePoint)
確定是否應該認為指定字元(Unicode 代碼點)是 Java 標識符或 Unicode 標識符中可忽略的一個字元。
static boolean isISOControl(char ch)
確定指定字元是否為 ISO 控制字元。
static boolean isISOControl(int codePoint)
確定引用的字元(Unicode 代碼點)是否為 ISO 控制字元。
static boolean isJavaIdentifierPart(char ch)
確定指定字元是否可以是 Java 標識符中首字元以外的部分。
static boolean isJavaIdentifierPart(int codePoint)
確定字元(Unicode 代碼點)是否可以是 Java 標識符中首字元以外的部分。
static boolean isJavaIdentifierStart(char ch)
確定是否允許將指定字元作為 Java 標識符中的首字元。
static boolean isJavaIdentifierStart(int codePoint)
確定是否允許將字元(Unicode 代碼點)作為 Java 標識符中的首字元。
static boolean isJavaLetter(char ch)
已過時。 由 isJavaIdentifierStart(char) 取代。
static boolean isJavaLetterOrDigit(char ch)
已過時。 由 isJavaIdentifierPart(char) 取代。
static boolean isLetter(char ch)
確定指定字元是否為字母。
static boolean isLetter(int codePoint)
確定指定字元(Unicode 代碼點)是否為字母。
static boolean isLetterOrDigit(char ch)
確定指定字元是否為字母或數字。
static boolean isLetterOrDigit(int codePoint)
確定指定字元(Unicode 代碼點)是否為字母或數字。
static boolean isLowerCase(char ch)
確定指定字元是否為小寫字母。
static boolean isLowerCase(int codePoint)
確定指定字元(Unicode 代碼點)是否為小寫字母。
static boolean isLowSurrogate(char ch)
確定給定 char 值是否一個低代理項代碼單元(也稱為尾部代理項代碼單元)。
static boolean isMirrored(char ch)
確定指定字元依據 Unicode 規范是否對稱。
static boolean isMirrored(int codePoint)
確定指定字元(Unicode 代碼點)依據 Unicode 規范是否對稱。
static boolean isSpace(char ch)
已過時。 由 isWhitespace(char) 取代。
static boolean isSpaceChar(char ch)
確定指定字元是否為 Unicode 空白字元。
static boolean isSpaceChar(int codePoint)
確定指定字元(Unicode 代碼點)是否為 Unicode 空白字元。
static boolean isSupplementaryCodePoint(int codePoint)
確定指定字元(Unicode 代碼點)是否在增補字元范圍內。
static boolean isSurrogatePair(char high, char low)
確定指定的 char 值對是否為有效的代理項對。
static boolean isTitleCase(char ch)
確定指定字元是否為首字母大寫字元。
static boolean isTitleCase(int codePoint)
確定指定字元(Unicode 代碼點)是否為首字母大寫字元。
static boolean isUnicodeIdentifierPart(char ch)
確定指定字元是否可以是 Unicode 標識符中首字元以外的部分。
static boolean isUnicodeIdentifierPart(int codePoint)
確定指定字元(Unicode 代碼點)是否可以是 Unicode 標識符中首字元以外的部分。
static boolean isUnicodeIdentifierStart(char ch)
確定是否允許將指定字元作為 Unicode 標識符中的首字元。
static boolean isUnicodeIdentifierStart(int codePoint)
確定是否允許將指定字元(Unicode 代碼點)作為 Unicode 標識符中的首字元。
static boolean isUpperCase(char ch)
確定指定字元是否為大寫字母。
static boolean isUpperCase(int codePoint)
確定指定字元(Unicode 代碼點)是否為大寫字母。
static boolean isValidCodePoint(int codePoint)
確定指定的代碼點是否為從 0x0000 到 0x10FFFF 范圍之內的有效 Unicode 代碼點值。
static boolean isWhitespace(char ch)
確定指定字元依據 Java 標準是否為空白字元。
static boolean isWhitespace(int codePoint)
確定指定字元(Unicode 代碼點)依據 Java 標準是否為空白字元。
static int offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset)
返回給定 char 子數組中的索引,它是從給定 index 到 codePointOffset 代碼點的偏移量。
static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)
返回給定字元序列中的索引,它是從給定 index 到 codePointOffset 代碼點的偏移量。
static char reverseBytes(char ch)
返回通過反轉指定 char 值中的位元組順序而獲得的值。
static char[] toChars(int codePoint)
將指定的字元(Unicode 代碼點)轉換成其存儲在 char 數組中的 UTF-16 表示形式。
static int toChars(int codePoint, char[] dst, int dstIndex)
將指定字元(Unicode 代碼點)轉換為其 UTF-16 表示形式。
static int toCodePoint(char high, char low)
將指定的代理項對轉換為其增補代碼點值。
static char toLowerCase(char ch)
使用取自 UnicodeData 文件的大小寫映射信息將字元參數轉換為小寫。
static int toLowerCase(int codePoint)
使用取自 UnicodeData 文件的大小寫映射信息將字元(Unicode 代碼點)參數轉換為小寫。
String toString()
返回表示此 Character 值的 String 對象。
static String toString(char c)
返回一個表示指定 char 值的 String 對象。
static char toTitleCase(char ch)
使用取自 UnicodeData 文件的大小寫映射信息將字元參數轉換為首字母大寫。
static int toTitleCase(int codePoint)
使用取自 UnicodeData 文件的大小寫映射信息將字元(Unicode 代碼點)參數轉換為首字母大寫。
static char toUpperCase(char ch)
使用取自 UnicodeData 文件的大小寫映射信息將字元參數轉換為大寫。
static int toUpperCase(int codePoint)
使用取自 UnicodeData 文件的大小寫映射信息將字元(Unicode 代碼點)參數轉換為大寫。
static Character valueOf(char c)
返回一個表示指定 char 值的 Character 實例

string所有方法:
char charAt(int index)
返回指定索引處的 char 值。
int codePointAt(int index)
返回指定索引處的字元(Unicode 代碼點)。
int codePointBefore(int index)
返回指定索引之前的字元(Unicode 代碼點)。
int codePointCount(int beginIndex, int endIndex)
返回此 String 的指定文本范圍中的 Unicode 代碼點數。
int compareTo(String anotherString)
按字典順序比較兩個字元串。
int compareToIgnoreCase(String str)
按字典順序比較兩個字元串,不考慮大小寫。
String concat(String str)
將指定字元串連接到此字元串的結尾。
boolean contains(CharSequence s)
當且僅當此字元串包含指定的 char 值序列時,返回 true。
boolean contentEquals(CharSequence cs)
將此字元串與指定的 CharSequence 比較。
boolean contentEquals(StringBuffer sb)
將此字元串與指定的 StringBuffer 比較。
static String ValueOf(char[] data)
返回指定數組中表示該字元序列的 String。
static String ValueOf(char[] data, int offset, int count)
返回指定數組中表示該字元序列的 String。
boolean endsWith(String suffix)
測試此字元串是否以指定的後綴結束。
boolean equals(Object anObject)
將此字元串與指定的對象比較。
boolean equalsIgnoreCase(String anotherString)
將此 String 與另一個 String 比較,不考慮大小寫。
static String format(Locale l, String format, Object... args)
使用指定的語言環境、格式字元串和參數返回一個格式化字元串。
static String format(String format, Object... args)
使用指定的格式字元串和參數返回一個格式化字元串。
byte[] getBytes()
使用平台的默認字元集將此 String 編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中。
byte[] getBytes(Charset charset)
使用給定的 charset 將此 String 編碼到 byte 序列,並將結果存儲到新的 byte 數組。
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
已過時。 該方法無法將字元正確轉換為位元組。從 JDK 1.1 起,完成該轉換的首選方法是通過 getBytes() 方法,該方法使用平台的默認字元集。
byte[] getBytes(String charsetName)
使用指定的字元集將此 String 編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將字元從此字元串復制到目標字元數組。
int hashCode()
返回此字元串的哈希碼。
int indexOf(int ch)
返回指定字元在此字元串中第一次出現處的索引。
int indexOf(int ch, int fromIndex)
返回在此字元串中第一次出現指定字元處的索引,從指定的索引開始搜索。
int indexOf(String str)
返回指定子字元串在此字元串中第一次出現處的索引。
int indexOf(String str, int fromIndex)
返回指定子字元串在此字元串中第一次出現處的索引,從指定的索引開始。
String intern()
返回字元串對象的規范化表示形式。
boolean isEmpty()
當且僅當 length() 為 0 時返回 true。
int lastIndexOf(int ch)
返回指定字元在此字元串中最後一次出現處的索引。
int lastIndexOf(int ch, int fromIndex)
返回指定字元在此字元串中最後一次出現處的索引,從指定的索引處開始進行反向搜索。
int lastIndexOf(String str)
返回指定子字元串在此字元串中最右邊出現處的索引。
int lastIndexOf(String str, int fromIndex)
返回指定子字元串在此字元串中最後一次出現處的索引,從指定的索引開始反向搜索。
int length()
返回此字元串的長度。
boolean matches(String regex)
告知此字元串是否匹配給定的正則表達式。
int offsetByCodePoints(int index, int codePointOffset)
返回此 String 中從給定的 index 處偏移 codePointOffset 個代碼點的索引。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字元串區域是否相等。
boolean regionMatches(int toffset, String other, int ooffset, int len)
測試兩個字元串區域是否相等。
String replace(char oldChar, char newChar)
返回一個新的字元串,它是通過用 newChar 替換此字元串中出現的所有 oldChar 得到的。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替換序列替換此字元串所有匹配字面值目標序列的子字元串。
String replaceAll(String regex, String replacement)
使用給定的 replacement 替換此字元串所有匹配給定的正則表達式的子字元串。
String replaceFirst(String regex, String replacement)
使用給定的 replacement 替換此字元串匹配給定的正則表達式的第一個子字元串。
String[] split(String regex)
根據給定正則表達式的匹配拆分此字元串。
String[] split(String regex, int limit)
根據匹配給定的正則表達式來拆分此字元串。
boolean startsWith(String prefix)
測試此字元串是否以指定的前綴開始。
boolean startsWith(String prefix, int toffset)
測試此字元串從指定索引開始的子字元串是否以指定前綴開始。
CharSequence subSequence(int beginIndex, int endIndex)
返回一個新的字元序列,它是此序列的一個子序列。
String substring(int beginIndex)
返回一個新的字元串,它是此字元串的一個子字元串。
String substring(int beginIndex, int endIndex)
返回一個新字元串,它是此字元串的一個子字元串。
char[] toCharArray()
將此字元串轉換為一個新的字元數組。
String toLowerCase()
使用默認語言環境的規則將此 String 中的所有字元都轉換為小寫。
String toLowerCase(Locale locale)
使用給定 Locale 的規則將此 String 中的所有字元都轉換為小寫。
String toString()
返回此對象本身(它已經是一個字元串!)。
String toUpperCase()
使用默認語言環境的規則將此 String 中的所有字元都轉換為大寫。
String toUpperCase(Locale locale)
使用給定 Locale 的規則將此 String 中的所有字元都轉換為大寫。
String trim()
返回字元串的副本,忽略前導空白和尾部空白。
static String valueOf(boolean b)
返回 boolean 參數的字元串表示形式。
static String valueOf(char c)
返回 char 參數的字元串表示形式。
static String valueOf(char[] data)
返回 char 數組參數的字元串表示形式。
static String valueOf(char[] data, int offset, int count)
返回 char 數組參數的特定子數組的字元串表示形式。
static String valueOf(double d)
返回 double 參數的字元串表示形式。
static String valueOf(float f)
返回 float 參數的字元串表示形式。
static String valueOf(int i)
返回 int 參數的字元串表示形式。
static String valueOf(long l)
返回 long 參數的字元串表示形式。
static String valueOf(Object obj)
返回 Object 參數的字元串表示形式

『陸』 JAVA題:編寫一個加密程序,對用戶輸入的字元串加密後輸出。加密的方法是將每個字元映射成字母表中的對稱

publicStringgetPass(Stringstr){
Stringstr="";
for(inti=0;i<str.length();i++){
charch=str.charAt(i);//得到單個字元ch;
if(ch>64&&ch<91){//成立=>ch是大寫字母
str+=(char)(155-ch);//大寫對應字母之和:155eg:'A'+'Z'=155
}elseif(ch>96&&ch<123){//成立則ch是小寫字母
str+=(char)(219-ch);//小寫對應字母之和:219如:'a'+'z'=219
}else{
str="";//若進入此處,則說明str中有非字母字元
returnstr;//直接退出
}
}
returnstr;//返回str的值;
}
樓主估計是新手,此處程序鄙人就不進一步優化。(可優化)

『柒』 Java 中文字元串比較

public boolean equals(Object obj)指示某個其他對象是否與此對象「相等」。
equals 方法在非空對象引用上實現相等關系:

自反性:對於任何非空引用值 x,x.equals(x) 都應返回 true。
對稱性:對於任何非空引用值 x 和 y,當且僅當 y.equals(x) 返回 true 時,x.equals(y) 才應返回 true。
傳遞性:對於任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,並且 y.equals(z) 返回 true,那麼 x.equals(z) 應返回 true。
一致性:對於任何非空引用值 x 和 y,多次調用 x.equals(y) 始終返回 true 或始終返回 false,前提是對象上 equals 比較中所用的信息沒有被修改。
對於任何非空引用值 x,x.equals(null) 都應返回 false。
Object 類的 equals 方法實現對象上差別可能性最大的相等關系;即,對於任何非空引用值 x 和 y,當且僅當 x 和 y 引用同一個對象時,此方法才返回 true(x == y 具有值 true)。

注意:當此方法被重寫時,通常有必要重寫 hashCode 方法,以維護 hashCode 方法的常規協定,該協定聲明相等對象必須具有相等的哈希碼。

參數:
obj - 要與之比較的引用對象。
返回:
如果此對象與 obj 參數相同,則返回 true;否則返回 false。

『捌』 java基礎題判斷一個字元串是否是對稱字元串

建議以下兩種方法:
方法一:通過取取索引對應值來進行一一比對
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = input.next();//接收任意字元串

isOk1(str);
}
/**
* 判斷字元串是否對稱的方法(一)
* 通過取取索引對應值來進行一一比對
* @param str
*/
public static void isOk1(String str){
boolean result = true;
int count =(str.length()-1)/2;
for (int x=0;x<=count;x++ ){
if(str.charAt(x)!=str.charAt(str.length()-1-x)){
result = false;
break;
}
}
if(!result)
System.out.println("該字元串是不對稱的");
else
System.out.println("該字元串是對稱的");
}
方法二:通過String加強類中的取反方法reverse獲取其逆向值
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = input.next();//接收任意字元串
if (isOk2(str)==true) {
System.out.println("真,對稱!");
}else{
System.out.println("假,不對稱!");
}

}
/**
* 方法二
* 通過String加強類中的取反方法reverse獲取其逆向值
* 再與原字元串相比是否相等!
* 等於則返回TRUE,否則FALSE
* @param str
* @return
*/
public static boolean isOk2(String str){
StringBuffer sb = new StringBuffer(str);
String str2 = sb.reverse().toString();
return str.equals(str2);
}

『玖』 如何使用JAVA實現對字元串的DES加密和解密

java加密字元串可以使用des加密演算法,實例如下:
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* 加密解密
*
* @author shy.qiu
* @since http://blog.csdn.net/qiushyfm
*/
public class CryptTest {
/**
* 進行MD5加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToMD5(String info) {
byte[] digesta = null;
try {
// 得到一個md5的消息摘要
MessageDigest alga = MessageDigest.getInstance("MD5");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
/**
* 進行SHA加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToSHA(String info) {
byte[] digesta = null;
try {
// 得到一個SHA-1的消息摘要
MessageDigest alga = MessageDigest.getInstance("SHA-1");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
// //////////////////////////////////////////////////////////////////////////
/**
* 創建密匙
*
* @param algorithm
* 加密演算法,可用 DES,DESede,Blowfish
* @return SecretKey 秘密(對稱)密鑰
*/
public SecretKey createSecretKey(String algorithm) {
// 聲明KeyGenerator對象
KeyGenerator keygen;
// 聲明 密鑰對象
SecretKey deskey = null;
try {
// 返回生成指定演算法的秘密密鑰的 KeyGenerator 對象
keygen = KeyGenerator.getInstance(algorithm);
// 生成一個密鑰
deskey = keygen.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 返回密匙
return deskey;
}
/**
* 根據密匙進行DES加密
*
* @param key
* 密匙
* @param info
* 要加密的信息
* @return String 加密後的信息
*/
public String encryptToDES(SecretKey key, String info) {
// 定義 加密演算法,可用 DES,DESede,Blowfish
String Algorithm = "DES";
// 加密隨機數生成器 (RNG),(可以不寫)
SecureRandom sr = new SecureRandom();
// 定義要生成的密文
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
// 參數:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
c1.init(Cipher.ENCRYPT_MODE, key, sr);
// 對要加密的內容進行編碼處理,
cipherByte = c1.doFinal(info.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
// 返回密文的十六進制形式
return byte2hex(cipherByte);
}
/**
* 根據密匙進行DES解密
*
* @param key
* 密匙
* @param sInfo
* 要解密的密文
* @return String 返回解密後信息
*/
public String decryptByDES(SecretKey key, String sInfo) {
// 定義 加密演算法,
String Algorithm = "DES";
// 加密隨機數生成器 (RNG)
SecureRandom sr = new SecureRandom();
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
c1.init(Cipher.DECRYPT_MODE, key, sr);
// 對要解密的內容進行編碼處理
cipherByte = c1.doFinal(hex2byte(sInfo));
} catch (Exception e) {
e.printStackTrace();
}
// return byte2hex(cipherByte);
return new String(cipherByte);
}
// /////////////////////////////////////////////////////////////////////////////
/**
* 創建密匙組,並將公匙,私匙放入到指定文件中
*
* 默認放入mykeys.bat文件中
*/
public void createPairKey() {
try {
// 根據特定的演算法一個密鑰對生成器
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
// 加密隨機數生成器 (RNG)
SecureRandom random = new SecureRandom();
// 重新設置此隨機對象的種子
random.setSeed(1000);
// 使用給定的隨機源(和默認的參數集合)初始化確定密鑰大小的密鑰對生成器
keygen.initialize(512, random);// keygen.initialize(512);
// 生成密鑰組
KeyPair keys = keygen.generateKeyPair();
// 得到公匙
PublicKey pubkey = keys.getPublic();
// 得到私匙
PrivateKey prikey = keys.getPrivate();
// 將公匙私匙寫入到文件當中
doObjToFile("mykeys.bat", new Object[] { prikey, pubkey });
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* 利用私匙對信息進行簽名 把簽名後的信息放入到指定的文件中
*
* @param info
* 要簽名的信息
* @param signfile
* 存入的文件
*/
public void signToInfo(String info, String signfile) {
// 從文件當中讀取私匙
PrivateKey myprikey = (PrivateKey) getObjFromFile("mykeys.bat", 1);
// 從文件中讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile("mykeys.bat", 2);
try {
// Signature 對象可用來生成和驗證數字簽名
Signature signet = Signature.getInstance("DSA");
// 初始化簽署簽名的私鑰
signet.initSign(myprikey);
// 更新要由位元組簽名或驗證的數據
signet.update(info.getBytes());
// 簽署或驗證所有更新位元組的簽名,返回簽名
byte[] signed = signet.sign();
// 將數字簽名,公匙,信息放入文件中
doObjToFile(signfile, new Object[] { signed, mypubkey, info });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 讀取數字簽名文件 根據公匙,簽名,信息驗證信息的合法性
*
* @return true 驗證成功 false 驗證失敗
*/
public boolean validateSign(String signfile) {
// 讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile(signfile, 2);
// 讀取簽名
byte[] signed = (byte[]) getObjFromFile(signfile, 1);
// 讀取信息
String info = (String) getObjFromFile(signfile, 3);
try {
// 初始一個Signature對象,並用公鑰和簽名進行驗證
Signature signetcheck = Signature.getInstance("DSA");
// 初始化驗證簽名的公鑰
signetcheck.initVerify(mypubkey);
// 使用指定的 byte 數組更新要簽名或驗證的數據
signetcheck.update(info.getBytes());
System.out.println(info);
// 驗證傳入的簽名
return signetcheck.verify(signed);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將二進制轉化為16進制字元串
*
* @param b
* 二進制位元組數組
* @return String
*/
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
/**
* 十六進制字元串轉化為2進制
*
* @param hex
* @return
*/
public byte[] hex2byte(String hex) {
byte[] ret = new byte[8];
byte[] tmp = hex.getBytes();
for (int i = 0; i < 8; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
/**
* 將兩個ASCII字元合成一個位元組; 如:"EF"--> 0xEF
*
* @param src0
* byte
* @param src1
* byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
.byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
.byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
/**
* 將指定的對象寫入指定的文件
*
* @param file
* 指定寫入的文件
* @param objs
* 要寫入的對象
*/
public void doObjToFile(String file, Object[] objs) {
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
for (int i = 0; i < objs.length; i++) {
oos.writeObject(objs[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 返回在文件中指定位置的對象
*
* @param file
* 指定的文件
* @param i
* 從1開始
* @return
*/
public Object getObjFromFile(String file, int i) {
ObjectInputStream ois = null;
Object obj = null;
try {
FileInputStream fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
for (int j = 0; j < i; j++) {
obj = ois.readObject();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
/**
* 測試
*
* @param args
*/
public static void main(String[] args) {
CryptTest jiami = new CryptTest();
// 執行MD5加密"Hello world!"
System.out.println("Hello經過MD5:" + jiami.encryptToMD5("Hello"));
// 生成一個DES演算法的密匙
SecretKey key = jiami.createSecretKey("DES");
// 用密匙加密信息"Hello world!"
String str1 = jiami.encryptToDES(key, "Hello");
System.out.println("使用des加密信息Hello為:" + str1);
// 使用這個密匙解密
String str2 = jiami.decryptByDES(key, str1);
System.out.println("解密後為:" + str2);
// 創建公匙和私匙
jiami.createPairKey();
// 對Hello world!使用私匙進行簽名
jiami.signToInfo("Hello", "mysign.bat");
// 利用公匙對簽名進行驗證。
if (jiami.validateSign("mysign.bat")) {
System.out.println("Success!");
} else {
System.out.println("Fail!");
}
}
}

閱讀全文

與java對稱字元串相關的資料

熱點內容
扣扣加密技巧 瀏覽:720
蘋果如何創建伺服器錯誤 瀏覽:495
軟考初級程序員大題分值 瀏覽:473
js壓縮視頻文件 瀏覽:578
linux如何通過命令創建文件 瀏覽:989
應用加密app還能訪問應用嘛 瀏覽:433
安卓怎麼用支付寶交違章罰款 瀏覽:665
php面向對象的程序設計 瀏覽:504
數據挖掘演算法書籍推薦 瀏覽:894
投訴聯通用什麼app 瀏覽:150
web伺服器變更ip地址 瀏覽:954
java正則表達式驗證郵箱 瀏覽:360
成熟商務男裝下載什麼軟體app 瀏覽:609
加密2h代表長度是多少厘米 瀏覽:23
拍賣程序員 瀏覽:101
電腦的圖片放在哪個文件夾 瀏覽:276
unsignedintjava 瀏覽:217
編譯器下載地址 瀏覽:43
什麼是面對對象編程 瀏覽:709
b站伺服器什麼時候恢復 瀏覽:721