# 凯撒（Caesar-Cypher）-Mania

22

``````(C - 32 + d) % 95 + 32
``````

12

# Cjam，40 38 37字节

`````` q~'~),32>_@m<er "o|%|'*10<]>k<cpZ"_-
``````

``````"s!)!+.54@aBo>gt"\$q~'~),32>_@m>er\\$a/
``````

``````q~'~),32>_@m<er
q~                 "Take the input and evaluate it";
`~)              "Get the next character after the printable ASCII range";
,32>          "Get all printable ASCII characters":
_@        "Copy the printable ASCII string and bring the cypher difference"
"on top of stack";
m<      "Forward rotate the copy of printable ASCII string by difference";
"In case of inverse Cypher, this is m> to reverse rotate the string";
er    "Transliterate to complete the forward/inverse Cypher";
``````

``````<space> -> "     // Empty space to string conversion
Z -> \           // Character Z in an useless string to swap operation
_ -> a           // Copy operation to wrapping in an array
- -> /           // Set subtraction to string splitting
``````

`````` q~'~),32>_@m<er "o|%|'*10<]>k<cpZ"_-
q~'~),32>_@m<er                          "no-op space, Forward cypher, no-op space";
"o|%|'*10<]>k<cpZ"       "Useless String (Actually not)";
_      "Copy it and ..."
-     "remove all alphabets of copy from original";
``````

``````"s!)!+.54@aBo>gt"\$q~'~),32>_@m>er\\$a/
"s!)!+.54@aBo>gt"                       "Cypher of first part of first program"
"with difference of 2";
\$q~'~),32>_@m>er\\$a/   "Cypher of the useless string of first program";
"with difference 2";
\$                      "Sort the first program's main part's cypher";
q~'~),32>_@m>er       "Program to reverse cypher";
\\$     "Swap the cypher to the top of stack and sort it";
a    "Wrap it in array";
/   "Split the output string on an array, which";
"always returns the output in an array as there";
"are no occurrences of an array in a string";
``````

``````"abcd" 4
``````

``````efgh
``````

``````]^_`
``````

Def 2014年

@Deformyer更正了字节数。您将输入作为什么？
Optimizer

Def 2014年

'“ q〜'〜），32> _ @ m <er” 9} o |％|'* 10 <]> k <cp}]“ _-” 2'不起作用（java.lang.RuntimeException：出乎意料的}）
Def 2014年

1
@Deformyer您必须转义该字符串中的引号
Optimizer

7

# Python 2，147

## 向前

``````Z=lambda s,d:s.translate({i+32:(i+d)%95+32for i in range(95)})or u''and Z
"uE:F;=:XLd=rLfMK:GLE:M>`TBckjr`Be=a]qmckj?HKXBXBGXK:G@>`qmaVaHKXN__:G=X"
``````

## 逆

``````"d4)5*,)G;S,a;U<:)6;4)<-OC1RZYaO1R,PL`\RZY.7:G1G16G:)6/-O`\PEP7:G=NN)6,G"
I=lambda s,d:s.translate({i+32:(i-d)%95+32for i in range(95)})or u''and I
``````

5

# Python 3-248字节

``r=q="".__doc__[2];eval("p"+q+"int(''.join([c,ch"+q+"((o"+q+"d(c)-32+d)%95+32)][31<o"+q+"d(c)<127]fo"+q+" d in[int(input())]fo"+q+" c in input()))")or'\^UZ`smmyV[UZsGOwOT^ss[^PsOtx~}xPtp%!v~}tIG~|([^PsOt(|}\$IR[^kPkUZGUZ`sUZ\a`sttIR[^kOkUZkUZ\a`sttt'``

``'Q&Q66Bssx\$wssoFqOy+u!<6%6?&?6}#)<;;B~\$}#<ow@w|6?&?6<<\$6?&?6x<w=AGF?x=9MI?GF=qoGEP\$6?&?6x<w=PEFKqz\$6?&?64x4}#o}#)<}#%*)<==qz\$6?&?64w4}#4}#%*)<===6=\$';print("".join([c,chr((ord(c)-32-d)%95+32)][31<ord(c)<127]for d in[int(input())]for c in input()));``

