永发信息网

Java 字符加密

答案:4  悬赏:60  手机版
解决时间 2021-04-15 05:35
1、定义一个字符串,用1234进行加密后输出。

2、定义一个大小写混合的字符串,然后将其中的大写字母改成小写,小写字母改成大写。

最佳答案

1题的代码如下:


class jiami{
public static void main(String[] s){
String q="liuyutong";
for(int a=1;a<q.length();a++){
System.out.print((char)(q.charAt(a)^123));
}
}
}



2题的代码如下:


public class lianxi{
public static void main(String[] s){
String q="liuYUtong".toLowerCase();
String a="liuYUtong".toUpperCase();
System.out.println(q);
System.out.println(a);
}

全部回答

第一个加密问题你说的太抽象了,双向加密有好多种,像你以1234为key加密以上发的des是行不通的。

回答你第二个问题吧,实际上转换大小写的思想就是转换ascii码值,小写的a对应的是ascii码是97,大写的A对应的ascii码是65。实际上小写a转大写A就是97 - 32。同理大写A转小写a就是65 + 32。以下附上代码。

public class Test { public static void main(String[] args) { String str = "abcSDFsdfvn"; StringBuffer ss = new StringBuffer(); //用来保存转换后的字母 char[] ch = str.toCharArray(); for(int i = 0; i < ch.length; i++){ if(ch[i] >= 'a' && ch[i] <= 'z'){ //如果是小写字母 ch[i] = (char)((int)ch[i] - 32); //转成大写字母 }else if(ch[i] >= 'A' && ch[i] <= 'Z'){ ch[i] = (char)((int)ch[i] + 32); //转成小写字母 } ss.append(ch[i]); } System.out.println(ss.toString()); } }

public class MD5 {

static final int S11 = 7;

static final int S12 = 12;

static final int S13 = 17;

static final int S14 = 22;

static final int S21 = 5;

static final int S22 = 9;

static final int S23 = 14;

static final int S24 = 20;

static final int S31 = 4;

static final int S32 = 11;

static final int S33 = 16;

static final int S34 = 23;

static final int S41 = 6;

static final int S42 = 10;

static final int S43 = 15;

static final int S44 = 21;

static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

private long[] state = new long[4]; // state (ABCD)

private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb

// first)

private byte[] buffer = new byte[64]; // input buffer

public String digestHexStr;

private byte[] digest = new byte[16];

public String getMD5ofStr(String inbuf) { md5Init(); md5Update(inbuf.getBytes(), inbuf.length()); md5Final(); digestHexStr = ""; for (int i = 0; i < 16; i++) { digestHexStr += byteHEx(digest[i]); } return digestHexStr;

}

// 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数 public MD5() { md5Init();

return; }

private void md5Init() { count[0] = 0L; count[1] = 0L; //

private long F(long x, long y, long z) { return (x & y) | ((~x) & z);

}

private long G(long x, long y, long z) { return (x & z) | (y & (~z));

}

private long H(long x, long y, long z) { return x ^ y ^ z; }

private long I(long x, long y, long z) { return y ^ (x | (~z)); }

private long FF(long a, long b, long c, long d, long x, long s, long ac) { a += F(b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; }

private long GG(long a, long b, long c, long d, long x, long s, long ac) { a += G(b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; }

private long HH(long a, long b, long c, long d, long x, long s, long ac) { a += H(b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; }

private long II(long a, long b, long c, long d, long x, long s, long ac) { a += I(b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; }

private void md5Update(byte[] inbuf, int inputLen) {

int i, index, partLen; byte[] block = new byte[64]; index = (int) (count[0] >>> 3) & 0x3F; // if ((count[0] += (inputLen << 3)) < (inputLen << 3)) count[1]++; count[1] += (inputLen >>> 29);

partLen = 64 - index;

// Transform as many times as possible. if (inputLen >= partLen) { md5Memcpy(buffer, inbuf, index, 0, partLen); md5Transform(buffer);

for (i = partLen; i + 63 < inputLen; i += 64) {

md5Memcpy(block, inbuf, 0, i, 64); md5Transform(block); } index = 0;

} else

i = 0;

// md5Memcpy(buffer, inbuf, index, i, inputLen - i);

}

private void md5Final() { byte[] bits = new byte[8]; int index, padLen;

// Encode(bits, count, 8);

// md5Update(bits, 8);

// Encode(digest, state, 16);

}

private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos, int len) { int i;

for (i = 0; i < len; i++) output[outpos + i] = input[inpos + i]; }

import java.lang.*;public class DES { final private int IP[] = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; final private int IP_1[] = { 40,8,48,16,56,24,64,32, 39,7,47,15,55,23,63,31, 38,6,46,14,54,22,62,30, 37,5,45,13,53,21,61,29, 36,4,44,12,52,20,60,28, 35,3,43,11,51,19,59,27, 34,2,42,10,50,18,58,26, 33,1,41,9,49,17,57,25}; final private int PC_1[] = { 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; final private int PC_2[] = { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; final private int Esapansion[] = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; final private int P_trans[] = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }; final private int S[][]={ {14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7, 0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8, 4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0, 15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}, {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10, 3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5, 0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15, 13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}, {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8, 13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1, 13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7, 1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}, {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15, 13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9, 10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4, 3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}, { 2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9, 14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6, 4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14, 11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3, }, {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11, 10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8, 9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6, 4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}, {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1, 13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6, 1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2, 6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}, {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7, 1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2, 7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8, 2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11} }; final private int SHIFT[]={ 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1}; public String KEY []=new String [16]; public char[] IPtrans(char a[], int choice) { char temp[] = new char[64]; if (choice == 1) { for (int i = 0; i < 64; i++) temp[i] = a[IP[i]-1]; } else if(choice==0) { for (int i = 0; i < 64; i++){ temp[i] = a[IP_1[i]-1];// System.out.print(temp[i]); } } return temp; } //用于得到16个密钥 public void keymaker(String key){ char temp2[]=new char[56]; char[] temp3=key.toCharArray(); for(int t=0;t<56;t++) temp2[t]=temp3[PC_1[t]-1]; key=""; for(int t1=0;t1<56;t1++){ key+=temp2[t1]; } for(int i=0;i<16;i++) { String s1=key.substring(0, 28); String s2=key.substring(28); String k=shift(s1,i)+shift(s2,i); char temp[]=k.toCharArray(); KEY[i]=""; for(int j=0;j<48;j++) KEY[i]+=temp[PC_2[j]-1]; key=k; } } private String shift(String str,int c){ int n=SHIFT[c]; String temp3=str.substring(n, str.length())+str.substring(0, n); return temp3; } public char[] E_span(char[] Ri){ char temp[]=new char[48]; for(int i=0;i<48;i++) temp[i]=Ri[Esapansion[i]-1]; return temp; } public char[] XOR(char a[],char b[]){ for(int i=0;i<a.length;i++){// System.out.print(a[i]);// System.out.println(b[i]); if(a[i]==b[i]) a[i]='0'; else a[i]='1'; } return a; } public char[] S_box(char b[]){//48 bit String a=""; for(int t=0;t<b.length;t++) a+=b[t]; // System.out.println(a); String temp=""; for(int i=0;i<8;i++){ String hang=a.substring(i*6, i*6+1)+a.substring(i*6+5, i*6+6); String lie=a.substring(i*6+1, i*6+5);// System.out.println(hang); int index=Integer.parseInt(hang, 2)*16+Integer.parseInt(lie, 2); temp+=getEigthBitsStringFromByte(S[i][index]); } return temp.toCharArray(); } public String getEigthBitsStringFromByte(int b) { String temp=""; b |= 32; String str = Integer.toBinaryString(b); int len = str.length(); temp = str.substring(len - 4, len); return temp; } public char[] p_trans(char[] a){ char b []=new char[32];// System.out.println(a.length); for(int i=0;i<32;i++){ b[i]=a[P_trans[i]-1]; } return b; }}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
地下城与勇士怎么玩?
苹果手机越狱还是不越狱好呀 好纠结 听说费电
垮石岩怎么去啊,有知道地址的么
浙江横店在义乌吗?
一根木料长6米,它有一组相对的面是正方形,
DNF冰子的博客在哪里?
概率总是错,不知道重复的写不写,这几次考试
拉直头发的时候可以戴眼镜吗?
哎呀收集有关树的谚语,我们可以天翻地覆彻夜
在美容院做事,未知怀孕,快两个月,期间做过
怎样拨打要转接的电话?
中山大学河南分数线一般比一本线分高多少(理
名豪国际酒店-桑拿地址在哪,我要去那里办事
降低温度饱和溶液析出晶体前后,不变的是A.溶
问世间情是何物?
推荐资讯
有关remembering and forgetting的三分钟英语
qq宠物怎么赚钱
杭州有什么好吃的
清屿市在哪个地方
medicine works in a human body is a questi
能够近义词是什么,淘宝帮忙拍东西是什么意思
人称攀枝花是什么意思,攀枝花米易有哪些名小
物业环境部门口号,物业服务中心誓师口号
潘德的语言 冒险者队伍包括哪些
莱迪·时尚名品生活馆这个地址在什么地方,我
有没有与这相匹配的西方童话故事??
单选题α、β都是锐角,且cosα<cosβ,则下
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?