``````5
hello
``````

## 说明

``````r -> '
' -> ;
``````

5

# Ruby，131125字节

``````Y=->d,s{s.chars{|c|x=c.ord;\$><<(x<32?x:(x-32+d)%95+32).chr}};Y
"tdu<cKSKe;@9JKST;TPt;eGJ<r[uss_PsjivPq_Pdjid<`\plbji`e;@JUUr"
``````

``````"eUf-T<D<V,1*;<DE,EAe,V8;-cLfddPAd[ZgAbPAU[ZS-QMa]S[ZQV,1;FFc"
J=->d,s{s.chars{|c|x=c.ord;\$><<(x<32?x:(x-32-d)%95+32).chr}};J
``````

4

# oO CODE，750 744字节，两个程序中都使用了所有代码

``````CcCcccccccccCcYcccCCCccCcCcCccccccCcCcccccCcCcccCcCccCccCcCCccccCcCccccCCcCccccCCccCccCcCCcccCCCcCccccCcCCcCCcCCcCcCcCccccCCccCccCccCccCccCccCccCccccccCCCcCccCccCCcCcCcccCCcCcccCcCCcCCcCcCCccCCcCCcCCcCCcCCcCCcCCcCCcCCcCCcCcccccccCccccCccccCCccccCCcCccCCcccCccccccccccCcCccCccCccCccCcCCccCCcccCcCcCccCCcccCCCcCcccccccccccccCCccCccCcCcCcccCCccccccccccCcCccccccCcCccccCCcCccCccCCcCccccccccccCCccCcCcCcccccCcCccCcCCCcCccCccCCcCccCccCccCcCcccccCcCcccCCCcCcCccccCcCccCCcCCcCCcCcCCcccCcCCcCCcCCcCCcCCcCCcCCcCCcCCcCcCcccCccCCcccccCcCcccCcccccCcccCcccCccCccCCcCcccccccccccccCCCcccCcCcCcccCcccCCCcCccCccCccCcCCccCccCcCCCcCccccCcCccccccccCcCccCccCcCCccccccCccccccccCcccCCccCccCccCCcCCcCCcCCcCcCcCcccccCcCCcCCcCCcCCcCCcCCcCCcCccCcCCcccCCccCcCcccCCcccCCCcCC
``````

``````SsSsssssssssSsisssSSSssSsSsSssssssSsSsssssSsSsssSsSssSssSsSSssssSsSssssSSsSssssSSssSssSsSSsssSSSsSssssSsSSsSSsSSsSsSsSssssSSssSssSssSssSssSssSssSssssssSSSsSssSssSSsSsSsssSSsSsssSsSSsSSsSsSSssSSsSSsSSsSSsSSsSSsSSsSSsSSsSSsSsssssssSssssSssssSSssssSSsSssSSsssSssssssssssSsSssSssSssSssSsSSssSSsssSsSsSssSSsssSSSsSsssssssssssssSSssSssSsSsSsssSSssssssssssSsSssssssSsSssssSSsSssSssSSsSssssssssssSSssSsSsSsssssSsSssSsSSSsSssSssSSsSssSssSssSsSsssssSsSsssSSSsSsSssssSsSssSSsSSsSSsSsSSsssSsSSsSSsSSsSSsSSsSSsSSsSSsSSsSsSsssSssSSsssssSsSsssSsssssSsssSsssSssSssSSsSsssssssssssssSSSsssSsSsSsssSsssSSSsSssSssSssSsSSssSssSsSSSsSssssSsSssssssssSsSssSssSsSSssssssSssssssssSsssSSssSssSssSSsSSsSSsSSsSsSsSsssssSsSSsSSsSSsSSsSSsSSsSSsSssSsSSsssSSssSsSsssSSsssSSSsSS
``````

Brainfuck翻译：

``````+>>>+>,<[->[->+>+<<]>[-<+>]<<]>,[>++++[-<-------->]+<<+[<+>+++]<++++++++++>>[>-<-<+<-[>>>+<<<<]<-[+<-]+>>>>]<<[-]>>>[->[-<+<<+>>>]<[->+<]+<<+<<<[>[-]+[>+<+++]>++++++++++[<<->+>->-[<<<+>>>>]-[+>-]+<<<]<<]+>[->>+<<]>>->>-]<<<++++[->++++++++<]>.[-]>,]
+>>>->,<[->[->+>+<<]>[-<+>]<<]>,[>++++[-<-------->]+<<+[<+>+++]<++++++++++>>[>-<-<+<-[>>>+<<<<]<-[+<-]+>>>>]<<[-]>>>[->[-<+<<+>>>]<[->+<]+<<+<<<[>[-]+[>+<+++]>++++++++++[<<->+>->-[<<<+>>>>]-[+>-]+<<<]<<]+>[->>+<<]>>->>-]<<<++++[->++++++++<]>.[-]>,]
``````

oO CODE是Brainfuck的变体，仅字母的大小写很重要。

4

# GolfScript，95 64字节，两个程序中都使用了所有代码

``````0 0z{ 1)'[}??)9t:z21,--/; [84;%zt*84*84\$|)21*|\$Z!!\~'---|}`{)}%~
``````

``````1!1{|!2*(\~@@*:u;{32-..0<!\95<&{u+95+95%}*32+}%[""] (...}~a|*~&
``````

``````1 "0 0z{ 1)'[}??)9t:z21,--/; [84;%zt*84*84\$|)21*|\$Z!!\~'---|}`{)}%~"
``````

### 说明

``````1!1                            # Push 0 1.
{                              # Define a block and evaluate it.
|                          # Or.
!2*(                       # Get 1 for encryption, or -1 for decryption.
\~                         # Evaluate the input string.
@@*:u;                     # u = d for encryption, or -d for decryption.
{                          # For each character:
32-                    # Subtract 32.
..0<!\95<&             # Test if it is in the printable range.
{u+95+95%}*            # If so, add u (mod 95).
32+                    # Add 32 back.
}%
[""] (...                  # Push an empty array and 4 empty strings.
}~
a                              # No-op.
|*~                            # Evaluate ""*(""|"") which does nothing.
&                              # Calculate []&"" which is empty.
``````

``````0 0                            # Push 0 0.
z                              # No-op.
{                              # Define a block and get its string representation.
...                        # See decryption code.
|                          # This will be decoded into a }. The string will be truncated here when evaluated.
}`                             # Only the closing } will be truncated, but it is still used as the end of the block.
{)}%                           # Increment each character. Note that the braces before and after the block will also be incremented.
~                              # Evaluate the string.
``````

3

# 使用Javascript（ES7草案） - 167个 165字节

``````J=(s,d)=>s.replace(/[ -~]/g,x=>String.fromCharCode((x.charCodeAt()-32+d)%95+32));J
"eP<T-Qef<V;.95*,.PW\$HUG&W0TAef{=;270V/;86k1*;k8-.PPAV,1*;k8-.i=PQS^[U-QMa]S[ZQQc"``````

``````"t_Kc<`tuKeJ=HD9;=_f3WdV5f?cPtu+LJAF?e>JGEz@9JzG<=__Pe;@9JzG<=xL_`djib<`\plbji``r"
Y=(s,d)=>s.replace(/[ -~]/g,x=>String.fromCharCode((x.charCodeAt()+63-d)%95+32));Y``````

1

feersum 2014年

@feersum，它现在已修复...并且短了2个字节:)
nderscore 2014年

Martin Ender 2014年

@MartinBüttner我不明白你的意思...;）
nderscore 2014年

2

# > <>（鱼），467字节

``````ffii{{~~__:0a('0'*!.0a('0'*22(!'(~\$~_:}-}\$-a*}+{{if~~:i:0({}?;__:{}84{}*__({}?\__:{} _{}70{}g_{})_{}?\4__{}8*-_{}+{}80{}g_%4_{}8*{}+\\sl||||||||||||||||||||||||||||9||||||||||||||9||||||||||||||||||||||||||||||||||||||||||||||||||||9
>                      >                              >!;7f7-_{}!%_{}!<872-d_{}!&_{}!<[755(7(%~~_{}!<[55(7(_{}!*!*23a(_{}!'_{}!"55(7((~~_{}~~~o__'4'0.{{{o,
``````

``````iill~~""bb=3d+*3*-\$13d+*3*-55+\$*+"'"b=!0!'0d-!.~~li""=l=3+~!B>bb=~!;7~!-bb+~!B_bb=~!#b~!:3~!jb~!,b~!B_7bb~!;-0b~!.~!;3~!jb(7b~!;-~!.__vo                            <              <                                                    <
##############################################################################A######################A##############################A\$>:i:0b~!\$(b~!\$?;:50gb~!\$)b~!\$?^:88+:+(""b~!\$?^88+:+b~!\$-\$-56d+b~!\$*b~!\$%88+:++""b~!"""rbb*7*31~~~r/
``````

``````<2-digit offset> <text>
``````

## 说明

``````Encrypt:
_ : Mirror, but is a no-op if the program flow is horizontal
{ : Shift stack left
} : Shift stack right

Decrypt:
b : Push 11 to stack
~ : Pop top of stack
! : Skip the next instruction
``````

# > <>，​​149个字节

``````i68*:@-a*i@@-+i~v
4:v?)g31:;?(0:i:/8
(?v48*-+03g%48*+\*
_~\of0.   .1+1fo/
j*+:zq<6B99A6=qz6g
53Ji?C58/8;?r0?C5:
C?EiJ4r?<EFJ3;EtEg
:tAC5EK8l5tKK86t*i
``````

``````^+-~/5"V~^55" ^sk
)/k4}\(&/04|%/^/\$-
|4k)-~" %(\y)-~ Q~
TsQd[%#ttt#& &[d\$
_~ /of1+7..6+2fo+\
*(?^48*-\$-04g%48*/
84:^?)g41:;?(0:i:\
/i68*:@-a*i@@-+i~^
``````

## 说明

``````i                       read first input digit as char
68*:@-a*                subtract 48 (ASCII "0") and multiply by 10, keeping another 48 on the stack
i                       read second input digit as char
@@-+                    subtract 48 and add to 10*(first digit), giving the offset
i~                      read in space and discard it

--- LOOP ---
:                       copy the offset
i:                      read input char
:0)?;                   check if less than 0 (i.e. EOF) and terminate if so
:13g)?v                 check if greater than ~ in cell (1,3) and drop down if so
48*(?v                  check if less than 32 and drop down if so
48*-+03g%48*+           calculate Caesar shift of the char, fetching 95 from (0,3)

of1+1.                  repeat loop
of0.                    repeat loop
``````

## 编码工具

``for(var i=0;i<95;++i){var option=document.createElement("option");option.text=i;document.getElementById("offset").add(option)};function update(m){if(m==1)var code=document.getElementById("in").value;else var code=document.getElementById("out").value;var offset=parseInt(document.getElementById("offset").value);var output="";for(var i=0;i<code.length;i++){var n=code[i].charCodeAt(0);if(n<32||n>127)output+=code[i];else{var c=(n-32+offset*m)%95;output+=String.fromCharCode(c<0?c+95+32:c+32)}}if(m==1)document.getElementById("out").value=output;else document.getElementById("in").value=output};``
``<html><body><textarea id="in" onkeyup="update(1)" rows=5 style="width:100%"></textarea><textarea id="out" rows=5 style="width:100%" onkeyup="update(-1)"></textarea><select id="offset" onchange="update(1)"></select></body></html>``

1

# Perl-131

``````We;{for(split//,\$ARGV[1]){print chr(((ord\$_)-32+\$ARGV[0])%95+32)}};q!LUXmYVROZttqi'8-<AvCnaVXOTZeINXmmmUXJiEnrxwri'8-<AuCnj~zpxwnc!
``````

``````q U6!*-B.+'\$/IIF>[lapuKwC6+-\$)/:}#-BBB*-~>yCGMLE>[lapuJwC?SOEMLC88U,;for(split//,\$ARGV[1]){print chr(((ord\$_)-32-\$ARGV[0])%95+32)};
``````

@MartinBüttnerWoah，赞好！它确实有效吗？
KSFT 2014年

Martin Ender 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.